Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically register jobs when Job is extended #3

Merged
merged 2 commits into from
Sep 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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