diff --git a/Dockerfile b/Dockerfile index 063122a84..e3e9f7a72 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,10 +3,6 @@ FROM ruby:2.4 # Cleanup expired Let's Encrypt CA (Sept 30, 2021) RUN sed -i '/^mozilla\/DST_Root_CA_X3/s/^/!/' /etc/ca-certificates.conf && update-ca-certificates -f -RUN apt-get update && \ - apt-get install -y cron && \ - apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* - # Create a user for the web app. RUN addgroup --gid 9999 app && \ adduser --uid 9999 --gid 9999 --disabled-password --gecos "Application" app && \ @@ -19,9 +15,11 @@ WORKDIR /app # Configuration ARG gemfile=Gemfile ENV BUNDLE_GEMFILE=${gemfile} -ENV PUMA_OPTIONS "--preload -w 4 -p 3000" -ENV NNDD_VERSION "cdx-0.11-pre7" +ENV PUMA_OPTIONS="--preload -w 4 -p 3000" +ENV NNDD_VERSION="cdx-0.11-pre7" ENV RAILS_ENV=production +ENV RAILS_LOG_TO_STDOUT=true +ENV RAILS_SERVE_STATIC_FILES=true # Install gem bundle COPY Gemfile* cdx.gemspec cdx-api-elasticsearch.gemspec /app/ @@ -45,4 +43,4 @@ RUN /app/docker/config-nndd RUN mkdir -p /app/tmp /app/log && chown -R app:app /app/tmp /app/log EXPOSE 3000 -CMD ["/app/docker/web-run"] +CMD bundle exec puma $PUMA_OPTIONS -e $RAILS_ENV diff --git a/Gemfile b/Gemfile index 5ff64d8f5..714c78973 100644 --- a/Gemfile +++ b/Gemfile @@ -74,8 +74,7 @@ gem 'geojson_import', git: 'https://github.com/instedd/geojson_import', branch: # Services gem 'puma', '~> 3.0' gem 'sidekiq', '~> 4.2' -gem 'sidekiq-cron', '~> 0.3' # TODO: not maintained, consider sidekiq-scheduler instead -gem 'whenever', '~> 1.0' # TODO: replace with a sidekiq-cron job +gem 'sidekiq-cron' # External services gem 'location_service', git: 'https://github.com/instedd/ruby-location_service.git', branch: 'master' diff --git a/Gemfile.lock b/Gemfile.lock index 7a0b7b4f6..01e1a542a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -124,7 +124,6 @@ GEM capybara (>= 1.0, < 4) launchy childprocess (3.0.0) - chronic (0.10.2) chunky_png (1.4.0) climate_control (0.2.0) cocaine (0.5.8) @@ -137,7 +136,7 @@ GEM coffee-script-source execjs coffee-script-source (1.12.2) - concurrent-ruby (1.1.10) + concurrent-ruby (1.2.0) config (1.7.2) activesupport (>= 3.0) deep_merge (~> 1.2, >= 1.2.1) @@ -250,7 +249,7 @@ GEM ffi (1.15.5) filewatcher (0.3.6) trollop (~> 2.0) - fugit (1.8.0) + fugit (1.8.1) et-orbi (~> 1, >= 1.2.7) raabro (~> 1.4) gherkin (5.1.0) @@ -482,8 +481,6 @@ GEM ruby_parser (3.19.2) sexp_processor (~> 4.16) rubyzip (2.3.2) - rufus-scheduler (3.8.2) - fugit (~> 1.1, >= 1.1.6) sass (3.7.4) sass-listen (~> 4.0.0) sass-listen (4.0.0) @@ -506,8 +503,8 @@ GEM connection_pool (~> 2.2, >= 2.2.0) rack-protection (>= 1.5.0) redis (~> 3.2, >= 3.2.1) - sidekiq-cron (0.6.3) - rufus-scheduler (>= 3.3.0) + sidekiq-cron (1.2.0) + fugit (~> 1.1) sidekiq (>= 4.2.1) simplecov (0.18.5) docile (~> 1.1) @@ -541,7 +538,7 @@ GEM ttfunk (1.6.2.1) turbolinks (2.5.4) coffee-rails - tzinfo (1.2.10) + tzinfo (1.2.11) thread_safe (~> 0.1) uglifier (2.7.2) execjs (>= 0.3.0) @@ -563,8 +560,6 @@ GEM websocket-driver (0.6.5) websocket-extensions (>= 0.1.0) websocket-extensions (0.1.5) - whenever (1.0.0) - chronic (>= 0.6.3) xpath (3.2.0) nokogiri (~> 1.8) @@ -645,7 +640,7 @@ DEPENDENCIES selenium-webdriver (< 4.0) sentry-raven (~> 2.13) sidekiq (~> 4.2) - sidekiq-cron (~> 0.3) + sidekiq-cron simplecov site_prism (~> 3.0) spring @@ -659,7 +654,6 @@ DEPENDENCIES view_components! web-console (< 4.0) webmock (~> 2.3.1) - whenever (~> 1.0) BUNDLED WITH 1.17.3 diff --git a/app/jobs/cleanup_assay_files_job.rb b/app/jobs/cleanup_assay_files_job.rb new file mode 100644 index 000000000..92c063c70 --- /dev/null +++ b/app/jobs/cleanup_assay_files_job.rb @@ -0,0 +1,10 @@ +class CleanupAssayFilesJob + include Sidekiq::Worker + + # Delete AssayFile that don't have any AssayAttachment (parent model) associated. + def perform + AssayFile + .where("created_at > ? AND assay_attachment_id IS NULL", 7.days.ago) + .find_each(&:destroy) + end +end diff --git a/app/jobs/daily_alert_job.rb b/app/jobs/daily_alert_job.rb index dc3e52901..94cbb3ecd 100644 --- a/app/jobs/daily_alert_job.rb +++ b/app/jobs/daily_alert_job.rb @@ -8,8 +8,3 @@ def perform alert_history_check(1.day, Alert.aggregation_frequencies["day"]) end end - -# note: an alternative gem for scheduling cron jobs could be: http://github.com/javan/whenever but -# the advantage of gem "sidekiq-cron" is that it appears in the sidekiq web url , http://localhost:3000/sidekiq/cron - -Sidekiq::Cron::Job.create(name: 'Alert Daily - daily 12:15am', cron: '15 0 * * *', klass: 'DailyAlertJob') #run daily at 15 after midnight diff --git a/app/jobs/daily_demo_data.rb b/app/jobs/daily_demo_data.rb index 031cec6b9..1d9eafb5e 100644 --- a/app/jobs/daily_demo_data.rb +++ b/app/jobs/daily_demo_data.rb @@ -3,23 +3,22 @@ class DailyDemoData include Sidekiq::Worker - #just paste here, we can move this value into an env setting once we start to use this tool + #just paste here, we can move this value into an env setting once we start to use this tool def get_repeat_demo_per_device 1000 end - + ## # Note: it is assumed that rake db:ssed has been called to load the seeds data generated. # # if say 1000 daily testresults are needed per device, then loop over the device templates (only two templates at the moment) - def perform - insert_demo_data(get_repeat_demo_per_device) + def perform + insert_demo_data(get_repeat_demo_per_device) end - + end -# note: an alternative gem for scheduling cron jobs could be: http://github.com/javan/whenever but -# the advantage of gem "sidekiq-cron" is that it appears in the sidekiq web url , http://localhost:3000/sidekiq/cron if use_demo_data? - Sidekiq::Cron::Job.create(name: 'Demo Data - daily 12:15am', cron: '15 0 * * *', klass: 'DailyDemoData') #run daily at 15 after midnight + # run daily at 15 after midnight + Sidekiq::Cron::Job.create(name: 'Demo Data - daily 12:15am', cron: '15 0 * * *', klass: 'DailyDemoData') end diff --git a/app/jobs/hourly_alert_job.rb b/app/jobs/hourly_alert_job.rb index 018b4f83e..f138bcb44 100644 --- a/app/jobs/hourly_alert_job.rb +++ b/app/jobs/hourly_alert_job.rb @@ -8,11 +8,3 @@ def perform alert_history_check(1.hour, Alert.aggregation_frequencies["hour"]) end end - -# note: an alternative gem for scheduling cron jobs could be: http://github.com/javan/whenever but -# the advantage of gem "sidekiq-cron" is that it appears in the sidekiq web url , http://localhost:3000/sidekiq/cron - -Sidekiq::Cron::Job.create(name: 'Alert Hourly - hourly', cron: '0 * * * *', klass: 'HourlyAlertJob') #run each hour - -#for test run every 5 mins: -#Sidekiq::Cron::Job.create(name: 'Alert Hourly - 10mins 12:15am', cron: '*/10 * * * *', klass: 'HourlyAlertJob') #run daily at 15 after midnight diff --git a/app/jobs/hourly_utilization_efficiency_job.rb b/app/jobs/hourly_utilization_efficiency_job.rb index c8522b465..4317b9dcc 100644 --- a/app/jobs/hourly_utilization_efficiency_job.rb +++ b/app/jobs/hourly_utilization_efficiency_job.rb @@ -20,11 +20,11 @@ def perform #convert the aggregation [days or hours] into hours for calculations hours_per_day = 24 if alert.aggregation_frequency == "month" - utilization_efficiency_hours = amount * hours_per_day * 30 + utilization_efficiency_hours = amount * hours_per_day * 30 elsif alert.aggregation_frequency == "week" - utilization_efficiency_hours = amount * hours_per_day * 7 + utilization_efficiency_hours = amount * hours_per_day * 7 elsif alert.aggregation_frequency == "day" - utilization_efficiency_hours = amount * hours_per_day + utilization_efficiency_hours = amount * hours_per_day else utilization_efficiency_hours = amount end @@ -51,10 +51,3 @@ def perform end end end - -#run every 30 mins to give more accuracy, +/- 30 mins -Sidekiq::Cron::Job.create(name: 'Alert Utilization Efficiency - hourly', cron: '*/30 * * * *', klass: 'HourlyUtilizationEfficiencyJob') - - -#every 2 minsutes for testing -#Sidekiq::Cron::Job.create(name: 'Alert Utilization Efficiency - hourly', cron: '*/2 * * * *', klass: 'HourlyUtilizationEfficiencyJob') diff --git a/app/jobs/monthly_alert_job.rb b/app/jobs/monthly_alert_job.rb index 6db630b72..cddb55b13 100644 --- a/app/jobs/monthly_alert_job.rb +++ b/app/jobs/monthly_alert_job.rb @@ -8,7 +8,3 @@ def perform alert_history_check(1.month, Alert.aggregation_frequencies["month"]) end end - -Sidekiq::Cron::Job.create(name: 'Alert Monthly - daily 12:15am', cron: '35 0 * * *', klass: 'MonthlyAlertJob') #run daily at 35 after midnight - -#Sidekiq::Cron::Job.create(name: 'Alert Monthly - 10mins 12:15am', cron: '*/5 * * * *', klass: 'MonthlyAlertJob') diff --git a/app/jobs/weekly_alert_job.rb b/app/jobs/weekly_alert_job.rb index a5de7007a..690792f91 100644 --- a/app/jobs/weekly_alert_job.rb +++ b/app/jobs/weekly_alert_job.rb @@ -8,6 +8,3 @@ def perform alert_history_check(1.week, Alert.aggregation_frequencies["week"]) end end - -Sidekiq::Cron::Job.create(name: 'Alert Weekly - daily 12:15am', cron: '25 0 * * *', klass: 'WeekyAlertJob') #run daily at 25 after midnight - diff --git a/config/initializers/sidekiq.rb b/config/initializers/sidekiq.rb new file mode 100644 index 000000000..34033456b --- /dev/null +++ b/config/initializers/sidekiq.rb @@ -0,0 +1,26 @@ +Sidekiq.configure_server do |config| + config.on(:startup) do + # run daily at 15 after midnight + Sidekiq::Cron::Job.create(name: 'Alert Daily - daily 12:15am', cron: '15 0 * * *', klass: 'DailyAlertJob') + + # run daily at 25 after midnight + Sidekiq::Cron::Job.create(name: 'Alert Weekly - daily 12:15am', cron: '25 0 * * *', klass: 'WeekyAlertJob') + + # run daily at 35 after midnight + Sidekiq::Cron::Job.create(name: 'Alert Monthly - daily 12:15am', cron: '35 0 * * *', klass: 'MonthlyAlertJob') + + # run hourly + Sidekiq::Cron::Job.create(name: 'Alert Hourly - hourly', cron: '0 * * * *', klass: 'HourlyAlertJob') + + # run every 30 mins to give more accuracy, +/- 30 mins + Sidekiq::Cron::Job.create(name: 'Alert Utilization Efficiency - hourly', cron: '*/30 * * * *', klass: 'HourlyUtilizationEfficiencyJob') + + # for manual tests (run every some 2, 5 or 10 mins): + # Sidekiq::Cron::Job.create(name: 'Alert Utilization Efficiency - hourly', cron: '*/2 * * * *', klass: 'HourlyUtilizationEfficiencyJob') + # Sidekiq::Cron::Job.create(name: 'Alert Monthly - 10mins 12:15am', cron: '*/5 * * * *', klass: 'MonthlyAlertJob') + # Sidekiq::Cron::Job.create(name: 'Alert Hourly - 10mins 12:15am', cron: '*/10 * * * *', klass: 'HourlyAlertJob') + + # run daily at 02:00am + Sidekiq::Cron::Job.create(name: 'Cleanup unused AssayFile', cron: '0 2 * * *', klass: 'CleanupAssayFilesJob') + end +end diff --git a/config/schedule.rb b/config/schedule.rb deleted file mode 100644 index 51cdef76a..000000000 --- a/config/schedule.rb +++ /dev/null @@ -1,16 +0,0 @@ -# Use this file to easily define all of your cron jobs. -# -# -require File.expand_path(File.dirname(__FILE__) + "/../config/environment") - -set :output, "./cron_log.log" -set :environment, Rails.env - -# GEM_PATH and other environment variables are not being correctly loaded when running in a crontab -# so we load them in the job to be able to run it -# see: https://github.com/javan/whenever/issues/656 -ENV.each { |k, v| env(k, v) } - -every 1.week do - rake "assay_files:garbage_collector" -end diff --git a/docker/web-run b/docker/web-run deleted file mode 100755 index ad9635a98..000000000 --- a/docker/web-run +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash -source /etc/envvars -cd /app -exec su -p -c "bundle exec whenever --update-crontab && bundle exec puma $PUMA_OPTIONS -e $RAILS_ENV" app diff --git a/lib/tasks/assay_files.rake b/lib/tasks/assay_files.rake deleted file mode 100644 index ea27031c7..000000000 --- a/lib/tasks/assay_files.rake +++ /dev/null @@ -1,7 +0,0 @@ -namespace :assay_files do - desc "delete AssayFiles that don't have any AssayAttachment (parent model) associated" - task garbage_collector: :environment do - assay_files = AssayFile.where("created_at > ? AND assay_attachment_id IS NULL", 7.days.ago) - AssayFile.destroy(assay_files.map(&:id)) - end -end