Skip to content

Commit

Permalink
Merge pull request #32 from bibendi/feature/add-standalone-mode
Browse files Browse the repository at this point in the history
Standalone mode
  • Loading branch information
bibendi authored Oct 19, 2022
2 parents 2544661 + da22bf8 commit e36f15c
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 12 deletions.
2 changes: 0 additions & 2 deletions gemfiles/agnostic.gemfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# frozen_string_literal: true

# This file was generated by Appraisal

source "https://rubygems.org"
Expand Down
2 changes: 0 additions & 2 deletions gemfiles/rails.5.gemfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# frozen_string_literal: true

# This file was generated by Appraisal

source "https://rubygems.org"
Expand Down
2 changes: 0 additions & 2 deletions gemfiles/rails.6.gemfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# frozen_string_literal: true

# This file was generated by Appraisal

source "https://rubygems.org"
Expand Down
2 changes: 1 addition & 1 deletion lefthook-local.dip_example.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pre-commit:
commands:
rubocop:
runner: dip {cmd}
run: dip {cmd}
2 changes: 1 addition & 1 deletion lefthook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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}
11 changes: 9 additions & 2 deletions lib/schked/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ module Schked
class Config
attr_writer :logger,
:do_not_load_root_schedule,
:redis_servers
:redis_servers,
:standalone

def paths
@paths ||= []
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/schked/worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 11 additions & 0 deletions spec/lib/schked/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 14 additions & 0 deletions spec/lib/schked/worker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
2 changes: 2 additions & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

ENV["RAILS_ENV"] = "test"

require "bundler/setup"
require "pry-byebug"
require "combustion"
Expand Down
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

ENV["RACK_ENV"] = "test"

require "bundler/setup"
require "pry-byebug"
require "schked"
Expand Down

0 comments on commit e36f15c

Please sign in to comment.