Skip to content

Commit

Permalink
Merge pull request #3 from lithictech/autoregister
Browse files Browse the repository at this point in the history
Automatically register jobs when Job is extended
  • Loading branch information
rgalanakis authored Sep 5, 2022
2 parents c43a485 + e371aff commit 5fb0221
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 9 deletions.
14 changes: 10 additions & 4 deletions .github/workflows/pr-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ jobs:
strategy:
matrix:
ruby-version: ['3.1', '3.0', '2.7']
services:
redis:
image: redis
ports:
- 22379:6379
options: >-
--health-cmd "redis-cli ping"
--health-interval 5s
--health-timeout 5s
--health-retries 5
steps:
- name: Checkout repo
uses: actions/checkout@v2
Expand All @@ -19,11 +29,7 @@ jobs:
with:
ruby-version: ${{ matrix.ruby-version }}
bundler-cache: true
- name: Start backing services
run: docker-compose up -d
- name: Run rubocop
run: bundle exec rubocop
- name: Wait for services to come up
run: sleep 3
- name: Run specs
run: bundle exec rspec spec/
1 change: 1 addition & 0 deletions lib/amigo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ def _subscriber(event)

def register_job(job)
self.registered_jobs << job
self.registered_jobs.uniq!
end

# Start the scheduler.
Expand Down
1 change: 1 addition & 0 deletions lib/amigo/job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def self.extended(cls)
cls.extend(ClassMethods)
cls.pattern = ""
cls.include(InstanceMethods)
Amigo.register_job(cls)
end

module InstanceMethods
Expand Down
1 change: 1 addition & 0 deletions lib/amigo/scheduled_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def self.extended(cls)
cls.extend(ClassMethods)
cls.splay_duration = 30
cls.include(InstanceMethods)
Amigo.register_job(cls)
end

module InstanceMethods
Expand Down
30 changes: 25 additions & 5 deletions spec/amigo/amigo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,6 @@ def _perform(event)
extend Amigo::Job
on "foo"
end

expect(Amigo.registered_jobs).to_not include(job)
Amigo.register_job(job)
expect(Amigo.registered_jobs).to include(job)
expect(Amigo.registered_event_jobs).to include(job)
Expand All @@ -134,18 +132,40 @@ def _perform(event)
cron "*/10 * * * *"
splay 2
end

expect(Amigo.registered_jobs).to_not include(job)
Amigo.register_job(job)
expect(Amigo.registered_jobs).to include(job)
expect(Amigo.registered_event_jobs).to_not include(job)
expect(Amigo.registered_scheduled_jobs).to include(job)
expect(job.cron_expr).to eq("*/10 * * * *")
expect(job.splay_duration).to eq(2)
end

it "is idempotent" do
job = Class.new do
extend Amigo::Job
end
Amigo.register_job(job)
Amigo.register_job(job)
expect(Amigo.registered_jobs.count { |j| j == job }).to eq(1)
end
end

describe "Job" do
it "is automatically registered" do
job = Class.new do
extend Amigo::Job
end
expect(Amigo.registered_jobs).to include(job)
end
end

describe "ScheduledJob" do
it "is automatically registered" do
job = Class.new do
extend Amigo::ScheduledJob
end
expect(Amigo.registered_jobs).to include(job)
end

it "has a default splay of 30s" do
job = Class.new do
extend Amigo::ScheduledJob
Expand Down

0 comments on commit 5fb0221

Please sign in to comment.