From 5b247e8989393e8b784ae28d8d4907764f988381 Mon Sep 17 00:00:00 2001 From: Rodrigo Nardi Date: Sat, 15 Jun 2024 08:41:03 -0300 Subject: [PATCH 1/9] GitHub Warnings Adding accept type to reactions. Signed-off-by: Rodrigo Nardi --- lib/github/check.rb | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/github/check.rb b/lib/github/check.rb index 84120b6..a09fc62 100644 --- a/lib/github/check.rb +++ b/lib/github/check.rb @@ -51,11 +51,13 @@ def add_comment(pr_id, comment, repo) end def comment_reaction_thumb_up(repo, comment_id) - @app.create_issue_comment_reaction(repo, comment_id, '+1') + @app.create_issue_comment_reaction(repo, comment_id, '+1', + accept: Octokit::Preview::PREVIEW_TYPES[:reactions]) end def comment_reaction_thumb_down(repo, comment_id) - @app.create_issue_comment_reaction(repo, comment_id, '-1') + @app.create_issue_comment_reaction(repo, comment_id, '-1', + accept: Octokit::Preview::PREVIEW_TYPES[:reactions]) end def create(name) @@ -63,7 +65,7 @@ def create(name) @check_suite.pull_request.repository, name, @check_suite.commit_sha_ref, - accept: 'application/vnd.github.antiope-preview+json' + accept: Octokit::Preview::PREVIEW_TYPES[:checks] ) end @@ -94,7 +96,7 @@ def skipped(check_ref, output = {}) def get_check_run(check_ref) @app.check_run(@check_suite.pull_request.repository, check_ref, - accept: 'application/vnd.github.antiope-preview+json').to_h + accept: Octokit::Preview::PREVIEW_TYPES[:checks]).to_h end def fetch_check_runs @@ -104,7 +106,7 @@ def fetch_check_runs .check_runs_for_ref( @check_suite.pull_request.repository, @check_suite.pull_request.branch_name, - accept: 'application/vnd.github.antiope-preview+json' + accept: Octokit::Preview::PREVIEW_TYPES[:checks] ) .to_h[:check_runs] .map do |check_run| @@ -135,7 +137,7 @@ def fetch_username(username) def basic_status(check_ref, status, output) opts = { status: status, - accept: 'application/vnd.github.antiope-preview+json' + accept: Octokit::Preview::PREVIEW_TYPES[:checks] } opts[:output] = output unless output.empty? From f2cd69f10b24c6fa85fefa6cbfeca85168ba68ab Mon Sep 17 00:00:00 2001 From: Rodrigo Nardi Date: Mon, 17 Jun 2024 10:06:18 -0300 Subject: [PATCH 2/9] GitHub Warnings Updating unit tests Signed-off-by: Rodrigo Nardi --- spec/lib/github/check_spec.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/spec/lib/github/check_spec.rb b/spec/lib/github/check_spec.rb index a2ad915..89d5b97 100644 --- a/spec/lib/github/check_spec.rb +++ b/spec/lib/github/check_spec.rb @@ -74,7 +74,9 @@ let(:pr_info) { { comment_id: comment_id } } before do - allow(fake_client).to receive(:create_issue_comment_reaction).with(repo, comment_id, '+1').and_return(pr_info) + allow(fake_client).to receive(:create_issue_comment_reaction) + .with(repo, comment_id, '+1', accept: Octokit::Preview::PREVIEW_TYPES[:reactions]) + .and_return(pr_info) end it 'must returns pull request info' do From 1ba0f5cf61ff528819be4c4e55832018c48d1071 Mon Sep 17 00:00:00 2001 From: Rodrigo Nardi Date: Mon, 17 Jun 2024 20:03:41 -0300 Subject: [PATCH 3/9] Delayed Job This commit introduces the use of DelayedJob to avoid spamming the GitHub CI and simplify its output. Two workers were added to update the output and status of each PR. Signed-off-by: Rodrigo Nardi --- Rakefile | 16 +++++++++ bin/console.rb | 1 + config.ru | 5 +++ config/delayed_job.rb | 36 +++++++++++++++++++ .../20240617121935_create_delayed_jobs.rb | 26 ++++++++++++++ db/schema.rb | 16 ++++++++- lib/delayed_job_ctrl/delayed_job_ctrl.rb | 34 ++++++++++++++++++ lib/github/update_status.rb | 27 +++++++++----- lib/github_ci_app.rb | 2 ++ workers/ci_job_status.rb | 30 ++++++++++++++++ 10 files changed, 184 insertions(+), 9 deletions(-) create mode 100644 config/delayed_job.rb create mode 100644 db/migrate/20240617121935_create_delayed_jobs.rb create mode 100644 lib/delayed_job_ctrl/delayed_job_ctrl.rb create mode 100644 workers/ci_job_status.rb diff --git a/Rakefile b/Rakefile index 8d3e9f4..252df08 100644 --- a/Rakefile +++ b/Rakefile @@ -13,6 +13,22 @@ require 'otr-activerecord' load 'tasks/otr-activerecord.rake' +task :environment do + require_relative 'config/delayed_job' +end + +namespace :jobs do + desc 'Clear the delayed_job queue.' + task clear: :environment do + Delayed::Job.delete_all + end + + desc 'Start a delayed_job worker.' + task work: :environment do + Delayed::Worker.new(min_priority: ENV.fetch('MIN_PRIORITY', 1), max_priority: ENV.fetch('MAX_PRIORITY', 10)).start + end +end + namespace :db do # Some db tasks require your app code to be loaded; they'll expect to find it here task :environment do diff --git a/bin/console.rb b/bin/console.rb index b6424af..4633219 100755 --- a/bin/console.rb +++ b/bin/console.rb @@ -16,5 +16,6 @@ puts "Starting console: #{ENV.fetch('RAILS_ENV', nil)}" require_relative '../config/setup' +require_relative '../config/delayed_job' IRB.start diff --git a/config.ru b/config.ru index b8d98ff..79724d7 100755 --- a/config.ru +++ b/config.ru @@ -9,6 +9,8 @@ # frozen_string_literal: true require_relative 'app/github_app' +require_relative 'config/delayed_job' +require_relative 'lib/delayed_job_ctrl/delayed_job_ctrl' require 'puma' require 'rack/handler/puma' @@ -16,6 +18,9 @@ require 'rack/session/cookie' File.write('.session.key', SecureRandom.hex(32)) +DelayedJobCtrl.instance.create_worker(0, 5) +DelayedJobCtrl.instance.create_worker(6, 9) + use Rack::Session::Cookie, secret: File.read('.session.key'), same_site: true, max_age: 86_400 Rack::Handler::Puma.run Rack::URLMap.new('/' => GithubApp) diff --git a/config/delayed_job.rb b/config/delayed_job.rb new file mode 100644 index 0000000..e780e9c --- /dev/null +++ b/config/delayed_job.rb @@ -0,0 +1,36 @@ +# SPDX-License-Identifier: BSD-2-Clause +# +# delayed_job.rb +# Part of NetDEF CI System +# +# Copyright (c) 2024 by +# Network Device Education Foundation, Inc. ("NetDEF") +# +# frozen_string_literal: true + +require_relative '../lib/helpers/github_logger' +require_relative '../database_loader' + +require 'delayed_job' +require 'active_support' + +module Rails + class << self + attr_accessor :logger + end +end + +Rails.logger = GithubLogger.instance.create('delayed_job.log', Logger::INFO) +ActiveRecord::Base.logger = GithubLogger.instance.create('delayed_job.log', Logger::INFO) + +# this is used by DJ to guess where tmp/pids is located (default) +RAILS_ROOT = File.expand_path(__FILE__) + +Delayed::Worker.backend = :active_record +Delayed::Worker.destroy_failed_jobs = true +Delayed::Worker.sleep_delay = 5 +Delayed::Worker.max_attempts = 5 +Delayed::Worker.max_run_time = 5.minutes + +config = YAML.load_file('config/database.yml')[ENV.fetch('RACK_ENV', 'development')] +ActiveRecord::Base.establish_connection(config) diff --git a/db/migrate/20240617121935_create_delayed_jobs.rb b/db/migrate/20240617121935_create_delayed_jobs.rb new file mode 100644 index 0000000..d048d92 --- /dev/null +++ b/db/migrate/20240617121935_create_delayed_jobs.rb @@ -0,0 +1,26 @@ +# SPDX-License-Identifier: BSD-2-Clause +# +# 20240617121935_create_delayed_jobs.rb +# Part of NetDEF CI System +# +# Copyright (c) 2024 by +# Network Device Education Foundation, Inc. ("NetDEF") +# +# frozen_string_literal: true + +class CreateDelayedJobs < ActiveRecord::Migration[6.0] + def change + create_table :delayed_jobs do |t| + t.integer :priority, default: 0, null: false + t.integer :attempts, default: 0, null: false + t.text :handler, null: false + t.text :last_error + t.datetime :run_at + t.datetime :locked_at + t.datetime :failed_at + t.string :locked_by + t.string :queue + t.timestamps null: false + end + end +end diff --git a/db/schema.rb b/db/schema.rb index ff946ad..5dafe7f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2024_04_17_130601) do +ActiveRecord::Schema[7.0].define(version: 2024_06_17_121935) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -81,6 +81,20 @@ t.index ["stage_id"], name: "index_ci_jobs_on_stage_id" end + create_table "delayed_jobs", force: :cascade do |t| + t.integer "priority", default: 0, null: false + t.integer "attempts", default: 0, null: false + t.text "handler", null: false + t.text "last_error" + t.datetime "run_at", precision: nil + t.datetime "locked_at", precision: nil + t.datetime "failed_at", precision: nil + t.string "locked_by" + t.string "queue" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "github_users", force: :cascade do |t| t.string "github_login" t.string "github_username" diff --git a/lib/delayed_job_ctrl/delayed_job_ctrl.rb b/lib/delayed_job_ctrl/delayed_job_ctrl.rb new file mode 100644 index 0000000..88990d4 --- /dev/null +++ b/lib/delayed_job_ctrl/delayed_job_ctrl.rb @@ -0,0 +1,34 @@ +# SPDX-License-Identifier: BSD-2-Clause +# +# delayed_job_ctrl.rb +# Part of NetDEF CI System +# +# Copyright (c) 2024 by +# Network Device Education Foundation, Inc. ("NetDEF") +# +# frozen_string_literal: true + +require 'singleton' +require_relative '../../config/delayed_job' + +class DelayedJobCtrl + include Singleton + + def initialize + @threads = [] + @stop_requested = false + end + + def create_worker(min_priority, max_priority) + @threads << + Thread.new do + worker = Delayed::Worker.new(min_priority: min_priority, max_priority: max_priority, quiet: false) + + Thread.exit if @stop_requested + + worker.start + rescue StandardError + Thread.exit + end + end +end diff --git a/lib/github/update_status.rb b/lib/github/update_status.rb index f597b15..9acdbf0 100644 --- a/lib/github/update_status.rb +++ b/lib/github/update_status.rb @@ -79,7 +79,7 @@ def update_status return [200, 'Success'] unless @job.check_suite.pull_request.current_execution? @job.check_suite - update_build_summary_or_finished + insert_new_delayed_job [200, 'Success'] rescue StandardError => e @@ -88,16 +88,27 @@ def update_status [500, 'Internal Server Error'] end - def update_build_summary_or_finished - summary = Github::Build::Summary.new(@job) - summary.build_summary + def insert_new_delayed_job + queue = @job.check_suite.pull_request.github_pr_id % 10 - return unless @job.finished? + if can_add_new_job? + return CiJobStatus.delay(run_at: 5.seconds.from_now, priority: queue).update(@job.check_suite.id, @job.id) + end + + delete_and_create_delayed_job(queue) + end - logger(Logger::INFO, "Github::PlanExecution::Finished: '#{@job.check_suite.bamboo_ci_ref}'") + def delete_and_create_delayed_job(queue) + fetch_delayed_job.destroy_all + CiJobStatus.delay(run_at: 5.seconds.from_now, priority: queue).update(@job.check_suite.id, @job.id) + end + + def can_add_new_job? + fetch_delayed_job.empty? + end - finished = Github::PlanExecution::Finished.new({ 'bamboo_ref' => @job.check_suite.bamboo_ci_ref }) - finished.finished + def fetch_delayed_job + Delayed::Job.where('handler LIKE ?', "%method_name: :update\nargs:\n- #{@job.check_suite.id}%") end def current_execution? diff --git a/lib/github_ci_app.rb b/lib/github_ci_app.rb index b09faf4..58cf6b3 100644 --- a/lib/github_ci_app.rb +++ b/lib/github_ci_app.rb @@ -36,6 +36,8 @@ require_relative 'helpers/sinatra_payload' require_relative 'helpers/telemetry' +require_relative '../workers/ci_job_status' + # Slack libs require_relative 'slack/slack' diff --git a/workers/ci_job_status.rb b/workers/ci_job_status.rb new file mode 100644 index 0000000..970244c --- /dev/null +++ b/workers/ci_job_status.rb @@ -0,0 +1,30 @@ +# SPDX-License-Identifier: BSD-2-Clause +# +# ci_job_status.rb +# Part of NetDEF CI System +# +# Copyright (c) 2024 by +# Network Device Education Foundation, Inc. ("NetDEF") +# +# frozen_string_literal: true + +require_relative '../config/setup' + +class CiJobStatus + def self.update(check_suite_id, ci_job_id) + @logger = GithubLogger.instance.create('ci_job_status.log', Logger::INFO) + @logger.info("CiJobStatus::Update: Checksuite #{check_suite_id} -> '#{ci_job_id}'") + + job = CiJob.find(ci_job_id) + + summary = Github::Build::Summary.new(job) + summary.build_summary + + return unless job.finished? + + @logger.info("Github::PlanExecution::Finished: '#{job.check_suite.bamboo_ci_ref}'") + + finished = Github::PlanExecution::Finished.new({ 'bamboo_ref' => job.check_suite.bamboo_ci_ref }) + finished.finished + end +end From 437b3adab54edc964b926b575e018ecc223b0cda Mon Sep 17 00:00:00 2001 From: Rodrigo Nardi Date: Mon, 17 Jun 2024 20:06:47 -0300 Subject: [PATCH 4/9] Delayed Job Adding no code coverage to this class Signed-off-by: Rodrigo Nardi --- lib/delayed_job_ctrl/delayed_job_ctrl.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/delayed_job_ctrl/delayed_job_ctrl.rb b/lib/delayed_job_ctrl/delayed_job_ctrl.rb index 88990d4..1e36835 100644 --- a/lib/delayed_job_ctrl/delayed_job_ctrl.rb +++ b/lib/delayed_job_ctrl/delayed_job_ctrl.rb @@ -14,6 +14,7 @@ class DelayedJobCtrl include Singleton + # :nocov: def initialize @threads = [] @stop_requested = false @@ -31,4 +32,5 @@ def create_worker(min_priority, max_priority) Thread.exit end end + # :nocov: end From 5c8e248fcd55d264a2eee4dc479d96e5a5ba8359 Mon Sep 17 00:00:00 2001 From: Rodrigo Nardi Date: Mon, 17 Jun 2024 20:31:03 -0300 Subject: [PATCH 5/9] Delayed Job Adding DelayedJob at unit tests Signed-off-by: Rodrigo Nardi --- config.ru | 2 ++ lib/delayed_job_ctrl/delayed_job_ctrl.rb | 6 ++++++ lib/github/update_status.rb | 8 ++++++-- spec/spec_helper.rb | 10 ++++++++++ 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/config.ru b/config.ru index 79724d7..6afbff4 100755 --- a/config.ru +++ b/config.ru @@ -25,4 +25,6 @@ use Rack::Session::Cookie, secret: File.read('.session.key'), same_site: true, m Rack::Handler::Puma.run Rack::URLMap.new('/' => GithubApp) +DelayedJobCtrl.instance.stop_workers + exit 0 diff --git a/lib/delayed_job_ctrl/delayed_job_ctrl.rb b/lib/delayed_job_ctrl/delayed_job_ctrl.rb index 1e36835..f4ef16e 100644 --- a/lib/delayed_job_ctrl/delayed_job_ctrl.rb +++ b/lib/delayed_job_ctrl/delayed_job_ctrl.rb @@ -14,6 +14,8 @@ class DelayedJobCtrl include Singleton + DELAY = 5 + # :nocov: def initialize @threads = [] @@ -32,5 +34,9 @@ def create_worker(min_priority, max_priority) Thread.exit end end + + def stop_workers + @stop_requested = true + end # :nocov: end diff --git a/lib/github/update_status.rb b/lib/github/update_status.rb index 9acdbf0..969361f 100644 --- a/lib/github/update_status.rb +++ b/lib/github/update_status.rb @@ -92,7 +92,9 @@ def insert_new_delayed_job queue = @job.check_suite.pull_request.github_pr_id % 10 if can_add_new_job? - return CiJobStatus.delay(run_at: 5.seconds.from_now, priority: queue).update(@job.check_suite.id, @job.id) + return CiJobStatus + .delay(run_at: DelayedJobCtrl::DELAY.seconds.from_now, priority: queue) + .update(@job.check_suite.id, @job.id) end delete_and_create_delayed_job(queue) @@ -100,7 +102,9 @@ def insert_new_delayed_job def delete_and_create_delayed_job(queue) fetch_delayed_job.destroy_all - CiJobStatus.delay(run_at: 5.seconds.from_now, priority: queue).update(@job.check_suite.id, @job.id) + CiJobStatus + .delay(run_at: DelayedJobCtrl::DELAY.seconds.from_now, priority: queue) + .update(@job.check_suite.id, @job.id) end def can_add_new_job? diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index be86bd0..30769a6 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -26,6 +26,7 @@ SimpleCov.start require_relative '../app/github_app' +require_relative '../lib/delayed_job_ctrl/delayed_job_ctrl' def app GithubApp @@ -40,6 +41,15 @@ def app config.before(:all) do DatabaseCleaner.clean + DelayedJobCtrl.instance.create_worker(0, 10) + end + + config.after(:all) do + DelayedJobCtrl.instance.stop_workers + end + + config.before(:each) do + Delayed::Worker.delay_jobs = false end config.after(:each) do From ce6a15ec2badb131f59058579793a0a52e9ab8cc Mon Sep 17 00:00:00 2001 From: Rodrigo Nardi Date: Tue, 18 Jun 2024 06:08:21 -0300 Subject: [PATCH 6/9] Delayed Job Adding Gem Signed-off-by: Rodrigo Nardi --- Gemfile | 3 +++ Gemfile.lock | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/Gemfile b/Gemfile index f7cda4c..a7db1a5 100644 --- a/Gemfile +++ b/Gemfile @@ -34,6 +34,9 @@ gem 'puma', '5.5.2' gem 'rake', '13.0.6' +# Delayed Job +gem 'delayed_job_active_record' + # Code lint gem 'rubocop', '1.56.1', group: %i[development test] gem 'rubocop-performance', group: %i[development test] diff --git a/Gemfile.lock b/Gemfile.lock index 7530fd2..088517d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -24,6 +24,11 @@ GEM activerecord (>= 5.a) database_cleaner-core (~> 2.0.0) database_cleaner-core (2.0.1) + delayed_job (4.1.11) + activesupport (>= 3.0, < 8.0) + delayed_job_active_record (4.1.8) + activerecord (>= 3.0, < 8.0) + delayed_job (>= 3.0, < 5) diff-lcs (1.5.0) docile (1.4.0) factory_bot (6.2.1) @@ -152,6 +157,7 @@ PLATFORMS DEPENDENCIES database_cleaner + delayed_job_active_record factory_bot faker jwt (= 2.2.2) From f092a6cbdee6d03d1edd343ff3efd98d7bc4646d Mon Sep 17 00:00:00 2001 From: Rodrigo Nardi Date: Thu, 20 Jun 2024 10:17:17 -0300 Subject: [PATCH 7/9] Delayed Job Running DelayedJob in a separate process from Puma. Signed-off-by: Rodrigo Nardi --- Rakefile | 15 +++++---- config.ru | 9 ++--- config/delayed_job.rb | 2 ++ lib/delayed_job_ctrl/delayed_job_ctrl.rb | 42 ------------------------ lib/github/update_status.rb | 4 +-- spec/spec_helper.rb | 15 +++++++-- 6 files changed, 30 insertions(+), 57 deletions(-) delete mode 100644 lib/delayed_job_ctrl/delayed_job_ctrl.rb diff --git a/Rakefile b/Rakefile index 252df08..6bfb59b 100644 --- a/Rakefile +++ b/Rakefile @@ -13,19 +13,22 @@ require 'otr-activerecord' load 'tasks/otr-activerecord.rake' -task :environment do - require_relative 'config/delayed_job' -end +require_relative 'config/delayed_job' +require_relative 'config/setup' namespace :jobs do desc 'Clear the delayed_job queue.' - task clear: :environment do + task :clear do Delayed::Job.delete_all end desc 'Start a delayed_job worker.' - task work: :environment do - Delayed::Worker.new(min_priority: ENV.fetch('MIN_PRIORITY', 1), max_priority: ENV.fetch('MAX_PRIORITY', 10)).start + task :work do + Delayed::Worker.new( + min_priority: ENV.fetch('MIN_PRIORITY', 0), + max_priority: ENV.fetch('MAX_PRIORITY', 10), + quiet: false + ).start end end diff --git a/config.ru b/config.ru index 6afbff4..2f0c02b 100755 --- a/config.ru +++ b/config.ru @@ -10,7 +10,6 @@ require_relative 'app/github_app' require_relative 'config/delayed_job' -require_relative 'lib/delayed_job_ctrl/delayed_job_ctrl' require 'puma' require 'rack/handler/puma' @@ -18,13 +17,15 @@ require 'rack/session/cookie' File.write('.session.key', SecureRandom.hex(32)) -DelayedJobCtrl.instance.create_worker(0, 5) -DelayedJobCtrl.instance.create_worker(6, 9) +pids = [] +pids << spawn("RACK_ENV=#{ENV.fetch('RACK_ENV', 'development')} rake jobs:work MIN_PRIORITY=0 MAX_PRIORITY=3") +pids << spawn("RACK_ENV=#{ENV.fetch('RACK_ENV', 'development')} rake jobs:work MIN_PRIORITY=4 MAX_PRIORITY=6") +pids << spawn("RACK_ENV=#{ENV.fetch('RACK_ENV', 'development')} rake jobs:work MIN_PRIORITY=7 MAX_PRIORITY=9") use Rack::Session::Cookie, secret: File.read('.session.key'), same_site: true, max_age: 86_400 Rack::Handler::Puma.run Rack::URLMap.new('/' => GithubApp) -DelayedJobCtrl.instance.stop_workers +pids.each { |pid| Process.kill('TERM', pid.to_i) } exit 0 diff --git a/config/delayed_job.rb b/config/delayed_job.rb index e780e9c..eb33989 100644 --- a/config/delayed_job.rb +++ b/config/delayed_job.rb @@ -20,6 +20,8 @@ class << self end end +DELAYED_JOB_TIMER = 5 + Rails.logger = GithubLogger.instance.create('delayed_job.log', Logger::INFO) ActiveRecord::Base.logger = GithubLogger.instance.create('delayed_job.log', Logger::INFO) diff --git a/lib/delayed_job_ctrl/delayed_job_ctrl.rb b/lib/delayed_job_ctrl/delayed_job_ctrl.rb deleted file mode 100644 index f4ef16e..0000000 --- a/lib/delayed_job_ctrl/delayed_job_ctrl.rb +++ /dev/null @@ -1,42 +0,0 @@ -# SPDX-License-Identifier: BSD-2-Clause -# -# delayed_job_ctrl.rb -# Part of NetDEF CI System -# -# Copyright (c) 2024 by -# Network Device Education Foundation, Inc. ("NetDEF") -# -# frozen_string_literal: true - -require 'singleton' -require_relative '../../config/delayed_job' - -class DelayedJobCtrl - include Singleton - - DELAY = 5 - - # :nocov: - def initialize - @threads = [] - @stop_requested = false - end - - def create_worker(min_priority, max_priority) - @threads << - Thread.new do - worker = Delayed::Worker.new(min_priority: min_priority, max_priority: max_priority, quiet: false) - - Thread.exit if @stop_requested - - worker.start - rescue StandardError - Thread.exit - end - end - - def stop_workers - @stop_requested = true - end - # :nocov: -end diff --git a/lib/github/update_status.rb b/lib/github/update_status.rb index 969361f..c189bb5 100644 --- a/lib/github/update_status.rb +++ b/lib/github/update_status.rb @@ -93,7 +93,7 @@ def insert_new_delayed_job if can_add_new_job? return CiJobStatus - .delay(run_at: DelayedJobCtrl::DELAY.seconds.from_now, priority: queue) + .delay(run_at: DELAYED_JOB_TIMER.seconds.from_now, priority: queue) .update(@job.check_suite.id, @job.id) end @@ -103,7 +103,7 @@ def insert_new_delayed_job def delete_and_create_delayed_job(queue) fetch_delayed_job.destroy_all CiJobStatus - .delay(run_at: DelayedJobCtrl::DELAY.seconds.from_now, priority: queue) + .delay(run_at: DELAYED_JOB_TIMER.seconds.from_now, priority: queue) .update(@job.check_suite.id, @job.id) end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 30769a6..7a2393f 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -26,7 +26,7 @@ SimpleCov.start require_relative '../app/github_app' -require_relative '../lib/delayed_job_ctrl/delayed_job_ctrl' +require_relative '../config/delayed_job' def app GithubApp @@ -39,13 +39,22 @@ def app config.include FactoryBot::Syntax::Methods config.include WebMock::API + pid = nil + config.before(:all) do DatabaseCleaner.clean - DelayedJobCtrl.instance.create_worker(0, 10) + + pid = Thread.new do + Delayed::Worker.new( + min_priority: 0, + max_priority: 10, + quiet: true + ).start + end end config.after(:all) do - DelayedJobCtrl.instance.stop_workers + pid&.exit end config.before(:each) do From 4d77ac4081a738a5e3c7e35d8177f63be4ee8d94 Mon Sep 17 00:00:00 2001 From: Rodrigo Nardi Date: Thu, 20 Jun 2024 10:46:26 -0300 Subject: [PATCH 8/9] Delayed Job Changing DelyaedJob to use queue instead priority Signed-off-by: Rodrigo Nardi --- Rakefile | 4 ++-- config.ru | 6 +++--- lib/github/update_status.rb | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Rakefile b/Rakefile index 6bfb59b..62e12e2 100644 --- a/Rakefile +++ b/Rakefile @@ -24,9 +24,9 @@ namespace :jobs do desc 'Start a delayed_job worker.' task :work do + puts "Starting delayed_job worker - Queues: #{ENV.fetch('QUEUES', 'default')}" Delayed::Worker.new( - min_priority: ENV.fetch('MIN_PRIORITY', 0), - max_priority: ENV.fetch('MAX_PRIORITY', 10), + queue: ENV.fetch('QUEUES', 'default'), quiet: false ).start end diff --git a/config.ru b/config.ru index 2f0c02b..0d34e4c 100755 --- a/config.ru +++ b/config.ru @@ -18,9 +18,9 @@ require 'rack/session/cookie' File.write('.session.key', SecureRandom.hex(32)) pids = [] -pids << spawn("RACK_ENV=#{ENV.fetch('RACK_ENV', 'development')} rake jobs:work MIN_PRIORITY=0 MAX_PRIORITY=3") -pids << spawn("RACK_ENV=#{ENV.fetch('RACK_ENV', 'development')} rake jobs:work MIN_PRIORITY=4 MAX_PRIORITY=6") -pids << spawn("RACK_ENV=#{ENV.fetch('RACK_ENV', 'development')} rake jobs:work MIN_PRIORITY=7 MAX_PRIORITY=9") +pids << spawn("RACK_ENV=#{ENV.fetch('RACK_ENV', 'development')} rake jobs:work QUEUES=0,1,2,3") +pids << spawn("RACK_ENV=#{ENV.fetch('RACK_ENV', 'development')} rake jobs:work QUEUES=4,5,6") +pids << spawn("RACK_ENV=#{ENV.fetch('RACK_ENV', 'development')} rake jobs:work QUEUES=7,8,9") use Rack::Session::Cookie, secret: File.read('.session.key'), same_site: true, max_age: 86_400 diff --git a/lib/github/update_status.rb b/lib/github/update_status.rb index c189bb5..a9ec4ba 100644 --- a/lib/github/update_status.rb +++ b/lib/github/update_status.rb @@ -93,7 +93,7 @@ def insert_new_delayed_job if can_add_new_job? return CiJobStatus - .delay(run_at: DELAYED_JOB_TIMER.seconds.from_now, priority: queue) + .delay(run_at: DELAYED_JOB_TIMER.seconds.from_now, queue: queue) .update(@job.check_suite.id, @job.id) end @@ -103,7 +103,7 @@ def insert_new_delayed_job def delete_and_create_delayed_job(queue) fetch_delayed_job.destroy_all CiJobStatus - .delay(run_at: DELAYED_JOB_TIMER.seconds.from_now, priority: queue) + .delay(run_at: DELAYED_JOB_TIMER.seconds.from_now, queue: queue) .update(@job.check_suite.id, @job.id) end From 587dee9ceba624c0ae072f72f833376f1156b3e0 Mon Sep 17 00:00:00 2001 From: Rodrigo Nardi Date: Thu, 20 Jun 2024 10:50:21 -0300 Subject: [PATCH 9/9] Delayed Job Changing Process kill handler Signed-off-by: Rodrigo Nardi --- config.ru | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/config.ru b/config.ru index 0d34e4c..bfc2438 100755 --- a/config.ru +++ b/config.ru @@ -26,6 +26,10 @@ use Rack::Session::Cookie, secret: File.read('.session.key'), same_site: true, m Rack::Handler::Puma.run Rack::URLMap.new('/' => GithubApp) -pids.each { |pid| Process.kill('TERM', pid.to_i) } +pids.each do |pid| + Process.kill('TERM', pid.to_i) +rescue Errno::ESRCH + puts "Process #{pid} already dead" +end exit 0