Skip to content

Commit

Permalink
Merged in CHOUETTE-3189-replace-cron-aka-whenever- (pull request #1499)
Browse files Browse the repository at this point in the history
CHOUETTE-3189 Replace cron (aka whenever) by DelayedJob Cron

Approved-by: Luc Donnet
  • Loading branch information
Aymeric Le Dorze authored and Luc Donnet committed Dec 12, 2023
2 parents 7fcf8bc + 3b5cd9a commit 9ae3b4a
Show file tree
Hide file tree
Showing 30 changed files with 463 additions and 312 deletions.
5 changes: 2 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,12 @@ COPY config/database.yml.docker config/database.yml
COPY config/secrets.yml.docker config/secrets.yml
RUN build.sh docker::env::production

# Run whenever to define crontab
# Create version.json file if VERSION is available
COPY --from=assets-builder /app/public/assets/ public/assets/
COPY --from=assets-builder /app/public/packs/ public/packs/

# Create version.json file if VERSION is available
ARG VERSION
RUN build.sh docker::whenever docker::version
RUN build.sh docker::version

RUN bundle exec bootsnap precompile --gemfile app/ lib/

Expand Down
1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ gem 'delayed_job_active_record'
gem 'delayed_job_heartbeat_plugin'
gem 'delayed_cron_job'

gem 'whenever', en_route: 'whenever', require: false
gem 'rake'
gem 'ros-apartment', require: 'apartment'
gem 'aasm'
Expand Down
9 changes: 0 additions & 9 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,6 @@ GIT
rgeo-proj4
rubyzip

GIT
remote: https://bitbucket.org/enroute-mobi/whenever.git
revision: b7963381a11243affe4f35881c85be0710f434e3
specs:
whenever (0.9.4)
chronic (>= 0.6.3)

GIT
remote: https://github.com/metaware/carrierwave-google-storage.git
revision: d8092cb70096ae599ffb99ab99ee13c9fc4deafd
Expand Down Expand Up @@ -207,7 +200,6 @@ GEM
mime-types (>= 1.16)
ssrf_filter (~> 1.0, < 1.1.0)
choice (0.2.0)
chronic (0.10.2)
cliver (0.3.2)
cocoon (1.2.15)
coderay (1.1.3)
Expand Down Expand Up @@ -1006,7 +998,6 @@ DEPENDENCIES
uglifier (~> 2.7.2)
webmock
webpacker (= 6.0.0.beta.7)
whenever!
will_paginate
will_paginate-bootstrap
x25519
Expand Down
89 changes: 89 additions & 0 deletions app/jobs/cron/base_job.rb
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
15 changes: 15 additions & 0 deletions app/jobs/cron/check_dead_operations_job.rb
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
9 changes: 9 additions & 0 deletions app/jobs/cron/check_nightly_aggregates_job.rb
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
9 changes: 9 additions & 0 deletions app/jobs/cron/daily_job.rb
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
9 changes: 9 additions & 0 deletions app/jobs/cron/handle_dead_workers_job.rb
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
9 changes: 9 additions & 0 deletions app/jobs/cron/hourly_job.rb
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
9 changes: 9 additions & 0 deletions app/jobs/cron/minutes_job.rb
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
9 changes: 9 additions & 0 deletions app/jobs/cron/purge_referential_job.rb
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
9 changes: 9 additions & 0 deletions app/jobs/cron/purge_workgroups_job.rb
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
93 changes: 0 additions & 93 deletions app/lib/cron.rb

This file was deleted.

6 changes: 1 addition & 5 deletions build.rc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Used by build.sh to customize/add functions

export DEV_PACKAGES="libpq-dev libxml2-dev zlib1g-dev libmagic-dev libmagickwand-dev libproj-dev libgeos-dev"
export RUN_PACKAGES="libpq5 libxml2 zlib1g libmagic1 imagemagick libproj19 libgeos-c1v5 cron libjemalloc2"
export RUN_PACKAGES="libpq5 libxml2 zlib1g libmagic1 imagemagick libproj19 libgeos-c1v5 libjemalloc2"

function docker_assets_precompile() {
RUBYOPT="-W0" bundle exec rake ci:fix_webpacker assets:precompile i18n:js:export RAILS_DB_ADAPTER=nulldb RAILS_DB_PASSWORD=none RAILS_ENV=production
Expand All @@ -12,7 +12,3 @@ function docker_env_production() {
cp config/environments/production.rb.sample config/environments/production.rb
fi
}

function docker_whenever() {
RAILS_ENV=production bundle exec whenever --output '/proc/1/fd/1' --update-crontab chouette --set 'environment=production&bundle_command=bundle exec' --roles=app,db,web
}
2 changes: 0 additions & 2 deletions config/initializers/cron.rb

This file was deleted.

Loading

0 comments on commit 9ae3b4a

Please sign in to comment.