View bus_subscriptions.rb
# every minute
subscribe "every_minute", 'bus_event_type' => 'heartbeat_minutes' do |attributes|
InvoiceChargeWorker.process_all!
end
# every hour: 4:22, 5:22, 6:22, etc
subscribe "once_an_hour", 'bus_event_type' => 'heartbeat_minutes', 'minute' => 22 do |attributes|
InvoiceChargeWorker.process_all!
end
View Rakefile
namespace :resque do
task :setup => [:environment] do
require 'resque_scheduler'
require 'resque/scheduler'
require 'tresque'
QueueBus.heartbeat!
end
end
View heartbeat.rb
# for Tue, 11 Apr 2017 00:25:00 UTC +00:00
# epoch time: 1491870300
QueueBus.publish(heartbeat_seconds", {
"epoch_seconds"=>1491870300,
"epoch_minutes"=>24864505,
"epoch_hours"=>414408,
"epoch_days"=>17267,
"minute"=>25,
"hour"=>0,
View invoice_charge_worker.rb
class InvoiceChargeWorker
include TResque::Worker
inputs :invoice_id
worker_lock :invoice_id
def self.process_all!
Invoice.where(stat: 'pending').find_each do |invoice|
self.enqueue(invoice_id: invoice.id)
end
View invoice_charge_batch_worker.rb
class InvoiceChargeBatchWorker
include TResque::Worker
worker_lock :all
queue_lock :all
def work
Invoice.where(stat: 'pending').find_each do |invoice|
InvoiceChargeWorker.enqueue(invoice_id: invoice.id)
end
View invoice_charge_worker.rb
class InvoiceChargeWorker
include TResque::Worker
inputs :invoice_id
worker_lock :invoice_id
def work
return unless needed?
invoice.charge!
end
View Rakefile
require 'resque/tasks'
require 'resque_scheduler/tasks'
require "resque_bus/tasks"
namespace :resque do
task :setup => [:environment] do
require 'resque_scheduler'
require 'resque/scheduler'
require 'tresque'
end
View tresque.rb
require 'tresque'
module Account
class RegularWorker
include ::TResque::Worker
# defaults to account_default queue
end
end
module Account
View bus_subscriptions.rb
subscribe "user_changed" do |attributes|
UserIndexWorker.enqueue(user_id: attributes['id'])
end
View invoice_charge_worker.rb
class InvoiceChargeWorker
include TResque::Worker
inputs :invoice_id
worker_lock :to_id
def work
return unless needed?
invoice.charge!
end