Moved message delivering into background process.

This commit is contained in:
benni 2012-08-24 14:33:45 +02:00
parent af3875d46b
commit edadb7d8d1
3 changed files with 6 additions and 19 deletions

View file

@ -11,15 +11,6 @@ class ApplicationController < ActionController::Base
def self.current
Thread.current[:application_controller]
end
# Use this method to call a rake task,,
# e.g. to deliver mails after there are created.
def call_rake(task, options = {})
options[:rails_env] ||= Foodsoft.env
args = options.map { |n, v| "#{n.to_s.upcase}='#{v}'" }
system "/usr/bin/rake #{task} #{args.join(' ')} --trace 2>&1 >> #{Rails.root}/log/rake.log &"
end
protected

View file

@ -14,7 +14,7 @@ class MessagesController < ApplicationController
def create
@message = @current_user.send_messages.new(params[:message])
if @message.save
@message.deliver #TODO: Put this into a background job
Message.delay.deliver(@message.id)
redirect_to messages_url, :notice => "Nachricht ist gespeichert und wird versendet."
else
render :action => 'new'

View file

@ -21,6 +21,10 @@ class Message < ActiveRecord::Base
before_validation :clean_up_recipient_ids, :on => :create
def self.deliver(message_id)
find(message_id).deliver
end
def clean_up_recipient_ids
self.recipients_ids = recipients_ids.uniq.reject { |id| id.blank? } unless recipients_ids.nil?
self.recipients_ids = User.all.collect(&:id) if sent_to_all == "1"
@ -73,20 +77,12 @@ class Message < ActiveRecord::Base
begin
Mailer.foodsoft_message(self, user).deliver
rescue
logger.warn "Deliver failed for #{user.nick}: #{user.email}"
Rails.logger.warn "Deliver failed for #{user.nick}: #{user.email}"
end
end
end
update_attribute(:email_state, 1)
end
# Sends all pending messages that are to be send as emails.
def self.send_emails
messages = Message.pending
for message in messages
message.deliver
end
end
end