Struct sidekiq::WorkerOpts
source · pub struct WorkerOpts<Args, W: Worker<Args> + ?Sized> { /* private fields */ }Implementations§
source§impl<Args, W> WorkerOpts<Args, W>where
W: Worker<Args>,
impl<Args, W> WorkerOpts<Args, W>where
W: Worker<Args>,
sourcepub fn new() -> Self
pub fn new() -> Self
Examples found in repository?
More examples
pub fn retry(self, retry: bool) -> Self
sourcepub fn queue<S: Into<String>>(self, queue: S) -> Self
pub fn queue<S: Into<String>>(self, queue: S) -> Self
Examples found in repository?
More examples
examples/demo.rs (line 52)
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
fn opts() -> sidekiq::WorkerOpts<PaymentReportArgs, Self> {
sidekiq::WorkerOpts::new().queue("yolo")
}
async fn perform(&self, args: PaymentReportArgs) -> Result<()> {
use redis::AsyncCommands;
let times_called: usize = self
.redis
.get()
.await?
.unnamespaced_borrow_mut()
.incr("example_of_accessing_the_raw_redis_connection", 1)
.await?;
debug!({ "times_called" = times_called }, "Called this worker");
self.send_report(args.user_guid).await
}
}
struct FilterExpiredUsersMiddleware;
#[derive(Deserialize)]
struct FiltereExpiredUsersArgs {
user_guid: String,
}
impl FiltereExpiredUsersArgs {
fn is_expired(&self) -> bool {
self.user_guid == "USR-123-EXPIRED"
}
}
#[async_trait]
impl ServerMiddleware for FilterExpiredUsersMiddleware {
async fn call(
&self,
chain: ChainIter,
job: &Job,
worker: Arc<WorkerRef>,
redis: RedisPool,
) -> Result<()> {
let args: std::result::Result<(FiltereExpiredUsersArgs,), serde_json::Error> =
serde_json::from_value(job.args.clone());
// If we can safely deserialize then attempt to filter based on user guid.
if let Ok((filter,)) = args {
if filter.is_expired() {
error!({
"class" = &job.class,
"jid" = &job.jid,
"user_guid" = filter.user_guid
}, "Detected an expired user, skipping this job");
return Ok(());
}
}
chain.next(job, worker, redis).await
}
}
#[tokio::main]
async fn main() -> Result<()> {
tracing_subscriber::fmt().with_max_level(Level::INFO).init();
// Redis
let manager = RedisConnectionManager::new("redis://127.0.0.1/")?;
let redis = Pool::builder().build(manager).await?;
tokio::spawn({
let redis = redis.clone();
async move {
loop {
PaymentReportWorker::perform_async(
&redis,
PaymentReportArgs {
user_guid: "USR-123".into(),
},
)
.await
.unwrap();
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
}
}
});
// Enqueue a job with the worker! There are many ways to do this.
PaymentReportWorker::perform_async(
&redis,
PaymentReportArgs {
user_guid: "USR-123".into(),
},
)
.await?;
PaymentReportWorker::perform_in(
&redis,
std::time::Duration::from_secs(10),
PaymentReportArgs {
user_guid: "USR-123".into(),
},
)
.await?;
PaymentReportWorker::opts()
.queue("brolo")
.perform_async(
&redis,
PaymentReportArgs {
user_guid: "USR-123-EXPIRED".into(),
},
)
.await?;
sidekiq::perform_async(
&redis,
"PaymentReportWorker".into(),
"yolo".into(),
PaymentReportArgs {
user_guid: "USR-123".to_string(),
},
)
.await?;
// Enqueue a job
sidekiq::perform_async(
&redis,
"PaymentReportWorker".into(),
"yolo".into(),
PaymentReportArgs {
user_guid: "USR-123".to_string(),
},
)
.await?;
// Enqueue a job with options
sidekiq::opts()
.queue("yolo".to_string())
.perform_async(
&redis,
"PaymentReportWorker".into(),
PaymentReportArgs {
user_guid: "USR-123".to_string(),
},
)
.await?;
// Sidekiq server
let mut p = Processor::new(redis.clone(), vec!["yolo".to_string(), "brolo".to_string()]);
// Add known workers
p.register(HelloWorker);
p.register(PaymentReportWorker::new(redis.clone()));
// Custom Middlewares
p.using(FilterExpiredUsersMiddleware).await;
// Reset cron jobs
periodic::destroy_all(redis.clone()).await?;
// Cron jobs
periodic::builder("0 * * * * *")?
.name("Payment report processing for a user using json args")
.queue("yolo")
.args(json!({ "user_guid": "USR-123-PERIODIC-FROM-JSON-ARGS" }))?
.register(&mut p, PaymentReportWorker::new(redis.clone()))
.await?;
periodic::builder("0 * * * * *")?
.name("Payment report processing for a user using typed args")
.queue("yolo")
.args(PaymentReportArgs {
user_guid: "USR-123-PERIODIC-FROM-TYPED-ARGS".to_string(),
})?
.register(&mut p, PaymentReportWorker::new(redis.clone()))
.await?;
p.run().await;
Ok(())
}sourcepub fn unique_for(self, unique_for: Duration) -> Self
pub fn unique_for(self, unique_for: Duration) -> Self
Examples found in repository?
examples/unique.rs (line 15)
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
fn opts() -> sidekiq::WorkerOpts<CustomerNotification, Self> {
// Use default options to set the unique_for option by default.
sidekiq::WorkerOpts::new()
.queue("customers")
.unique_for(std::time::Duration::from_secs(30))
}
async fn perform(&self, _args: CustomerNotification) -> Result<()> {
Ok(())
}
}
#[derive(Deserialize, Debug, Serialize)]
struct CustomerNotification {
customer_guid: String,
}
#[tokio::main]
async fn main() -> Result<()> {
tracing_subscriber::fmt::init();
// Redis
let manager = RedisConnectionManager::new("redis://127.0.0.1/")?;
let redis = Pool::builder().build(manager).await?;
// Sidekiq server
let mut p = Processor::new(redis.clone(), vec!["customers".to_string()]);
// Add known workers
p.register(CustomerNotificationWorker);
// Create a bunch of jobs with the default uniqueness options. Only
// one of these should be created within a 30 second period.
for _ in 1..10 {
CustomerNotificationWorker::perform_async(
&redis,
CustomerNotification {
customer_guid: "CST-123".to_string(),
},
)
.await?;
}
// Override the unique_for option. Note: Because the code above
// uses the default unique_for value of 30, this code is essentially
// a no-op.
CustomerNotificationWorker::opts()
.unique_for(std::time::Duration::from_secs(90))
.perform_async(
&redis,
CustomerNotification {
customer_guid: "CST-123".to_string(),
},
)
.await?;
p.run().await;
Ok(())
}sourcepub async fn perform_async(
&self,
redis: &RedisPool,
args: impl Serialize + Send + 'static
) -> Result<()>
pub async fn perform_async( &self, redis: &RedisPool, args: impl Serialize + Send + 'static ) -> Result<()>
Examples found in repository?
examples/unique.rs (lines 59-64)
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
async fn main() -> Result<()> {
tracing_subscriber::fmt::init();
// Redis
let manager = RedisConnectionManager::new("redis://127.0.0.1/")?;
let redis = Pool::builder().build(manager).await?;
// Sidekiq server
let mut p = Processor::new(redis.clone(), vec!["customers".to_string()]);
// Add known workers
p.register(CustomerNotificationWorker);
// Create a bunch of jobs with the default uniqueness options. Only
// one of these should be created within a 30 second period.
for _ in 1..10 {
CustomerNotificationWorker::perform_async(
&redis,
CustomerNotification {
customer_guid: "CST-123".to_string(),
},
)
.await?;
}
// Override the unique_for option. Note: Because the code above
// uses the default unique_for value of 30, this code is essentially
// a no-op.
CustomerNotificationWorker::opts()
.unique_for(std::time::Duration::from_secs(90))
.perform_async(
&redis,
CustomerNotification {
customer_guid: "CST-123".to_string(),
},
)
.await?;
p.run().await;
Ok(())
}More examples
examples/demo.rs (lines 160-165)
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
async fn main() -> Result<()> {
tracing_subscriber::fmt().with_max_level(Level::INFO).init();
// Redis
let manager = RedisConnectionManager::new("redis://127.0.0.1/")?;
let redis = Pool::builder().build(manager).await?;
tokio::spawn({
let redis = redis.clone();
async move {
loop {
PaymentReportWorker::perform_async(
&redis,
PaymentReportArgs {
user_guid: "USR-123".into(),
},
)
.await
.unwrap();
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
}
}
});
// Enqueue a job with the worker! There are many ways to do this.
PaymentReportWorker::perform_async(
&redis,
PaymentReportArgs {
user_guid: "USR-123".into(),
},
)
.await?;
PaymentReportWorker::perform_in(
&redis,
std::time::Duration::from_secs(10),
PaymentReportArgs {
user_guid: "USR-123".into(),
},
)
.await?;
PaymentReportWorker::opts()
.queue("brolo")
.perform_async(
&redis,
PaymentReportArgs {
user_guid: "USR-123-EXPIRED".into(),
},
)
.await?;
sidekiq::perform_async(
&redis,
"PaymentReportWorker".into(),
"yolo".into(),
PaymentReportArgs {
user_guid: "USR-123".to_string(),
},
)
.await?;
// Enqueue a job
sidekiq::perform_async(
&redis,
"PaymentReportWorker".into(),
"yolo".into(),
PaymentReportArgs {
user_guid: "USR-123".to_string(),
},
)
.await?;
// Enqueue a job with options
sidekiq::opts()
.queue("yolo".to_string())
.perform_async(
&redis,
"PaymentReportWorker".into(),
PaymentReportArgs {
user_guid: "USR-123".to_string(),
},
)
.await?;
// Sidekiq server
let mut p = Processor::new(redis.clone(), vec!["yolo".to_string(), "brolo".to_string()]);
// Add known workers
p.register(HelloWorker);
p.register(PaymentReportWorker::new(redis.clone()));
// Custom Middlewares
p.using(FilterExpiredUsersMiddleware).await;
// Reset cron jobs
periodic::destroy_all(redis.clone()).await?;
// Cron jobs
periodic::builder("0 * * * * *")?
.name("Payment report processing for a user using json args")
.queue("yolo")
.args(json!({ "user_guid": "USR-123-PERIODIC-FROM-JSON-ARGS" }))?
.register(&mut p, PaymentReportWorker::new(redis.clone()))
.await?;
periodic::builder("0 * * * * *")?
.name("Payment report processing for a user using typed args")
.queue("yolo")
.args(PaymentReportArgs {
user_guid: "USR-123-PERIODIC-FROM-TYPED-ARGS".to_string(),
})?
.register(&mut p, PaymentReportWorker::new(redis.clone()))
.await?;
p.run().await;
Ok(())
}pub async fn perform_in( &self, redis: &RedisPool, duration: Duration, args: impl Serialize + Send + 'static ) -> Result<()>
Trait Implementations§
source§impl<Args, W: Worker<Args>> Default for WorkerOpts<Args, W>
impl<Args, W: Worker<Args>> Default for WorkerOpts<Args, W>
source§impl<Args, W: Worker<Args>> From<&WorkerOpts<Args, W>> for EnqueueOpts
impl<Args, W: Worker<Args>> From<&WorkerOpts<Args, W>> for EnqueueOpts
source§fn from(opts: &WorkerOpts<Args, W>) -> Self
fn from(opts: &WorkerOpts<Args, W>) -> Self
Converts to this type from the input type.
Auto Trait Implementations§
impl<Args, W> Freeze for WorkerOpts<Args, W>where
W: ?Sized,
impl<Args, W> RefUnwindSafe for WorkerOpts<Args, W>
impl<Args, W> Send for WorkerOpts<Args, W>
impl<Args, W> Sync for WorkerOpts<Args, W>
impl<Args, W> Unpin for WorkerOpts<Args, W>
impl<Args, W> UnwindSafe for WorkerOpts<Args, W>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more