diff --git a/.github/workflows/pr-checks.yml b/.github/workflows/pr-checks.yml index fa4a9e9..63bb539 100644 --- a/.github/workflows/pr-checks.yml +++ b/.github/workflows/pr-checks.yml @@ -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 @@ -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/ diff --git a/lib/amigo.rb b/lib/amigo.rb index 3be3a7d..08b00d7 100644 --- a/lib/amigo.rb +++ b/lib/amigo.rb @@ -199,6 +199,7 @@ def _subscriber(event) def register_job(job) self.registered_jobs << job + self.registered_jobs.uniq! end # Start the scheduler. diff --git a/lib/amigo/job.rb b/lib/amigo/job.rb index ef0d505..dd7fda6 100644 --- a/lib/amigo/job.rb +++ b/lib/amigo/job.rb @@ -11,6 +11,7 @@ def self.extended(cls) cls.extend(ClassMethods) cls.pattern = "" cls.include(InstanceMethods) + Amigo.register_job(cls) end module InstanceMethods diff --git a/lib/amigo/scheduled_job.rb b/lib/amigo/scheduled_job.rb index eaa0d21..28443a8 100644 --- a/lib/amigo/scheduled_job.rb +++ b/lib/amigo/scheduled_job.rb @@ -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 diff --git a/spec/amigo/amigo_spec.rb b/spec/amigo/amigo_spec.rb index 9d2549f..364aab0 100644 --- a/spec/amigo/amigo_spec.rb +++ b/spec/amigo/amigo_spec.rb @@ -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) @@ -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