-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merged in CHOUETTE-3189-replace-cron-aka-whenever- (pull request #1499)
CHOUETTE-3189 Replace cron (aka whenever) by DelayedJob Cron Approved-by: Luc Donnet
- Loading branch information
Showing
30 changed files
with
463 additions
and
312 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
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,89 @@ | ||
# frozen_string_literal: true | ||
|
||
module Cron | ||
class BaseJob | ||
class_attribute :cron_expression | ||
|
||
class << self | ||
attr_accessor :abstract_class | ||
|
||
def abstract_class? | ||
defined?(@abstract_class) && @abstract_class == true | ||
end | ||
|
||
def cron(cron_expression) | ||
self.cron_expression = cron_expression | ||
end | ||
|
||
# redefine this method to return false to remove a cron job | ||
def enabled | ||
true | ||
end | ||
|
||
def schedule | ||
return if scheduled? | ||
|
||
dj = delayed_job | ||
if dj | ||
dj.update(cron: cron_expression) | ||
else | ||
::Delayed::Job.enqueue(new, cron: cron_expression) | ||
end | ||
end | ||
|
||
def remove | ||
delayed_job.destroy if scheduled? | ||
end | ||
|
||
def schedule_all | ||
load_all_subclasses | ||
|
||
descendants.each do |job_class| | ||
next if job_class.abstract_class? | ||
|
||
if job_class.enabled | ||
job_class.schedule | ||
else | ||
job_class.remove | ||
end | ||
end | ||
end | ||
|
||
def scheduled? | ||
dj = delayed_job | ||
!dj.nil? && dj.cron == cron_expression | ||
end | ||
|
||
def delayed_job | ||
::Delayed::Job.where(handler: "--- !ruby/object:#{name} {}\n").first | ||
end | ||
|
||
def cron_name | ||
name.demodulize[0...-3] | ||
end | ||
|
||
private | ||
|
||
def load_all_subclasses | ||
# Need to load all jobs definitions in order to find subclasses | ||
glob = Rails.root.join('app/jobs/cron/**/*_job.rb') | ||
Dir.glob(glob).sort.each do |file| | ||
require file | ||
end | ||
end | ||
end | ||
|
||
self.abstract_class = true | ||
|
||
def perform | ||
perform_once | ||
rescue StandardError => e | ||
::Chouette::Safe.capture("#{self.class.cron_name} Cron Job failed", e) | ||
end | ||
|
||
# this method HAS TO be redefined in subclasses | ||
def perform_once | ||
raise NotImplementedError | ||
end | ||
end | ||
end |
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,15 @@ | ||
# frozen_string_literal: true | ||
|
||
module Cron | ||
class CheckDeadOperationsJob < MinutesJob | ||
def perform_once | ||
# check_ccset_operations | ||
# ::ParentNotifier.new(ComplianceCheckSet).notify_when_finished | ||
::ComplianceCheckSet.abort_old | ||
|
||
# check_import_operations | ||
::Import::Netex.abort_old | ||
::ParentNotifier.new(Import::Netex).notify_when_finished | ||
end | ||
end | ||
end |
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,9 @@ | ||
# frozen_string_literal: true | ||
|
||
module Cron | ||
class CheckNightlyAggregatesJob < MinutesJob | ||
def perform_once | ||
::Workgroup.where(nightly_aggregate_enabled: true).find_each(&:nightly_aggregate!) | ||
end | ||
end | ||
end |
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,9 @@ | ||
# frozen_string_literal: true | ||
|
||
module Cron | ||
class DailyJob < BaseJob | ||
self.abstract_class = true | ||
|
||
cron '0 3 * * *' | ||
end | ||
end |
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,9 @@ | ||
# frozen_string_literal: true | ||
|
||
module Cron | ||
class HandleDeadWorkersJob < MinutesJob | ||
def perform_once | ||
::Delayed::Heartbeat.delete_timed_out_workers | ||
end | ||
end | ||
end |
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,9 @@ | ||
# frozen_string_literal: true | ||
|
||
module Cron | ||
class HourlyJob < BaseJob | ||
self.abstract_class = true | ||
|
||
cron '0 * * * *' | ||
end | ||
end |
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,9 @@ | ||
# frozen_string_literal: true | ||
|
||
module Cron | ||
class MinutesJob < BaseJob | ||
self.abstract_class = true | ||
|
||
cron '*/5 * * * *' | ||
end | ||
end |
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,9 @@ | ||
# frozen_string_literal: true | ||
|
||
module Cron | ||
class PurgeReferentialJob < DailyJob | ||
def perform_once | ||
::Referential.clean! | ||
end | ||
end | ||
end |
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,9 @@ | ||
# frozen_string_literal: true | ||
|
||
module Cron | ||
class PurgeWorkgroupsJob < DailyJob | ||
def perform_once | ||
::Workgroup.purge_all | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.