Use ActiveJob instead of Resque directly
This allows us to directly pass entities to the job.
This commit is contained in:
parent
07c8393c8c
commit
47d9c79617
16 changed files with 76 additions and 73 deletions
2
Procfile
2
Procfile
|
@ -1,4 +1,4 @@
|
||||||
web: bundle exec rails server --binding=0.0.0.0 --port=$PORT
|
web: bundle exec rails server --binding=0.0.0.0 --port=$PORT
|
||||||
worker: QUEUE=foodsoft_notifier bundle exec rake resque:work
|
worker: QUEUE=* bundle exec rake resque:work
|
||||||
mail: bundle exec rake foodsoft:reply_email_smtp_server
|
mail: bundle exec rake foodsoft:reply_email_smtp_server
|
||||||
cron: supercronic crontab
|
cron: supercronic crontab
|
||||||
|
|
|
@ -137,7 +137,7 @@ class OrdersController < ApplicationController
|
||||||
|
|
||||||
flash[:notice] = (s ? I18n.t('orders.receive.notice', :msg => s) : I18n.t('orders.receive.notice_none'))
|
flash[:notice] = (s ? I18n.t('orders.receive.notice', :msg => s) : I18n.t('orders.receive.notice_none'))
|
||||||
end
|
end
|
||||||
Resque.enqueue(UserNotifier, FoodsoftConfig.scope, 'received_order', @order.id)
|
NotifyReceivedOrderJob.perform_later(@order)
|
||||||
if current_user.role_orders? || current_user.role_finance?
|
if current_user.role_orders? || current_user.role_finance?
|
||||||
redirect_to @order
|
redirect_to @order
|
||||||
elsif current_user.role_pickups?
|
elsif current_user.role_pickups?
|
||||||
|
|
2
app/jobs/application_job.rb
Normal file
2
app/jobs/application_job.rb
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
class ApplicationJob < ActiveJob::Base
|
||||||
|
end
|
15
app/jobs/notify_finished_order_job.rb
Normal file
15
app/jobs/notify_finished_order_job.rb
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
class NotifyFinishedOrderJob < ApplicationJob
|
||||||
|
def perform(order)
|
||||||
|
order.group_orders.each do |group_order|
|
||||||
|
next unless group_order.ordergroup
|
||||||
|
|
||||||
|
group_order.ordergroup.users.each do |user|
|
||||||
|
next unless user.settings.notify['order_finished']
|
||||||
|
|
||||||
|
Mailer.deliver_now_with_user_locale user do
|
||||||
|
Mailer.order_result(user, group_order)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
11
app/jobs/notify_negative_balance_job.rb
Normal file
11
app/jobs/notify_negative_balance_job.rb
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
class NotifyNegativeBalanceJob < ApplicationJob
|
||||||
|
def perform(ordergroup, transaction)
|
||||||
|
ordergroup.users.each do |user|
|
||||||
|
next unless user.settings.notify['negative_balance']
|
||||||
|
|
||||||
|
Mailer.deliver_now_with_user_locale user do
|
||||||
|
Mailer.negative_balance(user, transaction)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
15
app/jobs/notify_received_order_job.rb
Normal file
15
app/jobs/notify_received_order_job.rb
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
class NotifyReceivedOrderJob < ApplicationJob
|
||||||
|
def perform(order)
|
||||||
|
order.group_orders.each do |group_order|
|
||||||
|
next unless group_order.ordergroup
|
||||||
|
|
||||||
|
group_order.ordergroup.users.each do |user|
|
||||||
|
next unless user.settings.notify['order_received']
|
||||||
|
|
||||||
|
Mailer.deliver_now_with_user_locale user do
|
||||||
|
Mailer.order_received(user, group_order)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -260,7 +260,7 @@ class Order < ApplicationRecord
|
||||||
ordergroups.each(&:update_stats!)
|
ordergroups.each(&:update_stats!)
|
||||||
|
|
||||||
# Notifications
|
# Notifications
|
||||||
Resque.enqueue(UserNotifier, FoodsoftConfig.scope, 'finished_order', self.id)
|
NotifyFinishedOrderJob.perform_later(self)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -89,7 +89,7 @@ class Ordergroup < Group
|
||||||
update_balance!
|
update_balance!
|
||||||
# Notify only when order group had a positive balance before the last transaction:
|
# Notify only when order group had a positive balance before the last transaction:
|
||||||
if t.amount < 0 && self.account_balance < 0 && self.account_balance - t.amount >= 0
|
if t.amount < 0 && self.account_balance < 0 && self.account_balance - t.amount >= 0
|
||||||
Resque.enqueue(UserNotifier, FoodsoftConfig.scope, 'negative_balance', self.id, t.id)
|
NotifyNegativeBalanceJob.perform_later(self, t)
|
||||||
end
|
end
|
||||||
t
|
t
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,55 +0,0 @@
|
||||||
# This plain ruby class should handle all user notifications, called by various models
|
|
||||||
class UserNotifier
|
|
||||||
@queue = :foodsoft_notifier
|
|
||||||
|
|
||||||
# Resque style method to perform every class method defined here
|
|
||||||
def self.perform(foodcoop, method_name, *args)
|
|
||||||
FoodsoftConfig.select_foodcoop(foodcoop) if FoodsoftConfig[:multi_coop_install]
|
|
||||||
self.send method_name, args
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.finished_order(args)
|
|
||||||
order_id = args.first
|
|
||||||
Order.find(order_id).group_orders.each do |group_order|
|
|
||||||
next if group_order.ordergroup.nil?
|
|
||||||
|
|
||||||
group_order.ordergroup.users.each do |user|
|
|
||||||
if user.settings.notify['order_finished']
|
|
||||||
Mailer.deliver_now_with_user_locale user do
|
|
||||||
Mailer.order_result(user, group_order)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.received_order(args)
|
|
||||||
order_id = args.first
|
|
||||||
Order.find(order_id).group_orders.each do |group_order|
|
|
||||||
next if group_order.ordergroup.nil?
|
|
||||||
|
|
||||||
group_order.ordergroup.users.each do |user|
|
|
||||||
next unless user.settings.notify['order_received']
|
|
||||||
|
|
||||||
Mailer.deliver_now_with_user_locale user do
|
|
||||||
Mailer.order_received(user, group_order)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# If this order group's account balance is made negative by the given/last transaction,
|
|
||||||
# a message is sent to all users who have enabled notification.
|
|
||||||
def self.negative_balance(args)
|
|
||||||
ordergroup_id, transaction_id = args
|
|
||||||
transaction = FinancialTransaction.find transaction_id
|
|
||||||
|
|
||||||
Ordergroup.find(ordergroup_id).users.each do |user|
|
|
||||||
if user.settings.notify['negative_balance']
|
|
||||||
Mailer.deliver_now_with_user_locale user do
|
|
||||||
Mailer.negative_balance(user, transaction)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -47,6 +47,9 @@ module Foodsoft
|
||||||
# TODO Disable this. Uncommenting this line will currently cause rspec to fail.
|
# TODO Disable this. Uncommenting this line will currently cause rspec to fail.
|
||||||
config.action_controller.permit_all_parameters = true
|
config.action_controller.permit_all_parameters = true
|
||||||
|
|
||||||
|
config.active_job.queue_adapter = :resque
|
||||||
|
config.active_job.queue_name_prefix = ENV.fetch('JOB_QUEUE_PREFIX', "foodsoft_#{Rails.env}")
|
||||||
|
|
||||||
# Enable the asset pipeline
|
# Enable the asset pipeline
|
||||||
config.assets.enabled = true
|
config.assets.enabled = true
|
||||||
|
|
||||||
|
|
|
@ -60,10 +60,6 @@ Rails.application.configure do
|
||||||
# Use a different cache store in production.
|
# Use a different cache store in production.
|
||||||
# config.cache_store = :mem_cache_store
|
# config.cache_store = :mem_cache_store
|
||||||
|
|
||||||
# Use a real queuing backend for Active Job (and separate queues per environment)
|
|
||||||
config.active_job.queue_adapter = :resque
|
|
||||||
config.active_job.queue_name_prefix = "foodsoft_#{Rails.env}"
|
|
||||||
|
|
||||||
config.action_mailer.perform_caching = false
|
config.action_mailer.perform_caching = false
|
||||||
|
|
||||||
# Ignore bad email addresses and do not raise email delivery errors.
|
# Ignore bad email addresses and do not raise email delivery errors.
|
||||||
|
|
22
config/initializers/active_job_select_foodcoop.rb
Normal file
22
config/initializers/active_job_select_foodcoop.rb
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
module FoodsoftActiveJobArguments
|
||||||
|
def self.included(base) # :nodoc:
|
||||||
|
base.class_eval do
|
||||||
|
alias_method :orig_deserialize, :deserialize
|
||||||
|
alias_method :orig_serialize, :serialize
|
||||||
|
|
||||||
|
def serialize(arguments)
|
||||||
|
ret = orig_serialize(arguments)
|
||||||
|
ret.unshift FoodsoftConfig.scope
|
||||||
|
end
|
||||||
|
|
||||||
|
def deserialize(arguments)
|
||||||
|
FoodsoftConfig.select_multifoodcoop arguments.shift
|
||||||
|
orig_deserialize(arguments)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
ActiveSupport.on_load(:after_initialize) do
|
||||||
|
ActiveJob::Arguments.include FoodsoftActiveJobArguments
|
||||||
|
end
|
|
@ -18,7 +18,6 @@ services:
|
||||||
environment:
|
environment:
|
||||||
- DATABASE_URL=mysql2://root:secret@mariadb/development?encoding=utf8mb4
|
- DATABASE_URL=mysql2://root:secret@mariadb/development?encoding=utf8mb4
|
||||||
- REDIS_URL=redis://redis:6379
|
- REDIS_URL=redis://redis:6379
|
||||||
- QUEUE=foodsoft_notifier
|
|
||||||
- TEST_DATABASE_URL=mysql2://root:secret@mariadb/test?encoding=utf8mb4
|
- TEST_DATABASE_URL=mysql2://root:secret@mariadb/test?encoding=utf8mb4
|
||||||
- DATABASE_CLEANER_ALLOW_REMOTE_DATABASE_URL=true
|
- DATABASE_CLEANER_ALLOW_REMOTE_DATABASE_URL=true
|
||||||
- MAILCATCHER_ADDRESS=mailcatcher
|
- MAILCATCHER_ADDRESS=mailcatcher
|
||||||
|
|
|
@ -32,7 +32,7 @@ class MessagesController < ApplicationController
|
||||||
def create
|
def create
|
||||||
@message = @current_user.send_messages.new(params[:message])
|
@message = @current_user.send_messages.new(params[:message])
|
||||||
if @message.save
|
if @message.save
|
||||||
Resque.enqueue(MessageNotifier, FoodsoftConfig.scope, 'message_deliver', @message.id)
|
DeliverMessageJob.perform_later(@message)
|
||||||
redirect_to messages_url, :notice => I18n.t('messages.create.notice')
|
redirect_to messages_url, :notice => I18n.t('messages.create.notice')
|
||||||
else
|
else
|
||||||
render :action => 'new'
|
render :action => 'new'
|
||||||
|
|
|
@ -1,10 +1,5 @@
|
||||||
class MessageNotifier < UserNotifier
|
class DeliverMessageJob < ApplicationJob
|
||||||
@queue = :foodsoft_notifier
|
def perform(message)
|
||||||
|
|
||||||
def self.message_deliver(args)
|
|
||||||
message_id = args.first
|
|
||||||
message = Message.find(message_id)
|
|
||||||
|
|
||||||
message.message_recipients.each do |message_recipient|
|
message.message_recipients.each do |message_recipient|
|
||||||
recipient = message_recipient.user
|
recipient = message_recipient.user
|
||||||
if recipient.receive_email?
|
if recipient.receive_email?
|
|
@ -52,7 +52,7 @@ class MessagesMailReceiver
|
||||||
message.add_recipients [@message.sender_id]
|
message.add_recipients [@message.sender_id]
|
||||||
|
|
||||||
message.save!
|
message.save!
|
||||||
Resque.enqueue(MessageNotifier, FoodsoftConfig.scope, "message_deliver", message.id)
|
DeliverMessageJob.perform_later(message)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
Loading…
Reference in a new issue