From 848c526d09b2f3bcc7bf86ec2bff28cf4ffbc9ad Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Wed, 5 Feb 2014 17:14:57 +0100 Subject: [PATCH] Moved background jobs to own files. --- app/models/application_model.rb | 9 +-- .../background_job_search_index.rb | 11 ++++ .../ticket/article/communicate_email.rb | 63 +------------------ .../communicate_email/background_job.rb | 60 ++++++++++++++++++ 4 files changed, 74 insertions(+), 69 deletions(-) create mode 100644 app/models/application_model/background_job_search_index.rb create mode 100644 app/models/observer/ticket/article/communicate_email/background_job.rb diff --git a/app/models/application_model.rb b/app/models/application_model.rb index 25575376d..a48ae48e9 100644 --- a/app/models/application_model.rb +++ b/app/models/application_model.rb @@ -494,7 +494,7 @@ update search index, if configured - will be executed automatically # start background job to transfer data to search index return if !SearchIndexBackend.enabled? - Delayed::Job.enqueue( ApplicationModel::Job.new( self.class.to_s, self.id ) ) + Delayed::Job.enqueue( ApplicationModel::BackgroundJobSearchIndex.new( self.class.to_s, self.id ) ) end =begin @@ -851,11 +851,4 @@ destory object dependencies, will be executed automatically def destroy_dependencies end - # perform background job - class ApplicationModel::Job < Struct.new( :object, :o_id ) - def perform - Object.const_get(object).find(o_id).search_index_update_backend - end - end - end diff --git a/app/models/application_model/background_job_search_index.rb b/app/models/application_model/background_job_search_index.rb new file mode 100644 index 000000000..2242fad25 --- /dev/null +++ b/app/models/application_model/background_job_search_index.rb @@ -0,0 +1,11 @@ +# perform background job +class ApplicationModel::BackgroundJobSearchIndex + def initialize(object, o_id) + @object = object + @o_id = o_id + end + + def perform + Object.const_get(@object).find(@o_id).search_index_update_backend + end +end diff --git a/app/models/observer/ticket/article/communicate_email.rb b/app/models/observer/ticket/article/communicate_email.rb index 94ee39aa5..2d5d8e323 100644 --- a/app/models/observer/ticket/article/communicate_email.rb +++ b/app/models/observer/ticket/article/communicate_email.rb @@ -18,65 +18,6 @@ class Observer::Ticket::Article::CommunicateEmail < ActiveRecord::Observer return if type['name'] != 'email' # send background job - Delayed::Job.enqueue( Observer::Ticket::Article::CommunicateEmail::Send.new( record.id ) ) + Delayed::Job.enqueue( Observer::Ticket::Article::CommunicateEmail::BackgroundJob.new( record.id ) ) end - - class Send < Struct.new( :id ) - def perform - record = Ticket::Article.find( id ) - - # build subject - ticket = Ticket.lookup( :id => record.ticket_id ) - subject = ticket.subject_build( record.subject ) - - # send email - a = Channel::IMAP.new - message = a.send( - { - :message_id => record.message_id, - :in_reply_to => record.in_reply_to, - :from => record.from, - :to => record.to, - :cc => record.cc, - :subject => subject, - :body => record.body, - :attachments => record.attachments - } - ) - - # store mail plain - Store.add( - :object => 'Ticket::Article::Mail', - :o_id => record.id, - :data => message.to_s, - :filename => "ticket-#{ticket.number}-#{record.id}.eml", - :preferences => {}, - :created_by_id => record.created_by_id, - ) - - # add history record - recipient_list = '' - [:to, :cc].each { |key| - if record[key] && record[key] != '' - if recipient_list != '' - recipient_list += ',' - end - recipient_list += record[key] - end - } - if recipient_list != '' - History.add( - :o_id => record.id, - :history_type => 'email', - :history_object => 'Ticket::Article', - :related_o_id => ticket.id, - :related_history_object => 'Ticket', - :value_from => record.subject, - :value_to => recipient_list, - :created_by_id => record.created_by_id, - ) - end - end - end - -end +end \ No newline at end of file diff --git a/app/models/observer/ticket/article/communicate_email/background_job.rb b/app/models/observer/ticket/article/communicate_email/background_job.rb new file mode 100644 index 000000000..bca380c6e --- /dev/null +++ b/app/models/observer/ticket/article/communicate_email/background_job.rb @@ -0,0 +1,60 @@ +class Observer::Ticket::Article::CommunicateEmail::BackgroundJob + def initialize(id) + @article_id = id + end + def perform + record = Ticket::Article.find( @article_id ) + + # build subject + ticket = Ticket.lookup( :id => record.ticket_id ) + subject = ticket.subject_build( record.subject ) + + # send email + a = Channel::IMAP.new + message = a.send( + { + :message_id => record.message_id, + :in_reply_to => record.in_reply_to, + :from => record.from, + :to => record.to, + :cc => record.cc, + :subject => subject, + :body => record.body, + :attachments => record.attachments + } + ) + + # store mail plain + Store.add( + :object => 'Ticket::Article::Mail', + :o_id => record.id, + :data => message.to_s, + :filename => "ticket-#{ticket.number}-#{record.id}.eml", + :preferences => {}, + :created_by_id => record.created_by_id, + ) + + # add history record + recipient_list = '' + [:to, :cc].each { |key| + if record[key] && record[key] != '' + if recipient_list != '' + recipient_list += ',' + end + recipient_list += record[key] + end + } + if recipient_list != '' + History.add( + :o_id => record.id, + :history_type => 'email', + :history_object => 'Ticket::Article', + :related_o_id => ticket.id, + :related_history_object => 'Ticket', + :value_from => record.subject, + :value_to => recipient_list, + :created_by_id => record.created_by_id, + ) + end + end +end