In a controller, I have a post request by an authenticated user firing an ActiveJob.
RunReportsJob.perform_later(param1, param2, param3)
It works without errors. But if the user presses the button (firing post request) again to trigger RunReportsJob while it was already running, 2 jobs happen at the same time. I setup delayed_job because I thought it was a queueing service, and would keep everything at one job at a time. But this isn't happening. Below is my configuration:
# app/jobs/run_reports_job.rb:
class RunReportsJob < ActiveJob::Base
queue_as :RunReports
include ShopifyApp::Controller
# --------------------------------------------------------------
# JOB ERROR MANAGEMENT
# --------------------------------------------------------------
rescue_from(ActiveRecord::RecordNotFound) do |exception|
# Do something with the exception
logger.debug "Hit rescue_from"
end
# --------------------------------------------------------------
# JOB FUNCTION
# --------------------------------------------------------------
def perform(param1, param2, param3)
# some code is performed here
end
end
# config/application.rb
:
config.active_job.queue_adapter = :delayed_job
# config/initializers/delayed_job_config.rb
:
Delayed::Worker.max_attempts = 1
Delayed::Worker.max_run_time = 12.hours
You'll have to add some logic before RunReportsJob.perform_later(param1, param2, param3)
to check for an already queued job priority and increase the priority number (lower number is higher priority according to documentation) in this example the queue:
highest_priority = Delayed::Job.where(queue: :RunReports).maximum(:priority)
Delayed::Worker.default_priority = highest_priority + 1 if highest_priority
RunReportsJob.perform_later(param1, param2, param3)
DelayedJob
uses a table to save all the jobs info check the documentation at https://github.com/collectiveidea/delayed_job#gory-details