Ver código fonte

Use ActiveJobs interface

Dominik Sander 10 anos atrás
pai
commit
b863232429

+ 15 - 0
app/jobs/agent_check_job.rb

@@ -0,0 +1,15 @@
+class AgentCheckJob < ActiveJob::Base
+  # Given an Agent id, load the Agent, call #check on it, and then save it with an updated `last_check_at` timestamp.
+  def perform(agent_id)
+    agent = Agent.find(agent_id)
+    begin
+      return if agent.unavailable?
+      agent.check
+      agent.last_check_at = Time.now
+      agent.save!
+    rescue => e
+      agent.error "Exception during check. #{e.message}: #{e.backtrace.join("\n")}"
+      raise
+    end
+  end
+end

+ 16 - 0
app/jobs/agent_receive_job.rb

@@ -0,0 +1,16 @@
+class AgentReceiveJob < ActiveJob::Base
+  # Given an Agent id and an array of Event ids, load the Agent, call #receive on it with the Event objects, and then
+  # save it with an updated `last_receive_at` timestamp.
+  def perform(agent_id, event_ids)
+    agent = Agent.find(agent_id)
+    begin
+      return if agent.unavailable?
+      agent.receive(Event.where(:id => event_ids).order(:id))
+      agent.last_receive_at = Time.now
+      agent.save!
+    rescue => e
+      agent.error "Exception during receive. #{e.message}: #{e.backtrace.join("\n")}"
+      raise
+    end
+  end
+end

+ 6 - 32
app/models/agent.rb

@@ -387,24 +387,11 @@ class Agent < ActiveRecord::Base
       end
     end
 
-    # Given an Agent id and an array of Event ids, load the Agent, call #receive on it with the Event objects, and then
-    # save it with an updated `last_receive_at` timestamp.
-    #
-    # This method is tagged with `handle_asynchronously` and will be delayed and run with delayed_job.  It accepts Agent
-    # and Event ids instead of a literal ActiveRecord models because it is preferable to serialize delayed_jobs with ids.
+    # This method will enqueue an AgentReceiveJob job. It accepts Agent and Event ids instead of a literal ActiveRecord
+    # models because it is preferable to serialize jobs with ids.
     def async_receive(agent_id, event_ids)
-      agent = Agent.find(agent_id)
-      begin
-        return if agent.unavailable?
-        agent.receive(Event.where(:id => event_ids).order(:id))
-        agent.last_receive_at = Time.now
-        agent.save!
-      rescue => e
-        agent.error "Exception during receive. #{e.message}: #{e.backtrace.join("\n")}"
-        raise
-      end
+      AgentReceiveJob.perform_later(agent_id, event_ids)
     end
-    handle_asynchronously :async_receive
 
     # Given a schedule name, run `check` via `bulk_check` on all Agents with that schedule.
     # This is called by bin/schedule.rb for each schedule in `SCHEDULES`.
@@ -425,24 +412,11 @@ class Agent < ActiveRecord::Base
       end
     end
 
-    # Given an Agent id, load the Agent, call #check on it, and then save it with an updated `last_check_at` timestamp.
-    #
-    # This method is tagged with `handle_asynchronously` and will be delayed and run with delayed_job.  It accepts an Agent
-    # id instead of a literal Agent because it is preferable to serialize delayed_jobs with ids, instead of with the full
-    # Agents.
+    # This method will enqueue an AgentCheckJob job. It accepts an Agent id instead of a literal Agent because it is
+    # preferable to serialize job with ids, instead of with the full Agents.
     def async_check(agent_id)
-      agent = Agent.find(agent_id)
-      begin
-        return if agent.unavailable?
-        agent.check
-        agent.last_check_at = Time.now
-        agent.save!
-      rescue => e
-        agent.error "Exception during check. #{e.message}: #{e.backtrace.join("\n")}"
-        raise
-      end
+      AgentCheckJob.perform_later(agent_id)
     end
-    handle_asynchronously :async_check
   end
 end
 

+ 1 - 1
app/models/agents/email_agent.rb

@@ -35,7 +35,7 @@ module Agents
       incoming_events.each do |event|
         log "Sending digest mail to #{user.email} with event #{event.id}"
         recipients(event.payload).each do |recipient|
-          SystemMailer.delay.send_message(:to => recipient, :subject => interpolated(event)['subject'], :headline => interpolated(event)['headline'], :body => interpolated(event)['body'], :groups => [present(event.payload)])
+          SystemMailer.send_message(:to => recipient, :subject => interpolated(event)['subject'], :headline => interpolated(event)['headline'], :body => interpolated(event)['body'], :groups => [present(event.payload)]).deliver_later
         end
       end
     end

+ 1 - 1
app/models/agents/email_digest_agent.rb

@@ -42,7 +42,7 @@ module Agents
         groups = self.memory['queue'].map { |payload| present(payload) }
         log "Sending digest mail to #{user.email} with events [#{ids}]"
         recipients.each do |recipient|
-          SystemMailer.delay.send_message(:to => recipient, :subject => interpolated['subject'], :headline => interpolated['headline'], :groups => groups)
+          SystemMailer.send_message(:to => recipient, :subject => interpolated['subject'], :headline => interpolated['headline'], :groups => groups).deliver_later
         end
         self.memory['queue'] = []
         self.memory['events'] = []

+ 3 - 1
config/application.rb

@@ -13,7 +13,7 @@ module Huginn
     # -- all .rb files in that directory are automatically loaded.
 
     # Custom directories with classes and modules you want to be autoloadable.
-    config.autoload_paths += %W(#{config.root}/lib #{config.root}/app/presenters)
+    config.autoload_paths += %W(#{config.root}/lib #{config.root}/app/presenters #{config.root}/app/jobs)
 
     # Activate observers that should always be running.
     # config.active_record.observers = :cacher, :garbage_collector, :forum_observer
@@ -52,5 +52,7 @@ module Huginn
 
     # Do not swallow errors in after_commit/after_rollback callbacks.
     config.active_record.raise_in_transactional_callbacks = true
+
+    config.active_job.queue_adapter = :delayed_job
   end
 end