Sidekiq.rs (aka rusty-sidekiq
)
This is a reimplementation of sidekiq in rust. It is compatible with sidekiq.rb for both submitting and processing jobs. Sidekiq.rb is obviously much more mature than this repo, but I hope you enjoy using it. This library is built using tokio so it is async by default.
The Worker
This library uses serde to make worker arguments strongly typed as needed. Below is an example of a worker with strongly typed arguments. It also has custom options that will be used whenever a job is submitted. These can be overridden at enqueue time making it easy to change the queue name, for example, should you need to.
Creating a Job
There are several ways to insert a job, but for this example, we'll keep it simple. Given some worker, insert using strongly typed arguments.
.await?;
perform_async
You can make custom overrides at enqueue time.
.queue
.perform_async
.await?;
opts
Or you can have more control by using the crate level method.
.await?;
perform_async
See more examples in examples/demo.rs
.
Starting the Server
Below is an example of how you should create a Processor
, register workers, include any
custom middlewares, and start the server.
// Redis
let manager = new.unwrap;
let mut redis = builder.build.await.unwrap;
// Sidekiq server
let mut p = new;
// Add known workers
p.register;
// Custom Middlewares
p.using
.await;
// Start the server
p.run.await;
Periodic Jobs
Periodic cron jobs are supported out of the box. All you need to specify is a valid cron string and a worker instance. You can optionally supply arguments, a queue, a retry flag, and a name that will be logged when a worker is submitted.
Example:
// Clear out all periodic jobs and their schedules
destroy_all.await?;
// Add a new periodic job
builder?
.name
.queue
.args?
.register
.await?;
Periodic jobs are not removed automatically. If your project adds a periodic job and
then later removes the periodic::builder
call, the periodic job will still exist in
redis. You can call periodic::destroy_all(redis).await?
at the start of your program
to ensure only the periodic jobs added by the latest version of your program will be
executed.
The implementation relies on a sorted set in redis. It stores a json payload of the
periodic job with a score equal to the next scheduled UTC time of the cron string. All
processes will periodically poll for changes and atomically update the score to the new
next scheduled UTC time for the cron string. The worker that successfully changes the
score atomically will enqueue a new job. Processes that don't successfully update the
score will move on. This implementation detail means periodic jobs never leave redis.
Another detail is that json when decoded and then encoded might not produce the same
value as the original string. Ex: {"a":"b","c":"d"}
might become {"c":"d","a":b"}
.
To keep the json representation consistent, when updating a periodic job with its new
score in redis, the original json string will be used again to keep things consistent.
Server Middleware
One great feature of sidekiq is its middleware pattern. This library reimplements the
sidekiq server middleware pattern in rust. In the example below supposes you have an
app that performs work only for paying customers. The middleware below will hault jobs
from being executed if the customers have expired. One thing kind of interesting about
the implementation is that we can rely on serde to conditionally type-check workers.
For example, suppose I only care about user-centric workers, and I identify those by their
user_guid
as a parameter. With serde it's easy to validate your paramters.
Customization Details
Namespacing the workers
It's still very common to use the redis-namespace
gem with ruby sidekiq workers. This library
supports namespacing redis commands by using a connection customizer when you build the connection
pool.
let manager = new?;
let redis = builder
.connection_customizer
.build
.await?;
Now all commands used by this library will be prefixed with my_cool_app:
, example: ZDEL my_cool_app:scheduled {...}
.
Passing database connections into the workers
Workers will often need access to other software components like database connections, http clients,
etc. You can define these on your worker struct so long as they implement Clone
. Example:
async
Customizing the worker name for workers under a nested ruby module
You mind find that your worker under a module does not match with a ruby worker under a module.
A nested rusty-sidekiq worker workers::MyWorker
will only keep the final type name MyWorker
when
registering the worker for some "class name". Meaning, if a ruby worker is enqueued with the class
Workers::MyWorker
, the workers::MyWorker
type will not process that work. This is because by default
the class name is generated at compile time based on the worker struct name. To override this, redefine one
of the default trait methods:
;
And now when ruby enqueues a Workers::MyWorker
job, it will be picked up by rust-sidekiq.
License
MIT