Class | MailSenderWorker |
In: |
lib/workers/mail_sender_worker.rb
|
Parent: | BackgrounDRb::Worker::RailsBase |
Put your code that runs your task inside the do_work method it will be run automatically in a thread. You have access to all of your rails models. You also get logger and results method inside of this class by default.
HTTP_REQUEST_HOST | = | "localhost" |
HTTP_REQUEST_PORT | = | 3000 |
メール履歴から取り出し配送に回す。
# File lib/workers/mail_sender_worker.rb, line 44 44: def deliver(mail_history_id) 45: if mail_history = MailHistory.find(mail_history_id) 46: return mail_history.deliver 47: end 48: end
Worker としての既定のエントリポイント usage:
MiddleMan.new_worker(:class => :mail_sender_worker, :args => {:post => [queue.id, path]})
or
MiddleMan.new_worker(:class => :mail_sender_worker, :args => {:deliver => [mail_history_1.id, mail_history_2.id]})
# File lib/workers/mail_sender_worker.rb, line 14 14: def do_work(args) 15: # This method is called in it's own new thread when you 16: # call new worker. args is set to :args 17: if args.key?(:post) 18: unless post(*args[:post]) 19: logger.error("ERROR: MailSenderWorker: no mail delivered") 20: end 21: return 22: elsif args.key?(:deliver) 23: args[:deliver].each do |mail_history_id| 24: deliver(mail_history_id) 25: end 26: end 27: end
メールキューから取り出し配送に回す。
# File lib/workers/mail_sender_worker.rb, line 30 30: def post(mail_queue_id, path) 31: queue = MailQueue.find(mail_queue_id, :include => [ :product, :from ]) 32: if queue.processed? 33: logger.warn { "WARN: MailQueue #{queue.id} was already processed." } 34: return false 35: end 36: require "net/http" 37: http = Net::HTTP.new(HTTP_REQUEST_HOST, HTTP_REQUEST_PORT) 38: req = Net::HTTP::Get.new(path) 39: res = http.request(req) 40: return res 41: end