2016-08-06 12:40:07 -04:00
|
|
|
require "sucker_punch"
|
2014-05-18 15:20:06 -04:00
|
|
|
|
|
|
|
module ActiveJob
|
|
|
|
module QueueAdapters
|
2014-09-21 16:20:23 -04:00
|
|
|
# == Sucker Punch adapter for Active Job
|
|
|
|
#
|
|
|
|
# Sucker Punch is a single-process Ruby asynchronous processing library.
|
2016-06-09 17:17:55 -04:00
|
|
|
# This reduces the cost of hosting on a service like Heroku along
|
2016-01-27 16:07:46 -05:00
|
|
|
# with the memory footprint of having to maintain additional jobs if
|
|
|
|
# hosting on a dedicated server. All queues can run within a
|
|
|
|
# single application (eg. Rails, Sinatra, etc.) process.
|
2014-09-21 16:20:23 -04:00
|
|
|
#
|
|
|
|
# Read more about Sucker Punch {here}[https://github.com/brandonhilkert/sucker_punch].
|
|
|
|
#
|
|
|
|
# To use Sucker Punch set the queue_adapter config to +:sucker_punch+.
|
|
|
|
#
|
|
|
|
# Rails.application.config.active_job.queue_adapter = :sucker_punch
|
2014-05-18 15:20:06 -04:00
|
|
|
class SuckerPunchAdapter
|
2015-03-11 17:57:13 -04:00
|
|
|
def enqueue(job) #:nodoc:
|
2016-01-27 11:29:18 -05:00
|
|
|
if JobWrapper.respond_to?(:perform_async)
|
|
|
|
# sucker_punch 2.0 API
|
|
|
|
JobWrapper.perform_async job.serialize
|
|
|
|
else
|
|
|
|
# sucker_punch 1.0 API
|
|
|
|
JobWrapper.new.async.perform job.serialize
|
|
|
|
end
|
2015-03-11 17:57:13 -04:00
|
|
|
end
|
2014-05-19 18:27:28 -04:00
|
|
|
|
2015-03-11 17:57:13 -04:00
|
|
|
def enqueue_at(job, timestamp) #:nodoc:
|
2016-01-27 11:29:18 -05:00
|
|
|
if JobWrapper.respond_to?(:perform_in)
|
|
|
|
delay = timestamp - Time.current.to_f
|
|
|
|
JobWrapper.perform_in delay, job.serialize
|
|
|
|
else
|
2016-08-06 12:40:07 -04:00
|
|
|
raise NotImplementedError, "sucker_punch 1.0 does not support `enqueued_at`. Please upgrade to version ~> 2.0.0 to enable this behavior."
|
2016-01-27 11:29:18 -05:00
|
|
|
end
|
2014-05-19 06:34:27 -04:00
|
|
|
end
|
|
|
|
|
2014-09-17 15:46:53 -04:00
|
|
|
class JobWrapper #:nodoc:
|
2014-05-19 06:34:27 -04:00
|
|
|
include SuckerPunch::Job
|
|
|
|
|
2014-08-25 10:34:50 -04:00
|
|
|
def perform(job_data)
|
|
|
|
Base.execute job_data
|
2014-05-18 15:20:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|