-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
postgres.rs
784 lines (697 loc) · 25.8 KB
/
postgres.rs
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
//! # apalis-postgres
//!
//! Allows using postgres as a Backend
//!
//! ## Postgres Example
//! ```rust,no_run
//! use apalis::prelude::*;
//! # use apalis_sql::postgres::PostgresStorage;
//! # use apalis_sql::postgres::PgPool;
//! use email_service::Email;
//!
//! #[tokio::main]
//! async fn main() -> std::io::Result<()> {
//! std::env::set_var("RUST_LOG", "debug,sqlx::query=error");
//! let database_url = std::env::var("DATABASE_URL").expect("Must specify url to db");
//! let pool = PgPool::connect(&database_url).await.unwrap();
//!
//! PostgresStorage::setup(&pool).await.unwrap();
//! let pg: PostgresStorage<Email> = PostgresStorage::new(pool);
//!
//! async fn send_email(job: Email, data: Data<usize>) -> Result<(), Error> {
//! /// execute job
//! Ok(())
//! }
//! // This can be even in another program/language
//! // let query = "Select apalis.push_job('apalis::Email', json_build_object('subject', 'Test apalis', 'to', 'test1@example.com', 'text', 'Lorem Ipsum'));";
//! // pg.execute(query).await.unwrap();
//!
//! Monitor::<TokioExecutor>::new()
//! .register_with_count(4, {
//! WorkerBuilder::new(&format!("tasty-avocado"))
//! .data(0usize)
//! .source(pg)
//! .build_fn(send_email)
//! })
//! .run()
//! .await
//! }
//! ```
use crate::context::SqlContext;
use crate::Config;
use apalis_core::codec::json::JsonCodec;
use apalis_core::error::Error;
use apalis_core::layers::{Ack, AckLayer};
use apalis_core::notify::Notify;
use apalis_core::poller::controller::Controller;
use apalis_core::poller::stream::BackendStream;
use apalis_core::poller::Poller;
use apalis_core::request::{Request, RequestStream};
use apalis_core::storage::{Job, Storage};
use apalis_core::task::task_id::TaskId;
use apalis_core::worker::WorkerId;
use apalis_core::{Backend, Codec};
use async_stream::try_stream;
use futures::{FutureExt, Stream};
use futures::{StreamExt, TryStreamExt};
use log::error;
use serde::{de::DeserializeOwned, Serialize};
use sqlx::postgres::PgListener;
use sqlx::types::chrono::{DateTime, Utc};
use sqlx::{Pool, Postgres, Row};
use std::convert::TryInto;
use std::sync::Arc;
use std::{fmt, io};
use std::{marker::PhantomData, time::Duration};
type Timestamp = i64;
pub use sqlx::postgres::PgPool;
use crate::from_row::SqlRequest;
/// Represents a [Storage] that persists to Postgres
// #[derive(Debug)]
pub struct PostgresStorage<T> {
pool: PgPool,
job_type: PhantomData<T>,
codec: Arc<
Box<
dyn Codec<T, serde_json::Value, Error = apalis_core::error::Error>
+ Sync
+ Send
+ 'static,
>,
>,
config: Config,
controller: Controller,
ack_notify: Notify<(WorkerId, TaskId)>,
}
impl<T> Clone for PostgresStorage<T> {
fn clone(&self) -> Self {
PostgresStorage {
pool: self.pool.clone(),
job_type: PhantomData,
codec: self.codec.clone(),
config: self.config.clone(),
controller: self.controller.clone(),
ack_notify: self.ack_notify.clone(),
}
}
}
impl<T> fmt::Debug for PostgresStorage<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("PostgresStorage")
.field("pool", &self.pool)
.field("job_type", &"PhantomData<T>")
.field("controller", &self.controller)
.field("config", &self.config)
.field(
"codec",
&"Arc<Box<dyn Codec<T, serde_json::Value, Error = Error> + Sync + Send + 'static>>",
)
.field("ack_notify", &self.ack_notify)
.finish()
}
}
impl<T: Job + Serialize + DeserializeOwned + Sync + Send + Unpin + 'static> Backend<Request<T>>
for PostgresStorage<T>
{
type Stream = BackendStream<RequestStream<Request<T>>>;
type Layer = AckLayer<PostgresStorage<T>, T>;
fn common_layer(&self, worker_id: WorkerId) -> Self::Layer {
AckLayer::new(self.clone(), worker_id)
}
fn poll(mut self, worker: WorkerId) -> Poller<Self::Stream> {
let config = self.config.clone();
let controller = self.controller.clone();
let stream = self
.stream_jobs(&worker, config.poll_interval, config.buffer_size)
.map_err(|e| Error::SourceError(Box::new(e)));
let stream = BackendStream::new(stream.boxed(), controller);
let ack_notify = self.ack_notify.clone();
let pool = self.pool.clone();
let ack_heartbeat = async move {
while let Some(ids) = ack_notify
.clone()
.ready_chunks(config.buffer_size)
.next()
.await
{
let worker_ids: Vec<String> = ids.iter().map(|c| c.0.to_string()).collect();
let task_ids: Vec<String> = ids.iter().map(|c| c.1.to_string()).collect();
let query =
"UPDATE apalis.jobs SET status = 'Done', done_at = now() WHERE id = ANY($1::text[]) AND lock_by = ANY($2::text[])";
if let Err(e) = sqlx::query(query)
.bind(task_ids)
.bind(worker_ids)
.execute(&pool)
.await
{
error!("Ack failed: {e}");
}
apalis_core::sleep(config.poll_interval).await;
}
};
let heartbeat = async move {
loop {
let now: i64 = Utc::now().timestamp();
if let Err(e) = self.keep_alive_at::<Self::Layer>(&worker, now).await {
error!("Heartbeat failed: {e}")
}
apalis_core::sleep(config.keep_alive).await;
}
}
.boxed();
Poller::new(stream, async {
futures::join!(heartbeat, ack_heartbeat);
})
}
}
impl PostgresStorage<()> {
/// Get postgres migrations without running them
#[cfg(feature = "migrate")]
pub fn migrations() -> sqlx::migrate::Migrator {
sqlx::migrate!("migrations/postgres")
}
/// Do migrations for Postgres
#[cfg(feature = "migrate")]
pub async fn setup(pool: &Pool<Postgres>) -> Result<(), sqlx::Error> {
Self::migrations().run(pool).await?;
Ok(())
}
}
impl<T: Serialize + DeserializeOwned> PostgresStorage<T> {
/// New Storage from [PgPool]
pub fn new(pool: PgPool) -> Self {
Self::new_with_config(pool, Config::default())
}
/// New Storage from [PgPool] and custom config
pub fn new_with_config(pool: PgPool, config: Config) -> Self {
Self {
pool,
job_type: PhantomData,
codec: Arc::new(Box::new(JsonCodec)),
config,
controller: Controller::new(),
ack_notify: Notify::new(),
}
}
/// Expose the pool for other functionality, eg custom migrations
pub fn pool(&self) -> &Pool<Postgres> {
&self.pool
}
}
/// A listener that listens to Postgres notifications
#[derive(Debug)]
pub struct PgListen {
listener: PgListener,
subscriptions: Vec<(String, PgSubscription)>,
}
/// A postgres subscription
#[derive(Debug, Clone)]
pub struct PgSubscription {
notify: Notify<()>,
}
impl PgListen {
/// Build a new listener.
///
/// Maintaining a connection can be expensive, its encouraged you only create one [PgListen] and share it with multiple [PostgresStorage]
pub async fn new(pool: PgPool) -> Result<Self, sqlx::Error> {
let listener = PgListener::connect_with(&pool).await?;
Ok(Self {
listener,
subscriptions: Vec::new(),
})
}
/// Add a new subscription
pub fn subscribe<T: Job>(&mut self) -> PgSubscription {
let sub = PgSubscription {
notify: Notify::new(),
};
self.subscriptions.push((T::NAME.to_owned(), sub.clone()));
sub
}
/// Start listening to jobs
pub async fn listen(mut self) -> Result<(), sqlx::Error> {
self.listener.listen("apalis::job").await?;
let mut notification = self.listener.into_stream();
while let Some(Ok(res)) = notification.next().await {
let _: Vec<_> = self
.subscriptions
.iter()
.filter(|s| s.0 == res.payload())
.map(|s| s.1.notify.notify(()))
.collect();
}
Ok(())
}
}
impl<T: DeserializeOwned + Send + Unpin + Job + 'static> PostgresStorage<T> {
fn stream_jobs(
&self,
worker_id: &WorkerId,
interval: Duration,
buffer_size: usize,
) -> impl Stream<Item = Result<Option<Request<T>>, sqlx::Error>> {
let pool = self.pool.clone();
let worker_id = worker_id.clone();
let codec = self.codec.clone();
try_stream! {
loop {
// Ideally wait for a job or a tick
apalis_core::sleep(interval).await;
let tx = pool.clone();
let job_type = T::NAME;
let fetch_query = "Select * from apalis.get_jobs($1, $2, $3);";
let jobs: Vec<SqlRequest<serde_json::Value>> = sqlx::query_as(fetch_query)
.bind(worker_id.to_string())
.bind(job_type)
// https://docs.rs/sqlx/latest/sqlx/postgres/types/index.html
.bind(i32::try_from(buffer_size).map_err(|e| sqlx::Error::Io(io::Error::new(io::ErrorKind::InvalidInput, e)))?)
.fetch_all(&tx)
.await?;
for job in jobs {
yield Some(Into::into(SqlRequest {
context: job.context,
req: codec.decode(&job.req).map_err(|e| sqlx::Error::Io(io::Error::new(io::ErrorKind::InvalidData, e)))?,
}))
}
}
}
.boxed()
}
async fn keep_alive_at<Service>(
&mut self,
worker_id: &WorkerId,
last_seen: Timestamp,
) -> Result<(), sqlx::Error> {
let pool = self.pool.clone();
let last_seen = DateTime::from_timestamp(last_seen, 0).ok_or(sqlx::Error::Io(
io::Error::new(io::ErrorKind::InvalidInput, "Invalid Timestamp"),
))?;
let worker_type = T::NAME;
let storage_name = std::any::type_name::<Self>();
let query = "INSERT INTO apalis.workers (id, worker_type, storage_name, layers, last_seen)
VALUES ($1, $2, $3, $4, $5)
ON CONFLICT (id) DO
UPDATE SET last_seen = EXCLUDED.last_seen";
sqlx::query(query)
.bind(worker_id.to_string())
.bind(worker_type)
.bind(storage_name)
.bind(std::any::type_name::<Service>())
.bind(last_seen)
.execute(&pool)
.await?;
Ok(())
}
}
impl<T> Storage for PostgresStorage<T>
where
T: Job + Serialize + DeserializeOwned + Send + 'static + Unpin + Sync,
{
type Job = T;
type Error = sqlx::Error;
type Identifier = TaskId;
/// Push a job to Postgres [Storage]
///
/// # SQL Example
///
/// ```sql
/// Select apalis.push_job(job_type::text, job::json);
/// ```
async fn push(&mut self, job: Self::Job) -> Result<TaskId, sqlx::Error> {
let id = TaskId::new();
let query = "INSERT INTO apalis.jobs VALUES ($1, $2, $3, 'Pending', 0, 25, NOW() , NULL, NULL, NULL, NULL)";
let pool = self.pool.clone();
let job = self
.codec
.encode(&job)
.map_err(|e| sqlx::Error::Io(io::Error::new(io::ErrorKind::InvalidData, e)))?;
let job_type = T::NAME;
sqlx::query(query)
.bind(job)
.bind(id.to_string())
.bind(job_type.to_string())
.execute(&pool)
.await?;
Ok(id)
}
async fn schedule(&mut self, job: Self::Job, on: Timestamp) -> Result<TaskId, sqlx::Error> {
let query =
"INSERT INTO apalis.jobs VALUES ($1, $2, $3, 'Pending', 0, 25, $4, NULL, NULL, NULL, NULL)";
let pool = self.pool.clone();
let id = TaskId::new();
let on = DateTime::from_timestamp(on, 0);
let job = self
.codec
.encode(&job)
.map_err(|e| sqlx::Error::Io(io::Error::new(io::ErrorKind::InvalidInput, e)))?;
let job_type = T::NAME;
sqlx::query(query)
.bind(job)
.bind(id.to_string())
.bind(job_type)
.bind(on)
.execute(&pool)
.await?;
Ok(id)
}
async fn fetch_by_id(
&self,
job_id: &TaskId,
) -> Result<Option<Request<Self::Job>>, sqlx::Error> {
let pool = self.pool.clone();
let fetch_query = "SELECT * FROM apalis.jobs WHERE id = $1";
let res: Option<SqlRequest<serde_json::Value>> = sqlx::query_as(fetch_query)
.bind(job_id.to_string())
.fetch_optional(&pool)
.await?;
match res {
None => Ok(None),
Some(c) => Ok(Some(
SqlRequest {
context: c.context,
req: self.codec.decode(&c.req).map_err(|e| {
sqlx::Error::Io(io::Error::new(io::ErrorKind::InvalidData, e))
})?,
}
.into(),
)),
}
}
async fn len(&self) -> Result<i64, sqlx::Error> {
let pool = self.pool.clone();
let query = "Select Count(*) as count from apalis.jobs where status='Pending'";
let record = sqlx::query(query).fetch_one(&pool).await?;
record.try_get("count")
}
async fn reschedule(&mut self, job: Request<T>, wait: Duration) -> Result<(), sqlx::Error> {
let pool = self.pool.clone();
let ctx = job
.get::<SqlContext>()
.ok_or(sqlx::Error::Io(io::Error::new(
io::ErrorKind::InvalidData,
"Missing SqlContext",
)))?;
let job_id = ctx.id();
let on = Utc::now() + wait;
let mut tx = pool.acquire().await?;
let query =
"UPDATE apalis.jobs SET status = 'Pending', done_at = NULL, lock_by = NULL, lock_at = NULL, run_at = $2 WHERE id = $1";
sqlx::query(query)
.bind(job_id.to_string())
.bind(on)
.execute(&mut *tx)
.await?;
Ok(())
}
async fn update(&self, job: Request<Self::Job>) -> Result<(), sqlx::Error> {
let pool = self.pool.clone();
let ctx = job
.get::<SqlContext>()
.ok_or(sqlx::Error::Io(io::Error::new(
io::ErrorKind::InvalidData,
"Missing SqlContext",
)))?;
let job_id = ctx.id();
let status = ctx.status().to_string();
let attempts: i32 = ctx
.attempts()
.current()
.try_into()
.map_err(|e| sqlx::Error::Io(io::Error::new(io::ErrorKind::InvalidData, e)))?;
let done_at = *ctx.done_at();
let lock_by = ctx.lock_by().clone();
let lock_at = *ctx.lock_at();
let last_error = ctx.last_error().clone();
let mut tx = pool.acquire().await?;
let query =
"UPDATE apalis.jobs SET status = $1, attempts = $2, done_at = $3, lock_by = $4, lock_at = $5, last_error = $6 WHERE id = $7";
sqlx::query(query)
.bind(status.to_owned())
.bind(attempts)
.bind(done_at)
.bind(lock_by.map(|w| w.name().to_string()))
.bind(lock_at)
.bind(last_error)
.bind(job_id.to_string())
.execute(&mut *tx)
.await?;
Ok(())
}
async fn is_empty(&self) -> Result<bool, sqlx::Error> {
Ok(self.len().await? == 0)
}
async fn vacuum(&self) -> Result<usize, sqlx::Error> {
let pool = self.pool.clone();
let query = "Delete from apalis.jobs where status='Done'";
let record = sqlx::query(query).execute(&pool).await?;
Ok(record.rows_affected().try_into().unwrap_or_default())
}
}
impl<T: Sync> Ack<T> for PostgresStorage<T> {
type Acknowledger = TaskId;
type Error = sqlx::Error;
async fn ack(
&self,
worker_id: &WorkerId,
task_id: &Self::Acknowledger,
) -> Result<(), sqlx::Error> {
self.ack_notify
.notify((worker_id.clone(), task_id.clone()))
.map_err(|e| sqlx::Error::Io(io::Error::new(io::ErrorKind::Interrupted, e)))?;
Ok(())
}
}
impl<T> PostgresStorage<T> {
/// Kill a job
pub async fn kill(
&mut self,
worker_id: &WorkerId,
task_id: &TaskId,
) -> Result<(), sqlx::Error> {
let pool = self.pool.clone();
let mut tx = pool.acquire().await?;
let query =
"UPDATE apalis.jobs SET status = 'Killed', done_at = now() WHERE id = $1 AND lock_by = $2";
sqlx::query(query)
.bind(task_id.to_string())
.bind(worker_id.to_string())
.execute(&mut *tx)
.await?;
Ok(())
}
/// Puts the job instantly back into the queue
/// Another [Worker] may consume
pub async fn retry(
&mut self,
worker_id: &WorkerId,
task_id: &TaskId,
) -> Result<(), sqlx::Error> {
let pool = self.pool.clone();
let mut tx = pool.acquire().await?;
let query =
"UPDATE apalis.jobs SET status = 'Pending', done_at = NULL, lock_by = NULL WHERE id = $1 AND lock_by = $2";
sqlx::query(query)
.bind(task_id.to_string())
.bind(worker_id.to_string())
.execute(&mut *tx)
.await?;
Ok(())
}
/// Reenqueue jobs that have been abandoned by their workers
pub async fn reenqueue_orphaned(&self, count: i32) -> Result<(), sqlx::Error>
where
T: Job,
{
let job_type = T::NAME;
let mut tx = self.pool.acquire().await?;
let query = "Update apalis.jobs
SET status = 'Pending', done_at = NULL, lock_by = NULL, lock_at = NULL, last_error ='Job was abandoned'
WHERE id in
(SELECT jobs.id from apalis.jobs INNER join apalis.workers ON lock_by = workers.id
WHERE status= 'Running' AND workers.last_seen < (NOW() - INTERVAL '300 seconds')
AND workers.worker_type = $1 ORDER BY lock_at ASC LIMIT $2);";
sqlx::query(query)
.bind(job_type)
.bind(count)
.execute(&mut *tx)
.await?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use crate::context::State;
use super::*;
use email_service::Email;
use futures::StreamExt;
use sqlx::types::chrono::Utc;
/// migrate DB and return a storage instance.
async fn setup() -> PostgresStorage<Email> {
let db_url = &std::env::var("DATABASE_URL").expect("No DATABASE_URL is specified");
let pool = PgPool::connect(&db_url).await.unwrap();
// Because connections cannot be shared across async runtime
// (different runtimes are created for each test),
// we don't share the storage and tests must be run sequentially.
PostgresStorage::setup(&pool).await.unwrap();
let storage = PostgresStorage::new(pool);
storage
}
/// rollback DB changes made by tests.
/// Delete the following rows:
/// - jobs whose state is `Pending` or locked by `worker_id`
/// - worker identified by `worker_id`
///
/// You should execute this function in the end of a test
async fn cleanup(storage: PostgresStorage<Email>, worker_id: &WorkerId) {
let mut tx = storage
.pool
.acquire()
.await
.expect("failed to get connection");
sqlx::query("Delete from apalis.jobs where lock_by = $1 or status = 'Pending'")
.bind(worker_id.to_string())
.execute(&mut *tx)
.await
.expect("failed to delete jobs");
sqlx::query("Delete from apalis.workers where id = $1")
.bind(worker_id.to_string())
.execute(&mut *tx)
.await
.expect("failed to delete worker");
}
struct DummyService {}
fn example_email() -> Email {
Email {
subject: "Test Subject".to_string(),
to: "example@postgres".to_string(),
text: "Some Text".to_string(),
}
}
async fn consume_one(
storage: &mut PostgresStorage<Email>,
worker_id: &WorkerId,
) -> Request<Email> {
let mut stream = storage.stream_jobs(worker_id, std::time::Duration::from_secs(10), 1);
stream
.next()
.await
.expect("stream is empty")
.expect("failed to poll job")
.expect("no job is pending")
}
async fn register_worker_at(
storage: &mut PostgresStorage<Email>,
last_seen: Timestamp,
) -> WorkerId {
let worker_id = WorkerId::new("test-worker");
storage
.keep_alive_at::<DummyService>(&worker_id, last_seen)
.await
.expect("failed to register worker");
worker_id
}
async fn register_worker(storage: &mut PostgresStorage<Email>) -> WorkerId {
register_worker_at(storage, Utc::now().timestamp()).await
}
async fn push_email(storage: &mut PostgresStorage<Email>, email: Email) {
storage.push(email).await.expect("failed to push a job");
}
async fn get_job(storage: &mut PostgresStorage<Email>, job_id: &TaskId) -> Request<Email> {
storage
.fetch_by_id(job_id)
.await
.expect("failed to fetch job by id")
.expect("no job found by id")
}
#[tokio::test]
async fn test_consume_last_pushed_job() {
let mut storage = setup().await;
push_email(&mut storage, example_email()).await;
let worker_id = register_worker(&mut storage).await;
let job = consume_one(&mut storage, &worker_id).await;
let ctx = job.get::<SqlContext>().unwrap();
assert_eq!(*ctx.status(), State::Running);
assert_eq!(*ctx.lock_by(), Some(worker_id.clone()));
// TODO: assert!(ctx.lock_at().is_some());
cleanup(storage, &worker_id).await;
}
#[tokio::test]
async fn test_acknowledge_job() {
let mut storage = setup().await;
push_email(&mut storage, example_email()).await;
let worker_id = register_worker(&mut storage).await;
let job = consume_one(&mut storage, &worker_id).await;
let ctx = job.get::<SqlContext>().unwrap();
let job_id = ctx.id();
storage
.ack(&worker_id, job_id)
.await
.expect("failed to acknowledge the job");
let job = get_job(&mut storage, job_id).await;
let ctx = job.get::<SqlContext>().unwrap();
// TODO: Currently ack is done in the background
// assert_eq!(*ctx.status(), State::Done);
// assert!(ctx.done_at().is_some());
cleanup(storage, &worker_id).await;
}
#[tokio::test]
async fn test_kill_job() {
let mut storage = setup().await;
push_email(&mut storage, example_email()).await;
let worker_id = register_worker(&mut storage).await;
let job = consume_one(&mut storage, &worker_id).await;
let ctx = job.get::<SqlContext>().unwrap();
let job_id = ctx.id();
storage
.kill(&worker_id, job_id)
.await
.expect("failed to kill job");
let job = get_job(&mut storage, job_id).await;
let ctx = job.get::<SqlContext>().unwrap();
assert_eq!(*ctx.status(), State::Killed);
// TODO: assert!(ctx.done_at().is_some());
cleanup(storage, &worker_id).await;
}
#[tokio::test]
async fn test_heartbeat_renqueueorphaned_pulse_last_seen_6min() {
let mut storage = setup().await;
push_email(&mut storage, example_email()).await;
let six_minutes_ago = Utc::now() - Duration::from_secs(6 * 60);
let worker_id = register_worker_at(&mut storage, six_minutes_ago.timestamp()).await;
let job = consume_one(&mut storage, &worker_id).await;
storage
.reenqueue_orphaned(5)
.await
.expect("failed to heartbeat");
let ctx = job.get::<SqlContext>().unwrap();
let job_id = ctx.id();
let job = get_job(&mut storage, job_id).await;
let ctx = job.get::<SqlContext>().unwrap();
assert_eq!(*ctx.status(), State::Pending);
assert!(ctx.done_at().is_none());
assert!(ctx.lock_by().is_none());
assert!(ctx.lock_at().is_none());
assert_eq!(*ctx.last_error(), Some("Job was abandoned".to_string()));
cleanup(storage, &worker_id).await;
}
#[tokio::test]
async fn test_heartbeat_renqueueorphaned_pulse_last_seen_4min() {
let mut storage = setup().await;
push_email(&mut storage, example_email()).await;
let four_minutes_ago = Utc::now() - Duration::from_secs(4 * 60);
let worker_id = register_worker_at(&mut storage, four_minutes_ago.timestamp()).await;
let job = consume_one(&mut storage, &worker_id).await;
let ctx = job.get::<SqlContext>().unwrap();
assert_eq!(*ctx.status(), State::Running);
storage
.reenqueue_orphaned(5)
.await
.expect("failed to heartbeat");
let job_id = ctx.id();
let job = get_job(&mut storage, job_id).await;
let ctx = job.get::<SqlContext>().unwrap();
assert_eq!(*ctx.status(), State::Running);
assert_eq!(*ctx.lock_by(), Some(worker_id.clone()));
cleanup(storage, &worker_id).await;
}
}