From 6f23eb32bfcbcc6c1efe52cdd5ec993de4c14ee7 Mon Sep 17 00:00:00 2001 From: Brian Miller Date: Sat, 6 Oct 2018 11:19:40 -0700 Subject: [PATCH 1/4] Added sidekiq cron to allow for the scheduling of jobs Added the meetup sync job to run at 4am and added the cron tab to the sidekiq UI to make sure we have visibility into the scheduled jobs. --- Gemfile | 1 + Gemfile.lock | 12 +++++++++++- config/initializers/sidekiq.rb | 7 +++++++ config/job_schedule.yml | 20 ++++++++++++++++++++ config/routes.rb | 1 + 5 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 config/job_schedule.yml diff --git a/Gemfile b/Gemfile index 14de8d03..4e29918c 100644 --- a/Gemfile +++ b/Gemfile @@ -29,6 +29,7 @@ gem 'rubocop', '~> 0.55.0', require: false gem 'sendgrid-ruby' gem 'sentry-raven' gem 'sidekiq' +gem 'sidekiq-cron' gem 'skylight' gem 'pry-rails' diff --git a/Gemfile.lock b/Gemfile.lock index 30e19a6e..54670d01 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -90,6 +90,8 @@ GEM responders warden (~> 1.2.3) erubis (2.7.0) + et-orbi (1.1.6) + tzinfo execjs (2.7.0) factory_girl (4.8.0) activesupport (>= 3.0.0) @@ -104,6 +106,9 @@ GEM formtastic (3.1.5) actionpack (>= 3.2.13) formtastic_i18n (0.6.0) + fugit (1.1.6) + et-orbi (~> 1.1, >= 1.1.6) + raabro (~> 1.1) geocoder (1.4.3) gibbon (3.0.1) faraday (>= 0.9.1) @@ -185,6 +190,7 @@ GEM pry (>= 0.10.4) public_suffix (3.0.0) puma (3.8.2) + raabro (1.1.6) rack (2.0.5) rack-cors (0.4.1) rack-protection (2.0.3) @@ -251,6 +257,9 @@ GEM connection_pool (~> 2.2, >= 2.2.2) rack-protection (>= 1.5.0) redis (>= 3.3.5, < 5) + sidekiq-cron (1.0.4) + fugit (~> 1.1) + sidekiq (>= 4.2.1) skylight (1.5.0) activesupport (>= 3.0.0) spring (2.0.1) @@ -313,6 +322,7 @@ DEPENDENCIES sendgrid-ruby sentry-raven sidekiq + sidekiq-cron skylight spring spring-watcher-listen (~> 2.0.0) @@ -320,4 +330,4 @@ DEPENDENCIES webmock (~> 3.0, >= 3.0.1) BUNDLED WITH - 1.16.4 + 1.16.5 diff --git a/config/initializers/sidekiq.rb b/config/initializers/sidekiq.rb index 937538b0..a3897d19 100644 --- a/config/initializers/sidekiq.rb +++ b/config/initializers/sidekiq.rb @@ -1,4 +1,5 @@ require 'sidekiq' +require 'sidekiq-cron' Sidekiq.configure_server do |config| # https://stackoverflow.com/questions/28412913/disable-automatic-retry-with-activejob-used-with-sidekiq @@ -11,3 +12,9 @@ Rails.logger = Sidekiq::Logging.logger ActiveRecord::Base.logger = Sidekiq::Logging.logger end + +schedule_file = "config/job_schedule.yml" + +if File.exist?(schedule_file) && Sidekiq.server? + Sidekiq::Cron::Job.load_from_hash YAML.load_file(schedule_file) +end diff --git a/config/job_schedule.yml b/config/job_schedule.yml new file mode 100644 index 00000000..b04f26a5 --- /dev/null +++ b/config/job_schedule.yml @@ -0,0 +1,20 @@ +## Examples of job configurations + +#my_first_job: +# cron: "*/5 * * * *" +# class: "HardWorker" +# queue: hard_worker +# +#second_job: +# cron: "*/30 * * * *" # execute at every 30 minutes +# class: "HardWorker" +# queue: hard_worker_long +# args: +# hard: "stuff" + + +# At 04:00 every day run the meetup member sync job +meetup_member_sync_job: + cron: "0 4 * * *" + class: "MeetupMemberSyncJob" + queue: "default" diff --git a/config/routes.rb b/config/routes.rb index e410d4c7..b4b29948 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,6 +4,7 @@ ActiveAdmin.routes(self) require 'sidekiq/web' + require 'sidekiq/cron/web' authenticate :admin_user, ->(u) { u.role.super_admin? } do mount Sidekiq::Web => '/admin/sidekiq' end From 1b9df78023a98f9e093cb6bcbf852897c453caf6 Mon Sep 17 00:00:00 2001 From: Brian Miller Date: Sat, 6 Oct 2018 11:28:27 -0700 Subject: [PATCH 2/4] Fixed rubocop warnings --- config/initializers/sidekiq.rb | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/config/initializers/sidekiq.rb b/config/initializers/sidekiq.rb index a3897d19..95605c50 100644 --- a/config/initializers/sidekiq.rb +++ b/config/initializers/sidekiq.rb @@ -1,6 +1,8 @@ require 'sidekiq' require 'sidekiq-cron' +schedule_file = 'config/job_schedule.yml' + Sidekiq.configure_server do |config| # https://stackoverflow.com/questions/28412913/disable-automatic-retry-with-activejob-used-with-sidekiq # prevent excessive retries from some default status. @@ -11,10 +13,6 @@ } Rails.logger = Sidekiq::Logging.logger ActiveRecord::Base.logger = Sidekiq::Logging.logger -end - -schedule_file = "config/job_schedule.yml" -if File.exist?(schedule_file) && Sidekiq.server? - Sidekiq::Cron::Job.load_from_hash YAML.load_file(schedule_file) + Sidekiq::Cron::Job.load_from_hash YAML.load_file(schedule_file) if File.exist?(schedule_file) end From 6100dd78c1e188ce2a4c38bed6cbc7c286e746a7 Mon Sep 17 00:00:00 2001 From: Brian Miller Date: Sat, 6 Oct 2018 12:29:53 -0700 Subject: [PATCH 3/4] Added github collect statistics job and open sidekiq UI to the admin role --- .rubocop_todo.yml | 1 - .ruby-gemset | 2 +- app/jobs/github_collect_statistics_job.rb | 11 +++++++++++ config/job_schedule.yml | 6 ++++++ config/routes.rb | 2 +- lib/tasks/git_hub.rake | 15 --------------- 6 files changed, 19 insertions(+), 18 deletions(-) create mode 100644 app/jobs/github_collect_statistics_job.rb delete mode 100644 lib/tasks/git_hub.rake diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 01a95103..a682acc1 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -678,7 +678,6 @@ Style/RescueStandardError: - 'app/lib/json_web_token.rb' - 'app/lib/send_grid_client.rb' - 'config/initializers/core_extensions/devise/strategies/json_web_token.rb' - - 'lib/tasks/git_hub.rake' - 'lib/tasks/meetup.rake' - 'lib/tasks/resource.rake' - 'lib/tasks/users.rake' diff --git a/.ruby-gemset b/.ruby-gemset index 382ffbf3..4dcdb08b 100644 --- a/.ruby-gemset +++ b/.ruby-gemset @@ -1 +1 @@ -operationcode_backend +-global \ No newline at end of file diff --git a/app/jobs/github_collect_statistics_job.rb b/app/jobs/github_collect_statistics_job.rb new file mode 100644 index 00000000..aa6fb4fd --- /dev/null +++ b/app/jobs/github_collect_statistics_job.rb @@ -0,0 +1,11 @@ +class GithubCollectStatisticsJob + include Sidekiq::Worker + + def perform + limit = GitHub::Client.new.rate_limit + logger.info("Github Ratelimit: #{limit}") + + GitHub::PullRequests.new.fetch_and_save! + GitHub::Issues.new.fetch_and_save! + end +end diff --git a/config/job_schedule.yml b/config/job_schedule.yml index b04f26a5..77705170 100644 --- a/config/job_schedule.yml +++ b/config/job_schedule.yml @@ -18,3 +18,9 @@ meetup_member_sync_job: cron: "0 4 * * *" class: "MeetupMemberSyncJob" queue: "default" + +# At 03:00 every day run the github collect statistics job +github_collect_statistics_job: + cron: "0 3 * * *" + class: "GithubCollectStatisticsJob" + queue: "default" diff --git a/config/routes.rb b/config/routes.rb index b4b29948..2a3e8193 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -5,7 +5,7 @@ require 'sidekiq/web' require 'sidekiq/cron/web' - authenticate :admin_user, ->(u) { u.role.super_admin? } do + authenticate :admin_user, ->(u) { u.role.admin_accessible? } do mount Sidekiq::Web => '/admin/sidekiq' end diff --git a/lib/tasks/git_hub.rake b/lib/tasks/git_hub.rake deleted file mode 100644 index 48eedd3a..00000000 --- a/lib/tasks/git_hub.rake +++ /dev/null @@ -1,15 +0,0 @@ -namespace :git_hub do - desc 'Saves pull request, commit, issue and user details for all repositories to date, to GitHubStatistic and GitHubUser' - task collect_statistics: :environment do - begin - limit = GitHub::Client.new.rate_limit - p 'Rate limit:' - p limit - - GitHub::PullRequests.new.fetch_and_save! - GitHub::Issues.new.fetch_and_save! - rescue => e - Rails.logger.error "When fetching and saving GitHub statistics, experienced this error: #{e}" - end - end -end From e94bb185f964b86eebd703f1110a99f8c877ff52 Mon Sep 17 00:00:00 2001 From: William Montgomery <10781353+wimo7083@users.noreply.github.com> Date: Tue, 9 Oct 2018 13:58:35 -0600 Subject: [PATCH 4/4] see extended After discussing data collections, it's aware we need to wait until she nell returns so that we can make sure we won't get in trouble. Making this only run on feb 31. --- config/job_schedule.yml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/config/job_schedule.yml b/config/job_schedule.yml index 77705170..95fc7441 100644 --- a/config/job_schedule.yml +++ b/config/job_schedule.yml @@ -15,12 +15,14 @@ # At 04:00 every day run the meetup member sync job meetup_member_sync_job: - cron: "0 4 * * *" + #cron: "0 4 * * *" + cron: "0 0 5 31 2 ?" # only run Feb 31 class: "MeetupMemberSyncJob" queue: "default" # At 03:00 every day run the github collect statistics job github_collect_statistics_job: - cron: "0 3 * * *" + #cron: "0 3 * * *" + cron: "0 0 5 31 2 ?" # only run Feb 31 class: "GithubCollectStatisticsJob" queue: "default"