Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion integration_tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ lazy_static = "1.0.0"
dotenv = "0.11"
antidote = "1.0.0"
assert_matches = "1.0.0"
failure = { features = ["backtrace"] }
failure = { version = "*", features = ["backtrace"] }

[[test]]
name = "integration_tests"
Expand Down
28 changes: 28 additions & 0 deletions integration_tests/tests/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,31 @@ fn jobs_failing_to_load_doesnt_panic_threads() -> Fallible<()> {
runner.check_for_failed_jobs()?;
Ok(())
}

#[test]
fn jobs_with_no_environment_can_be_run_from_a_runner_with_environment() -> Fallible<()> {
use swirl::PerformError;

pub struct Environment;

#[swirl::background_job]
fn takes_db_conn(_conn: &PgConnection) -> Result<(), PerformError> {
Ok(())
}

#[swirl::background_job]
fn takes_env(_env: &Environment) -> Result<(), PerformError> {
Ok(())
}

let runner = TestGuard::builder(Environment).build();
{
let conn = runner.connection_pool().get()?;
takes_db_conn().enqueue(&conn)?;
takes_env().enqueue(&conn)?;
}

runner.run_all_pending_jobs()?;
runner.check_for_failed_jobs()?;
Ok(())
}
10 changes: 8 additions & 2 deletions swirl/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ impl<Env: 'static> Registry<Env> {
pub fn load() -> Self {
let jobs = inventory::iter::<JobVTable>
.into_iter()
.filter(|vtable| vtable.env_type == TypeId::of::<Env>())
.filter(|vtable| {
vtable.env_type == TypeId::of::<Env>() || vtable.env_type == TypeId::of::<()>()
})
.map(|&vtable| (vtable.job_type, vtable))
.collect();

Expand Down Expand Up @@ -101,6 +103,10 @@ impl<Env: 'static> PerformJob<Env> {
pool: &dyn DieselPoolObj,
) -> Result<(), PerformError> {
let perform_fn = self.vtable.perform;
perform_fn(data, env, pool)
if self.vtable.env_type == TypeId::of::<()>() {
perform_fn(data, &(), pool)
} else {
perform_fn(data, env, pool)
}
}
}
7 changes: 4 additions & 3 deletions swirl_proc_macro/src/background_job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,10 @@ impl BackgroundJob {
}

if let Some(where_clause) = sig.generics.where_clause {
return Err(where_clause.where_token.span.error(
"#[swirl::background_job] cannot be used on functions with a where clause",
));
return Err(where_clause
.where_token
.span
.error("#[swirl::background_job] cannot be used on functions with a where clause"));
}

let fn_token = sig.fn_token;
Expand Down