Background Jobs
This crate provides tooling required to run some processes asynchronously from a usually synchronous application. The standard example of this is Web Services, where certain things need to be processed, but processing them while a user is waiting for their browser to respond might not be the best experience.
Usage
Add Background Jobs to your project
[]
= "2.0.0"
= "0.9.0"
= "1.0"
= "0.3"
= { = "1.0", = ["derive"] }
To get started with Background Jobs, first you should define a job.
Jobs are a combination of the data required to perform an operation, and the logic of that
operation. They implment the Job
, serde::Serialize
, and serde::DeserializeOwned
.
use Job;
use Error;
use ;
The run method for a job takes an additional argument, which is the state the job expects to use. The state for all jobs defined in an application must be the same. By default, the state is an empty tuple, but it's likely you'll want to pass in some Actix address, or something else.
Let's re-define the job to care about some application state.
Running jobs
By default, this crate ships with the background-jobs-actix
feature enabled. This uses the
background-jobs-actix
crate to spin up a Server and Workers, and provides a mechanism for
spawning new jobs.
background-jobs-actix
on it's own doesn't have a mechanism for storing worker state. This
can be implemented manually by implementing the Storage
trait from background-jobs-core
,
or the provided in-memory store can be used.
With that out of the way, back to the examples:
Main
use ;
use Error;
async
Complete Example
For the complete example project, see the examples folder
Bringing your own server/worker implementation
If you want to create your own jobs processor based on this idea, you can depend on the
background-jobs-core
crate, which provides the Job trait, as well as some
other useful types for implementing a jobs processor and job store.
Contributing
Feel free to open issues for anything you find an issue with. Please note that any contributed code will be licensed under the GPLv3.
License
This work is licensed under the Cooperative Software License. This is not a Free Software License, but may be considered a "source-available License." For most hobbyists, self-employed developers, worker-owned companies, and cooperatives, this software can be used in most projects so long as this software is distributed under the terms of the CSL. For more information, see the provided LICENSE file. If none exists, the license can be found online here. If you are a free software project and wish to use this software under the terms of the GNU Affero General Public License, please contact me at asonix@asonix.dog and we can sort that out. If you wish to use this project under any other license, especially in proprietary software, the answer is likely no.