2017-07-09 17:49:52 +00:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 13:40:03 +00:00
|
|
|
|
2016-08-06 16:40:07 +00:00
|
|
|
require "active_support/rescuable"
|
2018-07-01 11:22:44 +00:00
|
|
|
require "active_job/arguments"
|
2014-05-20 22:26:19 +00:00
|
|
|
|
|
|
|
module ActiveJob
|
2014-05-22 18:37:06 +00:00
|
|
|
module Execution
|
2014-05-22 18:35:02 +00:00
|
|
|
extend ActiveSupport::Concern
|
2014-08-25 14:34:50 +00:00
|
|
|
include ActiveSupport::Rescuable
|
2014-05-30 23:19:30 +00:00
|
|
|
|
2014-11-04 03:23:36 +00:00
|
|
|
# Includes methods for executing and performing jobs instantly.
|
2014-08-25 14:34:50 +00:00
|
|
|
module ClassMethods
|
|
|
|
# Performs the job immediately.
|
|
|
|
#
|
|
|
|
# MyJob.perform_now("mike")
|
|
|
|
#
|
2021-03-19 15:53:06 +00:00
|
|
|
def perform_now(...)
|
|
|
|
job_or_instantiate(...).perform_now
|
2014-08-25 14:34:50 +00:00
|
|
|
end
|
2014-05-22 18:35:02 +00:00
|
|
|
|
2021-07-29 21:18:07 +00:00
|
|
|
def execute(job_data) # :nodoc:
|
2016-02-26 05:47:01 +00:00
|
|
|
ActiveJob::Callbacks.run_callbacks(:execute) do
|
|
|
|
job = deserialize(job_data)
|
|
|
|
job.perform_now
|
|
|
|
end
|
2014-08-25 14:34:50 +00:00
|
|
|
end
|
|
|
|
end
|
2014-05-22 17:33:23 +00:00
|
|
|
|
2018-11-19 04:36:44 +00:00
|
|
|
# Performs the job immediately. The job is not sent to the queuing adapter
|
2014-09-17 19:46:53 +00:00
|
|
|
# but directly executed by blocking the execution of others until it's finished.
|
2021-02-14 17:15:07 +00:00
|
|
|
# +perform_now+ returns the value of your job's +perform+ method.
|
2014-08-25 14:34:50 +00:00
|
|
|
#
|
2019-12-28 18:04:38 +00:00
|
|
|
# class MyJob < ActiveJob::Base
|
|
|
|
# def perform
|
|
|
|
# "Hello World!"
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# puts MyJob.new(*args).perform_now # => "Hello World!"
|
2014-08-25 14:34:50 +00:00
|
|
|
def perform_now
|
2018-07-25 08:30:38 +00: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 14:34:50 +00:00
|
|
|
deserialize_arguments_if_needed
|
2019-12-13 02:25:03 +00:00
|
|
|
|
2021-02-02 01:06:49 +00:00
|
|
|
_perform_job
|
2021-01-22 21:07:53 +00:00
|
|
|
rescue Exception => exception
|
2016-05-14 00:43:48 +00:00
|
|
|
rescue_with_handler(exception) || raise
|
2014-05-20 22:26:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def perform(*)
|
2014-08-15 10:11:58 +00:00
|
|
|
fail NotImplementedError
|
2014-05-20 22:26:19 +00:00
|
|
|
end
|
2021-02-02 01:06:49 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
def _perform_job
|
|
|
|
run_callbacks :perform do
|
|
|
|
perform(*arguments)
|
|
|
|
end
|
|
|
|
end
|
2014-05-20 22:26:19 +00:00
|
|
|
end
|
|
|
|
end
|