Replaced delayed_job with resque. See README_DEVEL

This commit is contained in:
benni 2012-12-23 17:38:04 +01:00
parent 344abf5a26
commit 2c0fe08be6
18 changed files with 82 additions and 74 deletions

View file

@ -29,7 +29,6 @@ gem 'inherited_resources'
gem 'localize_input', :git => "git://github.com/bennibu/localize_input.git" gem 'localize_input', :git => "git://github.com/bennibu/localize_input.git"
gem 'wikicloth' gem 'wikicloth'
gem 'daemons' gem 'daemons'
gem 'delayed_job_active_record'
gem 'twitter-bootstrap-rails' gem 'twitter-bootstrap-rails'
gem 'simple-navigation' gem 'simple-navigation'
gem 'simple-navigation-bootstrap' gem 'simple-navigation-bootstrap'
@ -37,6 +36,7 @@ gem 'meta_search'
gem 'acts_as_versioned', git: 'git://github.com/technoweenie/acts_as_versioned.git' # Use this instead of rubygem gem 'acts_as_versioned', git: 'git://github.com/technoweenie/acts_as_versioned.git' # Use this instead of rubygem
gem 'acts_as_tree' gem 'acts_as_tree'
gem 'acts_as_configurable', git: 'git://github.com/bwalding/acts_as_configurable.git' gem 'acts_as_configurable', git: 'git://github.com/bwalding/acts_as_configurable.git'
gem 'resque'
group :production do group :production do
gem 'exception_notification', :require => 'exception_notifier' gem 'exception_notification', :require => 'exception_notifier'

View file

@ -68,11 +68,6 @@ GEM
coffee-script-source (1.3.3) coffee-script-source (1.3.3)
commonjs (0.2.6) commonjs (0.2.6)
daemons (1.1.9) daemons (1.1.9)
delayed_job (3.0.3)
activesupport (~> 3.0)
delayed_job_active_record (0.3.2)
activerecord (> 2.1.0)
delayed_job (~> 3.0.0)
erubis (2.7.0) erubis (2.7.0)
exception_notification (2.6.1) exception_notification (2.6.1)
actionmailer (>= 3.0.4) actionmailer (>= 3.0.4)
@ -131,6 +126,8 @@ GEM
rack (1.4.1) rack (1.4.1)
rack-cache (1.2) rack-cache (1.2)
rack (>= 0.4) rack (>= 0.4)
rack-protection (1.3.2)
rack
rack-ssl (1.3.2) rack-ssl (1.3.2)
rack rack
rack-test (0.6.2) rack-test (0.6.2)
@ -155,8 +152,16 @@ GEM
rake (10.0.2) rake (10.0.2)
rdoc (3.12) rdoc (3.12)
json (~> 1.4) json (~> 1.4)
redis (3.0.2)
redis-namespace (1.2.1)
redis (~> 3.0.0)
responders (0.9.3) responders (0.9.3)
railties (~> 3.1) railties (~> 3.1)
resque (1.23.0)
multi_json (~> 1.0)
redis-namespace (~> 1.0)
sinatra (>= 0.9.2)
vegas (~> 0.1.2)
ruby-prof (0.11.2) ruby-prof (0.11.2)
ruby-rc4 (0.1.5) ruby-rc4 (0.1.5)
sass (3.2.1) sass (3.2.1)
@ -171,6 +176,10 @@ GEM
simple_form (2.0.3) simple_form (2.0.3)
actionpack (~> 3.0) actionpack (~> 3.0)
activemodel (~> 3.0) activemodel (~> 3.0)
sinatra (1.3.3)
rack (~> 1.3, >= 1.3.6)
rack-protection (~> 1.2)
tilt (~> 1.3, >= 1.3.3)
sprockets (2.2.1) sprockets (2.2.1)
hike (~> 1.2) hike (~> 1.2)
multi_json (~> 1.0) multi_json (~> 1.0)
@ -195,6 +204,8 @@ GEM
uglifier (1.3.0) uglifier (1.3.0)
execjs (>= 0.3.0) execjs (>= 0.3.0)
multi_json (~> 1.0, >= 1.0.2) multi_json (~> 1.0, >= 1.0.2)
vegas (0.1.11)
rack (>= 1.0.0)
wikicloth (0.8.0) wikicloth (0.8.0)
builder builder
expression_parser expression_parser
@ -211,7 +222,6 @@ DEPENDENCIES
client_side_validations client_side_validations
coffee-rails (~> 3.2.1) coffee-rails (~> 3.2.1)
daemons daemons
delayed_job_active_record
exception_notification exception_notification
haml-rails haml-rails
inherited_resources inherited_resources
@ -223,6 +233,7 @@ DEPENDENCIES
prawn prawn
rails (= 3.2.9) rails (= 3.2.9)
rails3_acts_as_paranoid (~> 0.2.0) rails3_acts_as_paranoid (~> 0.2.0)
resque
ruby-prof ruby-prof
sass-rails (~> 3.2.3) sass-rails (~> 3.2.3)
simple-navigation simple-navigation

View file

@ -74,11 +74,23 @@ Start the WEBrick server to try it out:
(8) (optional) Get background jobs done (8) (optional) Get background jobs done
--------------------------------------- ---------------------------------------
We use for time intensive tasks a background job queue, at the moment delayed job. To deliver mails etc you have to We use for time intensive tasks a background job queue, at the moment resque with redis as key/value store.
start the workers: Install redis (in ubuntu the package redis-server works out of the box) and start the resque worker with:
rake jobs:work rake resque:work QUEUE=foodsoft_notifier
or as a daemon process in the background To have look on the current queue, failed jobs etc start the resque server with
scrip/delayed_job start # and stop resque-web
(9) (optional) View mails in browser instead in your logs
---------------------------------------------------------
We use mailcatcher in development mode to view all delivered mails in a browser interface.
Just install mailcatcher with gem install mailcatcher and start the service with
mailcatcher
From now on you have a smpt server listening on 1025. To see the emails go to
http://localhost:1080

View file

@ -5,7 +5,7 @@ class FeedbackController < ApplicationController
def create def create
if params[:message].present? if params[:message].present?
Mailer.feedback(FoodsoftConfig.scope, current_user, params[:message]).deliver Mailer.feedback(current_user, params[:message]).deliver
redirect_to root_url, :notice => "Das Feedback wurde erfolgreich verschickt. Vielen Dank!" redirect_to root_url, :notice => "Das Feedback wurde erfolgreich verschickt. Vielen Dank!"
else else
render :action => 'new' render :action => 'new'

View file

@ -10,7 +10,7 @@ class InvitesController < ApplicationController
def create def create
@invite = Invite.new(params[:invite]) @invite = Invite.new(params[:invite])
if @invite.save if @invite.save
Mailer.delay.invite(FoodsoftConfig.scope, @invite.id) Mailer.invite(@invite)
respond_to do |format| respond_to do |format|
format.html do format.html do

View file

@ -14,7 +14,7 @@ class LoginController < ApplicationController
user.reset_password_token = user.new_random_password(16) user.reset_password_token = user.new_random_password(16)
user.reset_password_expires = Time.now.advance(:days => 2) user.reset_password_expires = Time.now.advance(:days => 2)
if user.save if user.save
Mailer.delay.reset_password(FoodsoftConfig.scope, user.id) Mailer.reset_password(user)
logger.debug("Sent password reset email to #{user.email}.") logger.debug("Sent password reset email to #{user.email}.")
end end
end end

View file

@ -2,7 +2,7 @@ class MessagesController < ApplicationController
# Renders the "inbox" action. # Renders the "inbox" action.
def index def index
@messages = Message.public.page(params[:page]).per(@per_page).order('created_at DESC') @messages = Message.public.page(params[:page]).per(@per_page).order('created_at DESC').includes(:sender)
end end
# Creates a new message object. # Creates a new message object.
@ -14,7 +14,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
Message.delay.deliver(@message.id) Resque.enqueue(UserNotifier, FoodsoftConfig.scope, 'message_deliver', @message.id)
redirect_to messages_url, :notice => "Nachricht ist gespeichert und wird versendet." redirect_to messages_url, :notice => "Nachricht ist gespeichert und wird versendet."
else else
render :action => 'new' render :action => 'new'

View file

@ -10,6 +10,7 @@ class Mailer < ActionMailer::Base
# Sends an email copy of the given internal foodsoft message. # Sends an email copy of the given internal foodsoft message.
def foodsoft_message(message, recipient) def foodsoft_message(message, recipient)
set_foodcoop_scope
@message = message @message = message
mail subject: "[#{FoodsoftConfig[:name]}] " + message.subject, mail subject: "[#{FoodsoftConfig[:name]}] " + message.subject,
@ -19,9 +20,9 @@ class Mailer < ActionMailer::Base
# Sends an email with instructions on how to reset the password. # Sends an email with instructions on how to reset the password.
# Assumes user.setResetPasswordToken has been successfully called already. # Assumes user.setResetPasswordToken has been successfully called already.
def reset_password(foodcoop, user_id) def reset_password(user)
set_foodcoop_scope(foodcoop) set_foodcoop_scope
@user = User.find(user_id) @user = user
@link = new_password_url(id: @user.id, token: @user.reset_password_token) @link = new_password_url(id: @user.id, token: @user.reset_password_token)
mail :to => @user.email, mail :to => @user.email,
@ -29,9 +30,9 @@ class Mailer < ActionMailer::Base
end end
# Sends an invite email. # Sends an invite email.
def invite(foodcoop, invite_id) def invite(invite)
set_foodcoop_scope(foodcoop) set_foodcoop_scope
@invite = Invite.find(invite_id) @invite = invite
@link = accept_invitation_url(token: @invite.token) @link = accept_invitation_url(token: @invite.token)
mail :to => @invite.email, mail :to => @invite.email,
@ -40,6 +41,7 @@ class Mailer < ActionMailer::Base
# Notify user of upcoming task. # Notify user of upcoming task.
def upcoming_tasks(user, task) def upcoming_tasks(user, task)
set_foodcoop_scope
@user = user @user = user
@task = task @task = task
@ -49,6 +51,7 @@ class Mailer < ActionMailer::Base
# Sends order result for specific Ordergroup # Sends order result for specific Ordergroup
def order_result(user, group_order) def order_result(user, group_order)
set_foodcoop_scope
@order = group_order.order @order = group_order.order
@group_order = group_order @group_order = group_order
@ -58,6 +61,7 @@ class Mailer < ActionMailer::Base
# Notify user if account balance is less than zero # Notify user if account balance is less than zero
def negative_balance(user,transaction) def negative_balance(user,transaction)
set_foodcoop_scope
@group = user.ordergroup @group = user.ordergroup
@transaction = transaction @transaction = transaction
@ -65,8 +69,8 @@ class Mailer < ActionMailer::Base
:subject => "[#{FoodsoftConfig[:name]}] Gruppenkonto im Minus" :subject => "[#{FoodsoftConfig[:name]}] Gruppenkonto im Minus"
end end
def feedback(foodcoop, user, feedback) def feedback(user, feedback)
set_foodcoop_scope(foodcoop) set_foodcoop_scope
@user = user @user = user
@feedback = feedback @feedback = feedback
@ -78,6 +82,7 @@ class Mailer < ActionMailer::Base
end end
def not_enough_users_assigned(task, user) def not_enough_users_assigned(task, user)
set_foodcoop_scope
@task = task @task = task
@user = user @user = user
@ -87,7 +92,7 @@ class Mailer < ActionMailer::Base
private private
def set_foodcoop_scope(foodcoop) def set_foodcoop_scope(foodcoop = FoodsoftConfig.scope)
ActionMailer::Base.default_url_options[:foodcoop] = foodcoop ActionMailer::Base.default_url_options[:foodcoop] = foodcoop
end end

View file

@ -170,7 +170,7 @@ class Order < ActiveRecord::Base
ordergroups.each(&:update_stats!) ordergroups.each(&:update_stats!)
# Notifications # Notifications
UserNotifier.delay.finished_order(self.id) Resque.enqueue(UserNotifier, FoodsoftConfig.scope, 'finished_order', self.id)
end end
end end
end end

View file

@ -53,7 +53,7 @@ class Ordergroup < Group
save! save!
# 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
UserNotifier.delay.negative_balance(self.id, t.id) Resque.enqueue(UserNotifier, FoodsoftConfig.scope, 'negative_balance', self.id, t.id)
end end
end end
end end

View file

@ -5,8 +5,7 @@ die Bestellung für "#{@order.name}" wurde am #{I18n.l @order.ends} von #{@order
Für Euch wurden die folgenden Artikel bestellt: Für Euch wurden die folgenden Artikel bestellt:
- for group_order_article in @group_order.group_order_articles.ordered.all(:include => :order_article) - for group_order_article in @group_order.group_order_articles.ordered.all(:include => :order_article)
- article = group_order_article.order_article.article - article = group_order_article.order_article.article
= article.name #{article.name}: #{group_order_article.result} x #{article.unit} = #{group_order_article.result * article.fc_price}
\: #{group_order_article.result} x #{article.unit} = #{group_order_article.result * article.fc_price}
Gesamtpreis: #{@group_order.price} Gesamtpreis: #{@group_order.price}
Bestellung online einsehen: #{group_order_url(@group_order)} Bestellung online einsehen: #{group_order_url(@group_order)}

View file

@ -46,5 +46,5 @@
= f.input :subject, input_html: {class: 'input-xxlarge'} = f.input :subject, input_html: {class: 'input-xxlarge'}
= f.input :body, input_html: {class: 'input-xxlarge'} = f.input :body, input_html: {class: 'input-xxlarge'}
.form-actions .form-actions
= f.submit = f.submit class: 'btn btn-primary'
= link_to 'oder abbrechen', :back = link_to 'oder abbrechen', :back

View file

@ -1,7 +1,20 @@
# This plain ruby class should handle all user notifications, called by various models # This plain ruby class should handle all user notifications, called by various models
class UserNotifier class UserNotifier
@queue = :foodsoft_notifier
def self.finished_order(order_id) # Resque style method to perform every class method defined here
def self.perform(foodcoop, method_name, *args)
FoodsoftConfig.select_foodcoop(foodcoop)
self.send method_name, args
end
def self.message_deliver(args)
message_id = args.first
Message.find(message_id).deliver
end
def self.finished_order(args)
order_id = args.first
Order.find(order_id).group_orders.each do |group_order| Order.find(order_id).group_orders.each do |group_order|
group_order.ordergroup.users.each do |user| group_order.ordergroup.users.each do |user|
begin begin
@ -15,7 +28,8 @@ class UserNotifier
# If this order group's account balance is made negative by the given/last transaction, # 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. # a message is sent to all users who have enabled notification.
def self.negative_balance(ordergroup_id, transaction_id) def self.negative_balance(args)
ordergroup_id, transaction_id = args
transaction = FinancialTransaction.find transaction_id transaction = FinancialTransaction.find transaction_id
Ordergroup.find(ordergroup_id).users.each do |user| Ordergroup.find(ordergroup_id).users.each do |user|

View file

@ -37,4 +37,9 @@ Foodsoft::Application.configure do
# Configure hostname for action mailer # Configure hostname for action mailer
config.action_mailer.default_url_options = { host: 'localhost:3000' } config.action_mailer.default_url_options = { host: 'localhost:3000' }
# Mailcatcher config, start mailcatcher from console with 'mailcatcher'
# Mailcatcher can be installed by gem install mailcatcher
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = { address: "localhost", port: 1025 }
end end

View file

@ -1,3 +0,0 @@
Delayed::Worker.max_attempts = 5
Delayed::Worker.delay_jobs = !Rails.env.test?
Delayed::Worker.destroy_failed_jobs = false

View file

@ -1,22 +0,0 @@
class CreateDelayedJobs < ActiveRecord::Migration
def self.up
create_table :delayed_jobs, :force => true do |table|
table.integer :priority, :default => 0 # Allows some jobs to jump to the front of the queue
table.integer :attempts, :default => 0 # Provides for retries, but still fail eventually.
table.text :handler # YAML-encoded string of the object that will do work
table.text :last_error # reason for last failure (See Note below)
table.datetime :run_at # When to run. Could be Time.zone.now for immediately, or sometime in the future.
table.datetime :locked_at # Set when a client is working on this object
table.datetime :failed_at # Set when all retries have failed (actually, by default, the record is deleted instead)
table.string :locked_by # Who is working on this object (if locked)
table.string :queue # The name of the queue this job is in
table.timestamps
end
add_index :delayed_jobs, [:priority, :run_at], :name => 'delayed_jobs_priority'
end
def self.down
drop_table :delayed_jobs
end
end

View file

@ -78,22 +78,6 @@ ActiveRecord::Schema.define(:version => 20121216180646) do
add_index "configurable_settings", ["name"], :name => "index_configurable_settings_on_name" add_index "configurable_settings", ["name"], :name => "index_configurable_settings_on_name"
create_table "delayed_jobs", :force => true do |t|
t.integer "priority", :default => 0
t.integer "attempts", :default => 0
t.text "handler"
t.text "last_error"
t.datetime "run_at"
t.datetime "locked_at"
t.datetime "failed_at"
t.string "locked_by"
t.string "queue"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "delayed_jobs", ["priority", "run_at"], :name => "delayed_jobs_priority"
create_table "deliveries", :force => true do |t| create_table "deliveries", :force => true do |t|
t.integer "supplier_id" t.integer "supplier_id"
t.date "delivered_on" t.date "delivered_on"

3
lib/tasks/resque.rake Normal file
View file

@ -0,0 +1,3 @@
require "resque/tasks"
task "resque:setup" => :environment