-
Notifications
You must be signed in to change notification settings - Fork 5
Home
Turbocharged.Beanstalk is a .NET client for Beanstalk, an outrageously simple job queue.
Just about everything in Turbocharged.Beanstalk returns a Task<T> and is meant to play quite nicely with C#'s async/await keywords.
Producers are meant for inserting jobs, consumers are meant for reserving jobs.
An IProducer can be shared among many callers. Producer calls do not block each other.
var producer = BeanstalkConnection.ConnectProducerAsync(hostname, port);
await producer.UseAsync("my-tube");
await producer.PutAsync(jobData, priority, timeToRun: TimeSpan.FromSeconds(30));An IConsumer can be shared, but be aware that a waiting call to ReserveAsync will block future calls on the connection until:
- A job is reserved
- The server responds with DEADLINE_SOON (ReserveAsync returns null)
- The reserve command times out (
ReserveAsyncthrows aTimeoutException)
For this reason, I suggest building a loop something like this:
var consumer = BeanstalkConnection.ConnectConsumerAsync(hostname, port);
Job job;
while ((job = await consumer.ReserveAsync()) != null)
{
// Process the job
await consumer.DeleteAsync(job.Id);
}The most common (I think) use for the IConsumer interface is to reserve jobs in a loop, process them, then delete them. So, ConnectWorkerAsync is meant to help with this workflow.
var worker = BeanstalkConnection.ConnectWorkerAsync(hostname, port, options, async (worker, job) =>
{
// ...do some processing...
await worker.DeleteAsync(job.Id);
});
// ...some time later...
worker.Dispose();A worker maintains a dedicated TCP connection which reserves jobs in a loop. A worker is created with a delgate with signature Func<IWorker, Job, Task>. As soon a job is reserved, the delegate is called. The delegate is responsible for calling IWorker.DeleteAsync() to delete the job. You dispose the worker to stop the connection.