2017-07-09 13:49:52 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:40:03 -04:00
|
|
|
|
2016-08-06 12:40:07 -04:00
|
|
|
require "active_support/rescuable"
|
2017-06-30 01:17:57 -04:00
|
|
|
require_relative "arguments"
|
2014-05-20 18:26:19 -04:00
|
|
|
|
|
|
|
module ActiveJob
|
2014-05-22 14:37:06 -04:00
|
|
|
module Execution
|
2014-05-22 14:35:02 -04:00
|
|
|
extend ActiveSupport::Concern
|
2014-08-25 10:34:50 -04:00
|
|
|
include ActiveSupport::Rescuable
|
2014-05-30 19:19:30 -04:00
|
|
|
|
2014-11-03 22:23:36 -05:00
|
|
|
# Includes methods for executing and performing jobs instantly.
|
2014-08-25 10:34:50 -04:00
|
|
|
module ClassMethods
|
|
|
|
# Performs the job immediately.
|
|
|
|
#
|
|
|
|
# MyJob.perform_now("mike")
|
|
|
|
#
|
|
|
|
def perform_now(*args)
|
|
|
|
job_or_instantiate(*args).perform_now
|
|
|
|
end
|
2014-05-22 14:35:02 -04:00
|
|
|
|
2014-08-25 10:34:50 -04:00
|
|
|
def execute(job_data) #:nodoc:
|
2016-02-26 00:47:01 -05:00
|
|
|
ActiveJob::Callbacks.run_callbacks(:execute) do
|
|
|
|
job = deserialize(job_data)
|
|
|
|
job.perform_now
|
|
|
|
end
|
2014-08-25 10:34:50 -04:00
|
|
|
end
|
|
|
|
end
|
2014-05-22 13:33:23 -04:00
|
|
|
|
2014-08-25 10:34:50 -04:00
|
|
|
# Performs the job immediately. The job is not sent to the queueing adapter
|
2014-09-17 15:46:53 -04:00
|
|
|
# but directly executed by blocking the execution of others until it's finished.
|
2014-08-25 10:34:50 -04:00
|
|
|
#
|
|
|
|
# MyJob.new(*args).perform_now
|
|
|
|
def perform_now
|
|
|
|
deserialize_arguments_if_needed
|
2014-05-22 13:33:23 -04:00
|
|
|
run_callbacks :perform do
|
2016-08-03 17:30:39 -04:00
|
|
|
# Guard against jobs that were persisted before we started counting executions by zeroing out nil counters
|
|
|
|
self.executions = (executions || 0) + 1
|
|
|
|
|
2014-08-15 06:11:58 -04:00
|
|
|
perform(*arguments)
|
2014-05-22 13:33:23 -04:00
|
|
|
end
|
2014-05-22 14:35:02 -04:00
|
|
|
rescue => exception
|
2016-05-13 20:43:48 -04:00
|
|
|
rescue_with_handler(exception) || raise
|
2014-05-20 18:26:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def perform(*)
|
2014-08-15 06:11:58 -04:00
|
|
|
fail NotImplementedError
|
2014-05-20 18:26:19 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|