1
2
3
4
5
6
7
8
9
10
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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
//! # later
//!
//! A distributed background job manager and runner for Rust. This is currently in PoC stage.
//!
//! ## Set up
//! ### 1. Import `later` and required dependencies
//!
//! ```toml
//! later = { version = "0.0.7", features = ["redis", "postgres"] }
//! serde = "1.0"
//! ```
//!
//! ### 2. Define some types to use as a payload to the background jobs
//!
//! ```
//! use serde::{Deserialize, Serialize};
//!
//! #[derive(Serialize, Deserialize)] // <- Required derives
//! pub struct SendEmail {
//! pub address: String,
//! pub body: String,
//! }
//!
//! // ... more as required
//! ```
//!
//! ### 3. Generate the stub
//!
//! ```
//! # use serde::{Deserialize, Serialize};
//! #
//! # #[derive(Serialize, Deserialize)] // <- Required derives
//! # pub struct SendEmail {
//! # pub address: String,
//! # pub body: String,
//! # }
//! later::background_job! {
//! struct Jobs {
//! send_email: SendEmail,
//! }
//! }
//! ```
//!
//! This generates two types
//! * `JobsBuilder` - used to bootstrap the background job server - which can be used to enqueue jobs,
//! * `JobContext<T>` - used to pass application context (`T`) in the handler as well as enqueue jobs,
//!
//! ### 4. Use the generated code to bootstrap the background job server
//!
//! For `struct Jobs` a type `JobsBuilder` will be generated. Use this to bootstrap the server.
//!
//! ```no_run
//! # use serde::{Deserialize, Serialize};
//! #
//! # #[derive(Serialize, Deserialize)]
//! # pub struct SendEmail { pub address: String, pub body: String }
//! # pub struct MyContext {}
//! # later::background_job! {
//! # struct Jobs {
//! # send_email: SendEmail,
//! # }
//! # }
//! use later::{storage::redis::Redis, BackgroundJobServer, mq::amqp, Config};
//!
//! # #[tokio::main]
//! # async fn main() {
//! // bootstrap the server
//! let ctx = MyContext{ /*..*/ }; // Any context to pass onto the handlers
//! let storage = Redis::new("redis://127.0.0.1/") // More storage option to be available later
//! .await
//! .expect("connect to redis");
//! let mq = amqp::RabbitMq::new("amqp://guest:guest@localhost:5672".into()); // RabbitMq instance
//! let ctx = JobsBuilder::new(
//! later::Config::builder()
//! .name("fnf-example".into()) // Unique name for this app
//! .context(ctx) // Pass the context here
//! .storage(Box::new(storage)) // Configure storage
//! .message_queue_client(Box::new(mq)) // Configure mq
//! // ...
//! .build()
//! )
//! // for each payload defined in the `struct Jobs` above
//! // the generated fn name uses the pattern "with_[name]_handler"
//! .with_send_email_handler(handle_send_email) // Pass the handler function
//! // ..
//! .build()
//! .await
//! .expect("start BG Jobs server");
//!
//! // use ctx.enqueue(SendEmail{ ... }) to enqueue jobs,
//! // or ctx.enqueue_continue(parent_job_id, SendEmail{ ... }) to chain jobs.
//! // this will only accept types defined inside the macro above
//! # }
//! // define handler
//! async fn handle_send_email(
//! ctx: JobsContext<MyContext>, // JobContext is generated wrapper
//! payload: SendEmail,
//! ) -> anyhow::Result<()> {
//! // handle `payload`
//!
//! // ctx.app -> Access the MyContext passed during bootstrapping
//! // ctx.enqueue(_).await to enqueue more jobs
//! // ctx.enqueue_continue(_).await to chain jobs
//!
//! Ok(()) // or Err(_) to retry this message
//! }
//! ```
//!
//! This example use `Redis` storage. More storage is available in the [`storage`] module.
//!
//! ---
//!
//! ## Fire and forget jobs
//!
//! Fire and forget jobs are executed only once and executed by an available worker almost immediately.
//!
//! ```no_run
//! # #[derive(serde::Serialize, serde::Deserialize)]
//! # pub struct SendEmail { pub address: String, pub body: String }
//! # later::background_job! {
//! # struct Jobs {
//! # send_email: SendEmail,
//! # }
//! # }
//! # #[tokio::main]
//! # async fn main() -> anyhow::Result<()>{
//! # let ctx : later::BackgroundJobServerPublisher = todo!();
//! ctx.enqueue(SendEmail{
//! address: "hello@rust-lang.org".to_string(),
//! body: "You rock!".to_string()
//! }).await?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Continuations
//!
//! One or many jobs are chained together to create an workflow. Child jobs are executed **only when parent job has been finished**.
//!
//! ```no_run
//! # #[derive(serde::Serialize, serde::Deserialize)]
//! # pub struct SendEmail { pub address: String, pub body: String }
//! # later::background_job! {
//! # struct Jobs {
//! # send_email: SendEmail,
//! # create_account: CreateAccount,
//! # }
//! # }
//! # #[derive(serde::Serialize, serde::Deserialize)]
//! # pub struct CreateAccount { id: String }
//! # #[tokio::main]
//! # async fn main() -> anyhow::Result<()>{
//! # let ctx : later::BackgroundJobServerPublisher = todo!();
//! let email_welcome = ctx.enqueue(SendEmail{
//! address: "customer@example.com".to_string(),
//! body: "Creating your account!".to_string()
//! }).await?;
//!
//! let create_account = ctx.enqueue_continue(email_welcome, CreateAccount { id: "accout-1".to_string() }).await?;
//!
//! let email_confirmation = ctx.enqueue_continue(create_account, SendEmail{
//! address: "customer@example.com".to_string(),
//! body: "Your account has been created!".to_string()
//! }).await;
//! # Ok(())
//! # }
//! ```
//!
//! ## Delayed jobs
//!
//! Just like fire and forget jobs that starts after a certain interval.
//!
//! ```no_run
//! # #[derive(serde::Serialize, serde::Deserialize)]
//! # pub struct SendEmail { pub address: String, pub body: String }
//! # later::background_job! {
//! # struct Jobs {
//! # send_email: SendEmail,
//! # }
//! # }
//! # #[tokio::main]
//! # async fn main() -> anyhow::Result<()>{
//! # let ctx : later::BackgroundJobServerPublisher = todo!();
//! // delay
//! ctx.enqueue_delayed(SendEmail{
//! address: "hello@rust-lang.org".to_string(),
//! body: "You rock!".to_string()
//! }, std::time::Duration::from_secs(60)).await?;
//!
//! // specific time
//! let run_job_at : chrono::DateTime<chrono::Utc> = todo!();
//! ctx.enqueue_delayed_at(SendEmail{
//! address: "hello@rust-lang.org".to_string(),
//! body: "You rock!".to_string()
//! }, run_job_at).await?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Recurring jobs
//!
//! Run recurring job based on cron schedule.
//!
//! ```no_run
//! # #[derive(serde::Serialize, serde::Deserialize)]
//! # pub struct SendNewsletter { pub address: String }
//! # later::background_job! {
//! # struct Jobs {
//! # send_newsletter: SendNewsletter,
//! # }
//! # }
//! # #[tokio::main]
//! # async fn main() -> anyhow::Result<()>{
//! # let ctx : later::BackgroundJobServerPublisher = todo!();
//! ctx.enqueue_recurring("send-newsletter-1".to_string(),
//! SendNewsletter{
//! address: "hello@rust-lang.org".to_string(),
//! },
//! "0 6 1 * * *".to_string() // 6am, 1st day of every month
//! ).await?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Storage
//!
//! * `redis`: `later::storage::Redis::new("redis://127.0.0.1/").await`
//! * `postgres`: `later::storage::Postgres::new("postgres://test:test@localhost/later_test").await` (Requires feature `postgres`)
use crate BgJobHandler;
use ;
use Persist;
use ;
use EventsHandler;
use ;
use Storage;
use JoinHandle;
use TypedBuilder;
pub use anyhow;
pub use async_trait;
pub use futures;
pub use background_job;
pub use instrument;
pub type UtcDateTime = DateTime;
;
;
// ToDo: Remove H - use Box<dyn BgJobHandler<C>>