From 9a403d33a4d40495954d9c532aaad168300e7785 Mon Sep 17 00:00:00 2001 From: Misha Merkushin Date: Wed, 19 Oct 2022 15:53:17 +0300 Subject: [PATCH 1/2] add standalone mode --- gemfiles/agnostic.gemfile | 2 -- gemfiles/rails.5.gemfile | 2 -- gemfiles/rails.6.gemfile | 2 -- lib/schked/config.rb | 11 +++++++++-- lib/schked/worker.rb | 4 ++-- spec/lib/schked/config_spec.rb | 11 +++++++++++ spec/lib/schked/worker_spec.rb | 14 ++++++++++++++ spec/rails_helper.rb | 2 ++ spec/spec_helper.rb | 2 ++ 9 files changed, 40 insertions(+), 10 deletions(-) diff --git a/gemfiles/agnostic.gemfile b/gemfiles/agnostic.gemfile index 7a6434f..095e660 100644 --- a/gemfiles/agnostic.gemfile +++ b/gemfiles/agnostic.gemfile @@ -1,5 +1,3 @@ -# frozen_string_literal: true - # This file was generated by Appraisal source "https://rubygems.org" diff --git a/gemfiles/rails.5.gemfile b/gemfiles/rails.5.gemfile index 7a0df3c..aba6c7b 100644 --- a/gemfiles/rails.5.gemfile +++ b/gemfiles/rails.5.gemfile @@ -1,5 +1,3 @@ -# frozen_string_literal: true - # This file was generated by Appraisal source "https://rubygems.org" diff --git a/gemfiles/rails.6.gemfile b/gemfiles/rails.6.gemfile index d9cfaa4..6af5083 100644 --- a/gemfiles/rails.6.gemfile +++ b/gemfiles/rails.6.gemfile @@ -1,5 +1,3 @@ -# frozen_string_literal: true - # This file was generated by Appraisal source "https://rubygems.org" diff --git a/lib/schked/config.rb b/lib/schked/config.rb index 455682e..a25d779 100644 --- a/lib/schked/config.rb +++ b/lib/schked/config.rb @@ -6,7 +6,8 @@ module Schked class Config attr_writer :logger, :do_not_load_root_schedule, - :redis_servers + :redis_servers, + :standalone def paths @paths ||= [] @@ -35,7 +36,13 @@ def fire_callback(name, *args) end def redis_servers - @redis_servers ||= [ENV["REDIS_URL"]] + @redis_servers ||= [ENV.fetch("REDIS_URL", "redis://127.0.0.1:6379")] + end + + def standalone? + @standalone = ENV["RAILS_ENV"] == "test" || ENV["RACK_ENV"] == "test" if @standalone.nil? + + !!@standalone end private diff --git a/lib/schked/worker.rb b/lib/schked/worker.rb index aed191c..c44fc19 100644 --- a/lib/schked/worker.rb +++ b/lib/schked/worker.rb @@ -7,12 +7,12 @@ class Worker def initialize(config:) @config = config - @locker = RedisLocker.new(config.redis_servers, lock_ttl: 40_000) + @locker = RedisLocker.new(config.redis_servers, lock_ttl: 40_000) unless config.standalone? @scheduler = Rufus::Scheduler.new(trigger_lock: locker) watch_signals define_callbacks - define_extend_lock + define_extend_lock unless config.standalone? load_schedule end diff --git a/spec/lib/schked/config_spec.rb b/spec/lib/schked/config_spec.rb index dbc9f13..a132124 100644 --- a/spec/lib/schked/config_spec.rb +++ b/spec/lib/schked/config_spec.rb @@ -15,4 +15,15 @@ end it { expect(config.logger).to be_a(Logger) } + + it { expect(config).to be_standalone } + + context "when RACK_ENV=production" do + it "is not standalone" do + old_val = ENV["RACK_ENV"] + ENV["RACK_ENV"] = "production" + expect(config).not_to be_standalone + ENV["RACK_ENV"] = old_val + end + end end diff --git a/spec/lib/schked/worker_spec.rb b/spec/lib/schked/worker_spec.rb index 63804e8..f3b199a 100644 --- a/spec/lib/schked/worker_spec.rb +++ b/spec/lib/schked/worker_spec.rb @@ -44,6 +44,8 @@ describe "start" do it "starts rufus scheduler" do expect_any_instance_of(Rufus::Scheduler).to receive(:join) + expect(Schked::RedisLocker).not_to receive(:new) + expect_any_instance_of(described_class).not_to receive(:define_extend_lock) worker.wait end @@ -69,4 +71,16 @@ expect(logger).to have_received(:info).with(/Finished task: test_task/) end end + + describe "when is not standalone" do + let(:config) { Schked::Config.new.tap { |x| x.standalone = false } } + + it "starts rufus scheduler" do + expect_any_instance_of(Rufus::Scheduler).to receive(:join) + expect(Schked::RedisLocker).to receive(:new).and_call_original + expect_any_instance_of(described_class).to receive(:define_extend_lock).and_call_original + + worker.wait + end + end end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 35d056c..ee9e147 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +ENV["RAILS_ENV"] = "test" + require "bundler/setup" require "pry-byebug" require "combustion" diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 875d8fc..0ddbc75 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +ENV["RACK_ENV"] = "test" + require "bundler/setup" require "pry-byebug" require "schked" From da22bf8de2192d070a79079cff7b6eb64dee2673 Mon Sep 17 00:00:00 2001 From: Misha Merkushin Date: Wed, 19 Oct 2022 15:53:45 +0300 Subject: [PATCH 2/2] fix lefthook run cmd --- lefthook-local.dip_example.yml | 2 +- lefthook.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lefthook-local.dip_example.yml b/lefthook-local.dip_example.yml index e8928f6..2b1f70e 100644 --- a/lefthook-local.dip_example.yml +++ b/lefthook-local.dip_example.yml @@ -1,4 +1,4 @@ pre-commit: commands: rubocop: - runner: dip {cmd} + run: dip {cmd} diff --git a/lefthook.yml b/lefthook.yml index ed48839..5207494 100644 --- a/lefthook.yml +++ b/lefthook.yml @@ -3,4 +3,4 @@ pre-commit: rubocop: tags: backend glob: "**/*.rb" - runner: bundle exec standardrb --fix {staged_files} && git add {staged_files} + run: bundle exec standardrb --fix {staged_files} && git add {staged_files}