effectum 0.7.0

An embeddable task queue based on SQLite
Documentation
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
use std::{
    fmt::Debug,
    sync::{
        atomic::{AtomicU32, AtomicU64, Ordering},
        Arc,
    },
};

use ahash::HashMap;
use tokio::{
    sync::{oneshot, Notify},
    task::JoinHandle,
    time::Instant,
};
use tracing::{event, instrument, Level, Span};

use crate::{
    db_writer::{
        ready_jobs::{GetReadyJobsArgs, ReadyJob},
        DbOperation, DbOperationType,
    },
    job_registry::{JobRegistry, JobRunner},
    shared_state::{SharedState, Time},
    worker_list::ListeningWorker,
    Error, Queue, Result, SmartString,
};

/// The internal ID for a worker.
pub type WorkerId = u64;

struct CancellableTask {
    close_tx: oneshot::Sender<()>,
    join_handle: JoinHandle<()>,
}

/// A worker that runs jobs from the queue. Dropping a worker will disconnect it and stop running
/// jobs.
#[must_use = "Workers must be stored. Dropping a worker early will disconnect it from the queue."]
pub struct Worker {
    /// The worker's internal ID.
    pub id: WorkerId,
    counts: Arc<RunningJobs>,
    worker_list_task: Option<CancellableTask>,
}

pub struct WorkerCounts {
    pub started: u64,
    pub finished: u64,
}

impl Worker {
    /// Unregister a worker from the queue. It will still finish any jobs it is currently running,
    /// but will no longer take new jobs.
    pub async fn unregister(mut self, timeout: Option<std::time::Duration>) -> Result<()> {
        if let Some(task) = self.worker_list_task.take() {
            event!(Level::INFO, worker_id = %self.id, "Unregistering worker");
            task.close_tx.send(()).ok();
            if let Some(timeout) = timeout {
                tokio::time::timeout(timeout, task.join_handle)
                    .await
                    .map_err(|_| Error::Timeout)??;
            } else {
                task.join_handle.await?;
            }
        }
        Ok(())
    }

    /// Create a [WorkerBuilder] to build a new worker.
    pub fn builder<CONTEXT>(queue: &Queue, context: CONTEXT) -> WorkerBuilder<CONTEXT>
    where
        CONTEXT: Send + Sync + Debug + Clone + 'static,
    {
        WorkerBuilder::new(queue, context)
    }

    /// Return some counts about the number of jobs this worker has processed.
    pub fn counts(&self) -> WorkerCounts {
        WorkerCounts {
            started: self.counts.started.load(Ordering::Relaxed),
            finished: self.counts.finished.load(Ordering::Relaxed),
        }
    }
}

impl Drop for Worker {
    fn drop(&mut self) {
        if let Some(task) = self.worker_list_task.take() {
            event!(Level::INFO, worker_id = %self.id, "Unregistering worker");
            task.close_tx.send(()).ok();
            tokio::spawn(task.join_handle);
        }
    }
}

/// A builder object for a [Worker].
pub struct WorkerBuilder<'a, CONTEXT>
where
    CONTEXT: Send + Sync + Debug + Clone + 'static,
{
    /// The job registry from which this worker should take its job functions.
    registry: Option<&'a JobRegistry<CONTEXT>>,
    job_defs: Option<Vec<JobRunner<CONTEXT>>>,
    queue: &'a Queue,
    /// The context value to send to the worker's jobs.
    context: CONTEXT,
    /// Limit the job types this worker will run. Defaults to all job types in the registry.
    jobs: Vec<SmartString>,
    /// Fetch new jobs when the number of running jobs drops to this number. Defaults to
    /// the same as max_concurrency.
    min_concurrency: Option<u16>,
    /// The maximum number of jobs that can be run concurrently. Defaults to 1, but you will
    /// usually want to set this to a higher number.
    max_concurrency: Option<u16>,
}

impl<'a, CONTEXT> WorkerBuilder<'a, CONTEXT>
where
    CONTEXT: Send + Sync + Debug + Clone + 'static,
{
    /// Create a new [WorkerBuilder] for a particular [Queue].
    pub fn new(queue: &'a Queue, context: CONTEXT) -> Self {
        Self {
            registry: None,
            job_defs: None,
            queue,
            context,
            jobs: Vec::new(),
            min_concurrency: None,
            max_concurrency: None,
        }
    }

    /// Get the job definitions from this [JobRegistry].
    pub fn registry(mut self, registry: &'a JobRegistry<CONTEXT>) -> Self {
        if self.job_defs.is_some() {
            panic!("Cannot set both registry and job_defs");
        }

        self.registry = Some(registry);
        self
    }

    /// Get the job definitions from this list of [JobRunners](JobRunner).
    pub fn jobs(mut self, jobs: impl Into<Vec<JobRunner<CONTEXT>>>) -> Self {
        if self.job_defs.is_some() {
            panic!("Cannot set both registry and job_defs");
        }

        self.job_defs = Some(jobs.into());
        self
    }

    fn has_job_type(&self, job_type: &str) -> bool {
        if let Some(job_defs) = self.job_defs.as_ref() {
            job_defs.iter().any(|job_def| job_def.name == job_type)
        } else if let Some(registry) = self.registry.as_ref() {
            registry.jobs.contains_key(job_type)
        } else {
            panic!("Must set either registry or job_defs");
        }
    }

    /// Limit this worker to only running these job types, even if the registry contains more
    /// types.
    pub fn limit_job_types(mut self, job_types: &[impl AsRef<str>]) -> Self {
        self.jobs = job_types
            .iter()
            .map(|s| {
                assert!(
                    self.has_job_type(s.as_ref()),
                    "Job type {} not found in registry",
                    s.as_ref()
                );

                SmartString::from(s.as_ref())
            })
            .collect();
        self
    }

    /// Set the minimum concurrency for this worker. When the number of running jobs falls below
    /// this number, the worker will try to fetch more jobs, up to `max_concurrency`.
    /// Defaults to the same as max_concurrency.
    pub fn min_concurrency(mut self, min_concurrency: u16) -> Self {
        assert!(min_concurrency > 0);
        self.min_concurrency = Some(min_concurrency);
        self
    }

    /// The maximum number of jobs that the worker will run concurrently. Defaults to 1.
    pub fn max_concurrency(mut self, max_concurrency: u16) -> Self {
        assert!(max_concurrency > 0);
        self.max_concurrency = Some(max_concurrency);
        self
    }

    /// Consume this [WorkerBuilder] and create a new [Worker]. The Worker must be stored, as it
    /// will automatically disconnect from the Queue when it is dropped.
    pub async fn build(self) -> Result<Worker> {
        let job_defs: HashMap<SmartString, JobRunner<CONTEXT>> =
            if let Some(job_defs) = self.job_defs {
                job_defs
                    .into_iter()
                    .filter(|job| self.jobs.is_empty() || self.jobs.contains(&job.name))
                    .map(|job| (job.name.clone(), job))
                    .collect()
            } else if let Some(registry) = self.registry {
                let job_list = if self.jobs.is_empty() {
                    registry.jobs.keys().cloned().collect()
                } else {
                    self.jobs
                };

                job_list
                    .iter()
                    .filter_map(|job| {
                        registry
                            .jobs
                            .get(job)
                            .map(|job_def| (job.clone(), job_def.clone()))
                    })
                    .collect()
            } else {
                panic!("Must set either registry or jobs");
            };

        let max_concurrency = self.max_concurrency.unwrap_or(1).max(1);
        let min_concurrency = self.min_concurrency.unwrap_or(max_concurrency).max(1);

        let job_list = job_defs.keys().cloned().collect::<Vec<_>>();

        event!(
            Level::INFO,
            ?job_list,
            min_concurrency,
            max_concurrency,
            "Starting worker",
        );

        let (close_tx, close_rx) = oneshot::channel();

        let mut workers = self.queue.state.workers.write().await;
        let listener = workers.add_worker(&job_list);
        drop(workers);

        let counts = Arc::new(RunningJobs {
            started: AtomicU64::new(0),
            finished: AtomicU64::new(0),
            current_weighted: AtomicU32::new(0),
            job_finished: Notify::new(),
        });

        let worker_id = listener.id;
        let worker_internal = WorkerInternal {
            listener,
            running_jobs: counts.clone(),
            job_list: job_list.into_iter().map(String::from).collect(),
            job_defs: Arc::new(job_defs),
            queue: self.queue.state.clone(),
            context: self.context,
            min_concurrency,
            max_concurrency,
        };

        let join_handle = tokio::spawn(worker_internal.run(close_rx));

        Ok(Worker {
            id: worker_id,
            counts,
            worker_list_task: Some(CancellableTask {
                close_tx,
                join_handle,
            }),
        })
    }
}

pub(crate) struct RunningJobs {
    pub started: AtomicU64,
    pub finished: AtomicU64,
    pub current_weighted: AtomicU32,
    pub job_finished: Notify,
}

struct WorkerInternal<CONTEXT>
where
    CONTEXT: Send + Sync + Debug + Clone + 'static,
{
    listener: Arc<ListeningWorker>,
    queue: SharedState,
    job_list: Vec<String>,
    job_defs: Arc<HashMap<SmartString, JobRunner<CONTEXT>>>,
    running_jobs: Arc<RunningJobs>,
    context: CONTEXT,
    min_concurrency: u16,
    max_concurrency: u16,
}

pub(crate) fn log_error<T, E>(result: Result<T, E>)
where
    E: std::error::Error,
{
    if let Err(e) = result {
        event!(Level::ERROR, ?e);
    }
}

impl<CONTEXT> WorkerInternal<CONTEXT>
where
    CONTEXT: Send + Sync + Debug + Clone + 'static,
{
    #[instrument(parent = None, name="worker_loop", skip_all, fields(worker_id = %self.listener.id))]
    async fn run(self, mut close_rx: oneshot::Receiver<()>) {
        let mut global_close_rx = self.queue.close.clone();
        loop {
            let mut running_jobs = self.running_jobs.current_weighted.load(Ordering::Relaxed);
            let min_concurrency = self.min_concurrency as u32;
            if running_jobs < min_concurrency {
                log_error(self.run_ready_jobs().await);
                running_jobs = self.running_jobs.current_weighted.load(Ordering::Relaxed);
            }

            let grab_new_jobs = running_jobs < min_concurrency;

            tokio::select! {
                biased;
                _ = &mut close_rx => {
                    log_error(self.shutdown().await);
                    break;
                }
                _ = global_close_rx.changed() => {
                    log_error(self.shutdown().await);
                    break;
                }
                _ = self.listener.notify_task_ready.notified(), if grab_new_jobs  => {
                    event!(Level::TRACE, "New task ready");
                }
                _ = self.running_jobs.job_finished.notified() => {
                    event!(Level::TRACE, "Job finished");
                }
            }
        }
    }

    async fn shutdown(&self) -> Result<()> {
        let mut running_jobs = self.running_jobs.current_weighted.load(Ordering::Relaxed);
        while running_jobs > 0 {
            self.running_jobs.job_finished.notified().await;
            running_jobs = self.running_jobs.current_weighted.load(Ordering::Relaxed);
        }

        let mut workers = self.queue.workers.write().await;
        workers.remove_worker(self.listener.id)
    }

    async fn run_ready_jobs(&self) -> Result<()> {
        let running_count = self.running_jobs.current_weighted.load(Ordering::Relaxed);
        let max_concurrency = self.max_concurrency as u32;
        let max_jobs = max_concurrency - running_count;
        let job_types = self
            .job_list
            .iter()
            .map(|s| rusqlite::types::Value::from(s.clone()))
            .collect::<Vec<_>>();

        let running_jobs = self.running_jobs.clone();
        let worker_id = self.listener.id;
        let now = self.queue.time.now();
        event!(Level::TRACE, %now, current_running = %running_count, %max_concurrency, "Checking ready jobs");

        let (result_tx, result_rx) = oneshot::channel();
        self.queue
            .db_write_tx
            .send(DbOperation {
                worker_id,
                span: Span::current(),
                operation: DbOperationType::GetReadyJobs(GetReadyJobsArgs {
                    job_types,
                    max_jobs,
                    max_concurrency,
                    running_jobs,
                    now,
                    result_tx,
                }),
            })
            .await
            .map_err(|_| Error::QueueClosed)?;

        let ready_jobs = result_rx.await.map_err(|_| Error::QueueClosed)??;

        for job in ready_jobs {
            self.run_job(job).await?;
        }

        Ok(())
    }

    #[instrument(level="debug", skip(self, done), fields(worker_id = %self.listener.id))]
    async fn run_job(
        &self,
        ReadyJob {
            job,
            done_rx: mut done,
        }: ReadyJob,
    ) -> Result<()> {
        let job_def = self
            .job_defs
            .get(job.job_type.as_str())
            .expect("Got job for unsupported type");

        let worker_id = self.listener.id;
        let running = self.running_jobs.clone();
        let autoheartbeat = job_def.autoheartbeat;
        let time = job.queue.time.clone();

        (job_def.runner)(job.clone(), self.context.clone());

        tokio::spawn(async move {
            let use_autohearbeat = autoheartbeat && job.heartbeat_increment > 0;
            event!(Level::DEBUG, ?job, "Starting job monitor task");
            loop {
                let expires = job.expires.load(Ordering::Relaxed);
                let expires_instant = time.instant_for_timestamp(expires);

                tokio::select! {
                    _ = wait_for_next_autoheartbeat(&time, expires, job.heartbeat_increment), if use_autohearbeat => {
                        event!(Level::DEBUG, %job, "Sending autoheartbeat");
                        let new_time =
                            crate::job::send_heartbeat(job.job_id, worker_id, job.heartbeat_increment, &job.queue).await;

                        match new_time {
                            Ok(new_time) => job.expires.store(new_time.unix_timestamp(), Ordering::Relaxed),
                            Err(e) => event!(Level::ERROR, ?e),
                        }
                    }
                    _ = tokio::time::sleep_until(expires_instant) => {
                        event!(Level::DEBUG, %job, "Job expired");
                        let now_expires = job.expires.load(Ordering::Relaxed);
                        if now_expires == expires {
                            if !job.is_done().await {
                                log_error(job.fail("Job expired").await);
                            }
                            break;
                        }
                    }
                    _ = done.changed() => {
                        break;
                    }
                }
            }

            // Do this in a separate task from the job runner so that even if something goes horribly wrong
            // we'll still be able to update the internal counts.
            running
                .current_weighted
                .fetch_sub(job.weight as u32, Ordering::Relaxed);
            running.finished.fetch_add(1, Ordering::Relaxed);
            running.job_finished.notify_one();
        });

        Ok(())
    }
}

async fn wait_for_next_autoheartbeat(time: &Time, expires: i64, heartbeat_increment: i32) {
    let now = time.now();
    let before = (heartbeat_increment.min(30) / 2) as i64;
    let next_heartbeat_time = expires - before;

    let time_from_now = next_heartbeat_time - now.unix_timestamp();
    let instant = Instant::now() + std::time::Duration::from_secs(time_from_now.max(0) as u64);

    tokio::time::sleep_until(instant).await
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test_util::TestEnvironment;

    #[tokio::test]
    #[should_panic]
    async fn worker_without_jobs_should_panic() {
        let test = TestEnvironment::new().await;
        let _ = Worker::builder(&test.queue, test.context.clone())
            .build()
            .await
            .unwrap();
    }
}