Job Controllers
In Sword, a job controller is a struct that groups scheduled tasks. Each of its methods is a job that the scheduler runs on a schedule you declare.
Job controllers are built on top of tokio-cron-scheduler and integrate with the framework's module system and dependency injection.
Enabling the feature
Job controllers are enabled with the jobs feature:
[dependencies]
sword = { version = "0.6", features = ["jobs"] }Enabling this feature gives you the sword::jobs::* import as a prelude, which exposes the cron, repeated, and once attributes together with Uuid and JobScheduler.
Defining a job controller
A job controller is a struct annotated with #[controller(kind = Controller::JobHandler)]. Unlike the other controller types, it takes no routing data (path, namespace, or service):
use std::time::Duration;
use sword::jobs::*;
use sword::prelude::*;
#[controller(kind = Controller::JobHandler)]
pub struct CleanupJobs;
impl CleanupJobs {
#[repeated(Duration::from_secs(60))]
async fn cleanup(&self, _: Uuid, _: JobScheduler) {
tracing::info!("Running periodic cleanup");
}
}Handler signature
Every job handler must be async and receive exactly three parameters:
async fn job_name(&self, uuid: Uuid, scheduler: JobScheduler)&self: the controller instance, so a job can use injected dependencies.uuid: the unique id of the running job.scheduler: the runningJobScheduler, useful to inspect the job or query its next tick.
The attribute macro validates this signature at compile time and reports a clear error when it does not match.
Dependency injection
As with any other controller, a job controller can receive dependencies in its fields. The container builds it the same way:
use std::sync::Arc;
use sword::jobs::*;
use sword::prelude::*;
#[injectable]
pub struct EmailService;
#[controller(kind = Controller::JobHandler)]
pub struct NotificationJobs {
email: Arc<EmailService>,
}
impl NotificationJobs {
#[cron("0 0 9 * * *")]
async fn send_digest(&self, _: Uuid, _: JobScheduler) {
self.email.send_digest().await;
}
}Registration
Like any other controller, a job controller is registered from a module with register_controllers:
impl Module for NotificationModule {
fn register_controllers(controllers: &ControllerRegistry) {
controllers.register::<NotificationJobs>();
}
}Only the job controllers registered in a module are built and scheduled. See Scheduling for the available schedules and how the scheduler runs.

