Function perform_async

Source
pub async fn perform_async(
    redis: &RedisPool,
    class: String,
    queue: String,
    args: impl Serialize,
) -> Result<()>
Expand description

Helper function for enqueueing a worker into sidekiq. This can be used to enqueue a job for a ruby sidekiq worker to process.

Examples found in repository?
examples/demo.rs (lines 168-175)
114async fn main() -> Result<()> {
115    tracing_subscriber::fmt().with_max_level(Level::INFO).init();
116
117    // Redis
118    let manager = RedisConnectionManager::new("redis://127.0.0.1/")?;
119    let redis = Pool::builder().build(manager).await?;
120
121    tokio::spawn({
122        let redis = redis.clone();
123
124        async move {
125            loop {
126                PaymentReportWorker::perform_async(
127                    &redis,
128                    PaymentReportArgs {
129                        user_guid: "USR-123".into(),
130                    },
131                )
132                .await
133                .unwrap();
134
135                tokio::time::sleep(std::time::Duration::from_secs(1)).await;
136            }
137        }
138    });
139
140    // Enqueue a job with the worker! There are many ways to do this.
141    PaymentReportWorker::perform_async(
142        &redis,
143        PaymentReportArgs {
144            user_guid: "USR-123".into(),
145        },
146    )
147    .await?;
148
149    PaymentReportWorker::perform_in(
150        &redis,
151        std::time::Duration::from_secs(10),
152        PaymentReportArgs {
153            user_guid: "USR-123".into(),
154        },
155    )
156    .await?;
157
158    PaymentReportWorker::opts()
159        .queue("brolo")
160        .perform_async(
161            &redis,
162            PaymentReportArgs {
163                user_guid: "USR-123-EXPIRED".into(),
164            },
165        )
166        .await?;
167
168    sidekiq::perform_async(
169        &redis,
170        "PaymentReportWorker".into(),
171        "yolo".into(),
172        PaymentReportArgs {
173            user_guid: "USR-123".to_string(),
174        },
175    )
176    .await?;
177
178    // Enqueue a job
179    sidekiq::perform_async(
180        &redis,
181        "PaymentReportWorker".into(),
182        "yolo".into(),
183        PaymentReportArgs {
184            user_guid: "USR-123".to_string(),
185        },
186    )
187    .await?;
188
189    // Enqueue a job with options
190    sidekiq::opts()
191        .queue("yolo".to_string())
192        .perform_async(
193            &redis,
194            "PaymentReportWorker".into(),
195            PaymentReportArgs {
196                user_guid: "USR-123".to_string(),
197            },
198        )
199        .await?;
200
201    // Sidekiq server
202    let mut p = Processor::new(redis.clone(), vec!["yolo".to_string(), "brolo".to_string()]);
203
204    // Add known workers
205    p.register(HelloWorker);
206    p.register(PaymentReportWorker::new(redis.clone()));
207
208    // Custom Middlewares
209    p.using(FilterExpiredUsersMiddleware).await;
210
211    // Reset cron jobs
212    periodic::destroy_all(redis.clone()).await?;
213
214    // Cron jobs
215    periodic::builder("0 * * * * *")?
216        .name("Payment report processing for a user using json args")
217        .queue("yolo")
218        .args(json!({ "user_guid": "USR-123-PERIODIC-FROM-JSON-ARGS" }))?
219        .register(&mut p, PaymentReportWorker::new(redis.clone()))
220        .await?;
221
222    periodic::builder("0 * * * * *")?
223        .name("Payment report processing for a user using typed args")
224        .queue("yolo")
225        .args(PaymentReportArgs {
226            user_guid: "USR-123-PERIODIC-FROM-TYPED-ARGS".to_string(),
227        })?
228        .register(&mut p, PaymentReportWorker::new(redis.clone()))
229        .await?;
230
231    p.run().await;
232    Ok(())
233}