mosquito
mosquito
Mosquito is a generic background job runner written specifically for Crystal. Significant inspiration from my experience with the successes and failings of the Ruby gem Sidekiq.
Mosquito currently provides these features:
- Delayed execution
- Scheduled / Periodic execution
- Job Storage in Redis
- Crystal hash style
?
methods for parameter getters which return nil instead of raise - Automatic rescheduling of failed jobs
- Progressively increasing delay of failed jobs
- Dead letter queue of jobs which have failed too many times
Current Limitations:
- Job failure delay, maximum retry count, and several other variables cannot be easily configured.
- Redis functions not all atomic. There is the potential for multiple instances or fibers running background jobs to interfere with each other, resulting in duplicate task executions. Missed executions are unlikely.
- Visibility into the job queue is difficult and must be done through redis manually.
Project State
Updated 2018-06-08
Sufficient working beta. No functionality is tested, but it all seems to be working in manual tests. Use in a production environment at your own risk, and please open issues and feature requests.
I'm using it in a production environment.
Installation
Add this to your application's shard.yml
:
dependencies:
+ mosquito:
+ github: robacarp/mosquito
Further installation instructions are available for use with Amber as well as a vanilla crystal application:
Usage
Step 1: Define a queued job
class PutsJob < Mosquito::QueuedJob
params(message : String | Nil)
def perform
puts message
end
end
Step 2: Trigger that job
PutsJob.new(message: "ohai background job").enqueue
Step 3: Run your worker to process the job
crystal run bin/worker.cr
Success
> crystal run src/worker.cr
2017-11-06 17:07:29 - Mosquito is buzzing...
2017-11-06 17:07:51 - Running task puts_job<...> from puts_job
2017-11-06 17:07:51 - [PutsJob] ohai background job
2017-11-06 17:07:51 - task puts_job<...> succeeded, took 0.0 seconds
More information about queued jobs in the wiki.
Periodic Jobs
Periodic jobs run according to a predefined period.
This periodic job:
class PeriodicallyPutsJob < Mosquito::PeriodicJob
run_every 1.minute
def perform
emotions = %w{happy sad angry optimistic political skeptical epuhoric}
puts "The time is now #{Time.now} and the wizard is feeling #{emotions.sample}"
end
end
Would produce this output:
2017-11-06 17:20:13 - Mosquito is buzzing...
2017-11-06 17:20:13 - Queues: periodically_puts_job
2017-11-06 17:20:13 - Running task periodically_puts_job<...> from periodically_puts_job
2017-11-06 17:20:13 - [PeriodicallyPutsJob] The time is now 2017-11-06 17:20:13 and the wizard is feeling skeptical
2017-11-06 17:20:13 - task periodically_puts_job<...> succeeded, took 0.0 seconds
2017-11-06 17:21:14 - Queues: periodically_puts_job
2017-11-06 17:21:14 - Running task periodically_puts_job<...> from periodically_puts_job
2017-11-06 17:21:14 - [PeriodicallyPutsJob] The time is now 2017-11-06 17:21:14 and the wizard is feeling optimistic
2017-11-06 17:21:14 - task periodically_puts_job<...> succeeded, took 0.0 seconds
2017-11-06 17:22:15 - Queues: periodically_puts_job
2017-11-06 17:22:15 - Running task periodically_puts_job<...> from periodically_puts_job
2017-11-06 17:22:15 - [PeriodicallyPutsJob] The time is now 2017-11-06 17:22:15 and the wizard is feeling political
2017-11-06 17:22:15 - task periodically_puts_job<...> succeeded, took 0.0 seconds
More information: periodic jobs on the wiki
Connecting to Redis
Mosquito currently reads directly from the REDIS_URL
environment variable to connect to redis. If no variable is set, it uses redis connection defaults to connect to redis on localhost.
Contributing
Contributions are welcome. Please fork the repository, commit changes on a branch, and then open a pull request.
Contributors
- robacarp robacarp - creator, maintainer