mosquito

Redis backed periodic and ad hoc job processing background-jobs fast redis
1.0.0.rc1 released
mosquito-cr/mosquito
219 22 13
Mosquito
mosquito

GitHub Workflow Status Crystal Version GitHub

Mosquito is a generic background job runner written specifically for Crystal. Significant inspiration from experience with the successes and failings of the Ruby gems: Resque and 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
  • Rate limited jobs

Current Limitations:

  • Job failure delay, maximum retry count, and several other variables cannot be easily configured.
  • Visibility into the job queue is difficult and must be done through redis manually.

Project State

The Mosquito project is stable. A few projects are using Mosquito in production, and it's going okay.

There are some features which would be nice to have, but what is here is both tried and tested.

If you're using Mosquito, please get in touch on the Discussion board or on Crystal chat with any questions, feature suggestions, or feedback.

Installation

Update your shard.yml to include mosquito:

dependencies:
+  mosquito:
+    github: mosquito-cr/mosquito

Further installation instructions are available for use with web frameworks as well as a vanilla crystal application:

Usage

Step 1: Define a queued job

# src/jobs/puts_job.cr
class PutsJob < Mosquito::QueuedJob
  params message : String

  def perform
    puts message
  end
end

Step 2: Trigger that job

# src/<somewher>/<somefile>.cr
PutsJob.new(message: "ohai background job").enqueue

Step 3: Run your worker to process the job

# src/worker.cr

Mosquito.configure do |settings|
  settings.redis_url = ENV["REDIS_URL"]
end

Mosquito::Runner.start
crystal run src/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.local} 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

Throttling Jobs

Jobs can be throttled to limit the number of messages that get executed within a given period of time. For example, if 10 messages were enqueued for ThrottledJob at one time; 5 would be executed immediately, then pause for a minute, then execute the last 5.

class ThrottledJob < Mosquito::QueuedJob
  params message : String
  throttle limit: 5, period: 60

  def perform
    puts message
  end
end

Connecting to Redis

Mosquito uses Redis to schedule jobs and store metadata about those jobs. Conventionally, redis connections are configured by an environment variable. To pass that configuration to Mosqito,

Mosquito.configure do |settings|
  settings.redis_url = ENV["REDIS_URL"]
end

Contributing

Contributions are welcome. Please fork the repository, commit changes on a branch, and then open a pull request.

Testing

This repository uses minitest for testing. As a result, crystal spec doesn't do anything helpful. Do this instead:

make test

In lieu of crystal spec bells and whistles, Minitest provides a nice alternative to running one test at a time instead of the whole suite.

mosquito:
  github: mosquito-cr/mosquito
  version: ~> 1.0.0.rc1
License MIT
Crystal >= 0.36.1, < 2.0.0

Authors

  • robacarp

Dependencies 2

  • habitat ~> 0.4.7
    {'github' => 'luckyframework/habitat', 'version' => '~> 0.4.7'}
  • redis ~> 2.8.0
    {'github' => 'stefanwille/crystal-redis', 'version' => '~> 2.8.0'}

Development Dependencies 2

  • minitest ~> 1.0.0
    {'github' => 'ysbaddaden/minitest.cr', 'version' => '~> 1.0.0'}
  • timecop ~> 0.4.1
    {'github' => 'crystal-community/timecop.cr', 'version' => '~> 0.4.1'}

Other repos 1

Last synced .
search fire star recently