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"
|
2018-07-01 07:22:44 -04:00
|
|
|
require "active_job/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
|
2020-01-18 21:35:55 -05:00
|
|
|
ruby2_keywords(:perform_now) if respond_to?(:ruby2_keywords, true)
|
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
|
|
|
|
2018-11-18 23:36:44 -05:00
|
|
|
# Performs the job immediately. The job is not sent to the queuing adapter
|
2014-09-17 15:46:53 -04:00
|
|
|
# but directly executed by blocking the execution of others until it's finished.
|
2019-12-28 13:04:38 -05:00
|
|
|
# `perform_now` returns the value of your job's `perform` method.
|
2014-08-25 10:34:50 -04:00
|
|
|
#
|
2019-12-28 13:04:38 -05:00
|
|
|
# class MyJob < ActiveJob::Base
|
|
|
|
# def perform
|
|
|
|
# "Hello World!"
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# puts MyJob.new(*args).perform_now # => "Hello World!"
|
2014-08-25 10:34:50 -04:00
|
|
|
def perform_now
|
2018-07-25 04:30:38 -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-25 10:34:50 -04:00
|
|
|
deserialize_arguments_if_needed
|
2019-12-12 21:25:03 -05:00
|
|
|
|
2020-02-25 21:25:33 -05:00
|
|
|
run_callbacks :perform do
|
|
|
|
perform(*arguments)
|
2019-12-12 21:25:03 -05: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
|