Skip to content

Commit

Permalink
Add option to use time crate instead of chrono (#185)
Browse files Browse the repository at this point in the history
* Add option to use `time` crate instead of `chrono`

This is useful for users who are already working with `sqlx` and `time`,
bc `sqlx` doesn't allow you to use `chrono` and `time` together.

* leaves the default as `chrono` but allows users to opt-in to using
  `time` instead
* adds a `time` feature to `apalis-core`, `apalis-sql` and
  `apalis-redis`
* enabling both `cron` and `time` features now triggers a compile error
  because `apalis-cron` uses `cron` and `cron` depends on `chrono`
* adds `time` to the CI matrix
* (subjective) use `datetime - duration` instead of
  `datetime.sub(duration)`

* [fix] include CI changes

* [fix] use correct timestamping method in `apalis-redis`

* fix tests

* rustfmt
  • Loading branch information
utterstep authored Nov 9, 2023
1 parent ca5e4e1 commit 7a4afee
Show file tree
Hide file tree
Showing 22 changed files with 369 additions and 149 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ jobs:
- uses: actions-rs/cargo@v1
with:
command: check
args: --features async-std-comp --no-default-features
args: --features async-std-comp,chrono --no-default-features
- uses: actions-rs/cargo@v1
with:
command: check
args: --features async-std-comp,time --no-default-features


test:
Expand Down
5 changes: 3 additions & 2 deletions .github/workflows/mysql.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ jobs:
profile: minimal
toolchain: stable
override: true
- run: cargo test --no-default-features --features mysql,migrate,tokio-comp -- --test-threads=1
- run: cargo test --no-default-features --features mysql,migrate,tokio-comp,chrono -- --test-threads=1
working-directory: packages/apalis-sql

- run: cargo test --no-default-features --features mysql,migrate,tokio-comp,time -- --test-threads=1
working-directory: packages/apalis-sql
4 changes: 3 additions & 1 deletion .github/workflows/postgres.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,7 @@ jobs:
profile: minimal
toolchain: stable
override: true
- run: cargo test --no-default-features --features postgres,migrate,tokio-comp -- --test-threads=1
- run: cargo test --no-default-features --features postgres,migrate,tokio-comp,chrono -- --test-threads=1
working-directory: packages/apalis-sql
- run: cargo test --no-default-features --features postgres,migrate,tokio-comp,time -- --test-threads=1
working-directory: packages/apalis-sql
8 changes: 8 additions & 0 deletions .github/workflows/redis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,11 @@ jobs:
working-directory: packages/apalis-redis
env:
REDIS_URL: redis://127.0.0.1/
- run: cargo test --features tokio-comp,time --no-default-features -- --test-threads=1
working-directory: packages/apalis-redis
env:
REDIS_URL: redis://127.0.0.1/
- run: cargo test --features async-std-comp,time --no-default-features -- --test-threads=1
working-directory: packages/apalis-redis
env:
REDIS_URL: redis://127.0.0.1/
4 changes: 3 additions & 1 deletion .github/workflows/sqlite.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@ jobs:
profile: minimal
toolchain: stable
override: true
- run: cargo test --no-default-features --features sqlite,migrate,tokio-comp -- --test-threads=1
- run: cargo test --no-default-features --features sqlite,migrate,tokio-comp,chrono -- --test-threads=1
working-directory: packages/apalis-sql
- run: cargo test --no-default-features --features sqlite,migrate,tokio-comp,time -- --test-threads=1
working-directory: packages/apalis-sql
7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ edition = "2021"
bench = false

[features]
default = ["tracing", "tokio-comp"]
default = ["tracing", "tokio-comp", "chrono"]
## Support Tracing 👀
tracing = ["apalis-core/trace"]

Expand All @@ -30,6 +30,11 @@ mysql = ["apalis-sql/mysql"]
## Include Cron functionality
cron = ["apalis-cron"]

## Use chrono library for time representation
chrono = ["apalis-core/chrono", "apalis-sql/chrono", "apalis-redis/chrono"]
## Use time library for time representation
time = ["apalis-core/time", "apalis-sql/time", "apalis-redis/time"]

## Support for Sentry exception and performance monitoring
sentry = ["apalis-core/sentry"]
## Support Prometheus metrics
Expand Down
9 changes: 7 additions & 2 deletions packages/apalis-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ thiserror = "1.0.50"
log = "0.4"
http = { version = "0.2.9", optional = true }
strum = { version = "0.25", features = ["derive"] }
chrono = { version = "0.4", default-features = false, features = [
chrono = { version = "0.4", default-features = false, optional = true, features = [
"clock",
"serde",
] }
time = { version = "0.3", optional = true, features = [
"serde"
] }
tracing-futures = { version = "0.2.5", optional = true, default-features = false }
sentry-core = { version = "0.31.7", optional = true, default-features = false }
metrics = { version = "0.21", optional = true, default-features = false }
Expand All @@ -44,7 +47,9 @@ version = "0.2"
optional = true

[features]
default = ["tokio-comp", "extensions"]
default = ["tokio-comp", "extensions", "chrono"]
chrono = ["dep:chrono"]
time = ["dep:time"]
storage = ["extensions"]
mq = ["extensions"]
expose = []
Expand Down
28 changes: 15 additions & 13 deletions packages/apalis-core/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::{job::JobId, request::JobState, worker::WorkerId};

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

use crate::{job::JobId, request::JobState, worker::WorkerId, Timestamp};

#[cfg(feature = "extensions")]
use crate::error::JobError;
#[cfg(feature = "extensions")]
Expand All @@ -16,13 +15,13 @@ use std::{any::Any, marker::Send};
pub struct JobContext {
pub(crate) id: JobId,
pub(crate) status: JobState,
pub(crate) run_at: DateTime<Utc>,
pub(crate) run_at: Timestamp,
pub(crate) attempts: i32,
pub(crate) max_attempts: i32,
pub(crate) last_error: Option<String>,
pub(crate) lock_at: Option<DateTime<Utc>>,
pub(crate) lock_at: Option<Timestamp>,
pub(crate) lock_by: Option<WorkerId>,
pub(crate) done_at: Option<DateTime<Utc>>,
pub(crate) done_at: Option<Timestamp>,
#[cfg(feature = "extensions")]
#[serde(skip)]
pub(crate) data: Data,
Expand Down Expand Up @@ -52,7 +51,10 @@ impl JobContext {
JobContext {
id,
status: JobState::Pending,
run_at: Utc::now(),
#[cfg(feature = "chrono")]
run_at: chrono::Utc::now(),
#[cfg(feature = "time")]
run_at: time::OffsetDateTime::now_utc(),
lock_at: None,
done_at: None,
attempts: 0,
Expand Down Expand Up @@ -157,32 +159,32 @@ impl JobContext {
}

/// Get the time a job was done
pub fn done_at(&self) -> &Option<DateTime<Utc>> {
pub fn done_at(&self) -> &Option<Timestamp> {
&self.done_at
}

/// Set the time a job was done
pub fn set_done_at(&mut self, done_at: Option<DateTime<Utc>>) {
pub fn set_done_at(&mut self, done_at: Option<Timestamp>) {
self.done_at = done_at;
}

/// Get the time a job is supposed to start
pub fn run_at(&self) -> &DateTime<Utc> {
pub fn run_at(&self) -> &Timestamp {
&self.run_at
}

/// Set the time a job should run
pub fn set_run_at(&mut self, run_at: DateTime<Utc>) {
pub fn set_run_at(&mut self, run_at: Timestamp) {
self.run_at = run_at;
}

/// Get the time a job was locked
pub fn lock_at(&self) -> &Option<DateTime<Utc>> {
pub fn lock_at(&self) -> &Option<Timestamp> {
&self.lock_at
}

/// Set the lock_at value
pub fn set_lock_at(&mut self, lock_at: Option<DateTime<Utc>>) {
pub fn set_lock_at(&mut self, lock_at: Option<Timestamp>) {
self.lock_at = lock_at;
}

Expand Down
9 changes: 5 additions & 4 deletions packages/apalis-core/src/expose/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ use crate::error::JobError;
use crate::request::JobRequest;
use crate::request::JobState;
use crate::worker::WorkerId;
use chrono::DateTime;
use chrono::Utc;
use serde::Deserialize;
use serde::Serialize;

use crate::Timestamp;

/// A serializable version of a worker.
#[derive(Debug, Serialize, Deserialize)]
pub struct ExposedWorker {
Expand All @@ -21,12 +22,12 @@ pub struct ExposedWorker {
/// The layers that were loaded for worker. uses [std::any::type_name]
layers: String,
/// The last time the worker was seen. Some sources use keep alive.
last_seen: DateTime<Utc>,
last_seen: Timestamp,
}

impl ExposedWorker {
/// Build a worker representation for serialization
pub fn new<S, T>(worker_id: WorkerId, layers: String, last_seen: DateTime<Utc>) -> Self {
pub fn new<S, T>(worker_id: WorkerId, layers: String, last_seen: Timestamp) -> Self {
ExposedWorker {
worker_id,
job_type: std::any::type_name::<T>().to_string(),
Expand Down
10 changes: 10 additions & 0 deletions packages/apalis-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,13 @@ pub mod mock {
)
}
}

#[cfg(feature = "chrono")]
use chrono::{DateTime, Utc};
#[cfg(feature = "time")]
use time::OffsetDateTime;

#[cfg(feature = "chrono")]
type Timestamp = DateTime<Utc>;
#[cfg(feature = "time")]
type Timestamp = OffsetDateTime;
5 changes: 2 additions & 3 deletions packages/apalis-core/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ pub mod builder;
mod error;
use std::time::Duration;

use chrono::{DateTime, Utc};

use crate::{
job::{Job, JobId, JobStreamResult},
layers::ack::{Ack, AckError},
request::JobRequest,
worker::WorkerId,
Timestamp,
};

#[cfg(feature = "storage")]
Expand All @@ -31,7 +30,7 @@ pub trait Storage: Clone {
async fn push(&mut self, job: Self::Output) -> StorageResult<JobId>;

/// Push a job into the scheduled set
async fn schedule(&mut self, job: Self::Output, on: DateTime<Utc>) -> StorageResult<JobId>;
async fn schedule(&mut self, job: Self::Output, on: Timestamp) -> StorageResult<JobId>;

/// Return the number of pending jobs from the queue
async fn len(&self) -> StorageResult<i64>;
Expand Down
1 change: 1 addition & 0 deletions packages/apalis-cron/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ serde = { version = "1.0", features = ["derive"] }
default = ["tokio-comp"]
async-std-comp = ["async-std", "apalis-core/async-std-comp"]
tokio-comp = ["tokio", "tokio/net", "apalis-core/tokio-comp"]
time = []

[package.metadata.docs.rs]
# defines the configuration attribute `docsrs`
Expand Down
3 changes: 3 additions & 0 deletions packages/apalis-cron/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@
//! }
//! ```
#[cfg(feature = "time")]
compile_error!("`apalis-cron` does not support `time` feature. Please use `chrono` instead.");

use apalis_core::job::Job;
use apalis_core::utils::Timer;
use apalis_core::{error::JobError, request::JobRequest};
Expand Down
7 changes: 5 additions & 2 deletions packages/apalis-redis/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ redis = { version = "0.23", default-features = false, features = [
] }
serde = "1"
log = "0.4"
chrono = { version = "0.4", default-features = false, features = [
chrono = { version = "0.4", default-features = false, optional = true, features = [
"clock",
"serde",
] }
time = { version = "0.3", optional = true, features = ["serde"] }
serde_json = "1"
async-stream = "0.3"
futures = "0.3"
Expand All @@ -38,7 +39,7 @@ tokio = { version = "1", features = ["macros"] }
email-service = { path = "../../examples/email-service" }

[features]
default = ["tokio-comp"]
default = ["tokio-comp", "chrono"]
async-std-comp = [
"async-std",
"redis/async-std-comp",
Expand All @@ -51,3 +52,5 @@ tokio-comp = [
"apalis-core/tokio-comp",
]
expose = ["apalis-core/expose"]
chrono = ["apalis-core/chrono", "dep:chrono"]
time = ["apalis-core/time", "dep:time"]
5 changes: 5 additions & 0 deletions packages/apalis-redis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@
mod storage;
pub use storage::RedisStorage;

#[cfg(feature = "chrono")]
type Timestamp = chrono::DateTime<chrono::Utc>;
#[cfg(feature = "time")]
type Timestamp = time::OffsetDateTime;
Loading

0 comments on commit 7a4afee

Please sign in to comment.