Add the schedule message function to Hutch.
See hutch-schedule-demo how to integration with rails.
Add this line to your application's Gemfile:
gem 'hutch-schedule'
And then execute:
$ bundle
Or install it yourself as:
$ gem install hutch-schedule
Use the code below to initialize the Hutch::Schedule
Hutch::Schedule.connect
They will do something below:
- Declear an topic exchange called
<hutch>.schedule
just for routing message to delay_queue<5s>. - Declear an queue named
<hutch>_delay_queue_<5s>
and with some params:
- Set
x-dead-letter-exchange: <hutch>
: let queue republish message to default exchange. - Set
x-message-ttl: <30.days>
: to avoid the queue is to large, because there is no consumer with this queue.
- If ActiveJob is loaded. it will use
ActiveJob::Base.descendants
to register all ActiveJob class to one-job-per-consumer to Hutch::Consumer
Let consumer to include Hutch::Enqueue
then it has the ability of publishing message to RabbitMQ with the consume '<routing_key>'
- enqueue: just publish one message
- enqueue_in: publish one message and delay seconds
- enqueue_at: publish one message and auto calculate the seconds need to delay
According to the RabbitMQ TTL Message design limits (discus), We design the fixed delay level from seconds to hours, below is the details:
- seconds(4): 5s, 10s, 20s, 30s
- minutes(14): 1m, 2m, 3m, 4m, 5m, 6m, 7m, 8m, 9m, 10m, 20m, 30m, 40m, 50m
- hours(3): 1h, 2h, 3h
RabbitMQ is not fit for storage lot`s of delay message so if you want delay an message beyand 3 hours so you need to storage it into database or some place.
If you want use error retry, then:
- Add
Hutch::ErrorHandlers::MaxRetry
toHutch::Config.error_handlers
like below
Hutch::Config.error_handlers << Hutch::ErrorHandlers::MaxRetry.new
- Let
Hutch::Consumer
to includeHutch::Enqueue
and setupattempts
class PlanConsumer
include Hutch::Consumer
include Hutch::Enqueue
attempts 3
consume 'abc.plan'
end
Error retry will use ActiveJob exponentially_longer
algorithm (executes**4) + 2
seconds
Add an hutch.rb
to conf/initializers
:
# reuse Hutch config.yaml file
Hutch::Config.load_from_file(Rails.root.join('config', 'config.yaml'))
# replace error_handlers with Hutch::ErrorHandlers::MaxRetry
Hutch::Config.error_handlers = [Hutch::ErrorHandlers::MaxRetry.new]
# Init Hutch
Hutch.connect
# Init Hutch::Schedule
Hutch::Schedule.connect
Then you can enqueue message in Rails console like below:
PlanConsumer.enqueue(a: 1)
# or schedule message
PlanConsumer.enqueue_in(5.seconds, a: 1)
class EmailJob < ApplicationJob
queue_as :email
retry_on StandardError, wait: :exponentially_longer
def perform(user_id)
user = User.find(user_id)
user.send_email
end
end
# in rails console, you can
EmailJob.perform_later(user.id)
# or
EmailJob.set(wait: 5.seconds).perform_later(user.id)
Bug reports and pull requests are welcome on GitHub at https://github.com/wppurking/hutch-schedule. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
The gem is available as open source under the terms of the MIT License.
Use the repo: https://github.com/wppurking/hutch-schedule-demo
- simplify usage
- add rate limit to hutch, per queue with dynamic key. process level and global level.
- add cron job support
- add unique job support