-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract Job::Schedulable concern from Job::Executable
- Loading branch information
Showing
2 changed files
with
46 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# frozen_string_literal: true | ||
|
||
module SolidQueue | ||
class Job | ||
module Schedulable | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
has_one :scheduled_execution, dependent: :destroy | ||
|
||
scope :scheduled, -> { where.not(finished_at: nil) } | ||
end | ||
|
||
class_methods do | ||
def schedule_all(jobs) | ||
schedule_all_at_once(jobs) | ||
successfully_scheduled(jobs) | ||
end | ||
|
||
private | ||
def schedule_all_at_once(jobs) | ||
ScheduledExecution.create_all_from_jobs(jobs) | ||
end | ||
|
||
def successfully_scheduled(jobs) | ||
where(id: ScheduledExecution.where(job_id: jobs.map(&:id)).pluck(:job_id)) | ||
end | ||
end | ||
|
||
def scheduled? | ||
scheduled_execution.present? | ||
end | ||
|
||
def due? | ||
scheduled_at.nil? || scheduled_at <= Time.current | ||
end | ||
|
||
private | ||
def schedule | ||
ScheduledExecution.create_or_find_by!(job_id: id) | ||
end | ||
end | ||
end | ||
end |