Skip to content

Commit

Permalink
Migrate to ActiveJob
Browse files Browse the repository at this point in the history
  • Loading branch information
tf committed Aug 1, 2018
1 parent 6f58a3d commit 9938153
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 48 deletions.
6 changes: 6 additions & 0 deletions app/jobs/cert_watch/application_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module CertWatch
class ApplicationJob < ActiveJob::Base
# Most jobs are safe to ignore if the underlying records are no longer available
discard_on ActiveJob::DeserializationError
end
end
8 changes: 4 additions & 4 deletions app/jobs/cert_watch/install_certificate_job.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module CertWatch
class InstallCertificateJob
extend StateMachineJob
class InstallCertificateJob < CertWatch::ApplicationJob
queue_as :cert_watch

@queue = :cert_watch
include StateMachineJob

def self.perform_with_result(certificate, _options = {})
def perform_with_result(certificate, _options = {})
CertWatch.installer.install(domain: certificate.domain,
provider: certificate.provider,
public_key: certificate.public_key,
Expand Down
8 changes: 4 additions & 4 deletions app/jobs/cert_watch/renew_certificate_job.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module CertWatch
class RenewCertificateJob
extend StateMachineJob
class RenewCertificateJob < CertWatch::ApplicationJob
queue_as :cert_watch

@queue = :cert_watch
include StateMachineJob

def self.perform_with_result(certificate, _options = {})
def perform_with_result(certificate, _options = {})
result = CertWatch.client.renew(certificate.domain)

certificate.attributes = result.slice(:public_key, :private_key, :chain)
Expand Down
6 changes: 3 additions & 3 deletions app/jobs/cert_watch/renew_expiring_certificates_job.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module CertWatch
class RenewExpiringCertificatesJob
@queue = :cert_watch
class RenewExpiringCertificatesJob < CertWatch::ApplicationJob
queue_as :cert_watch

def self.perform
def perform
Certificate
.auto_renewable
.installed
Expand Down
2 changes: 1 addition & 1 deletion cert_watch.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Gem::Specification.new do |s|
s.add_dependency 'state_machine', '~> 1.2'

# Trigger resque jobs with a state machine
s.add_dependency 'state_machine_job', ['>= 0.2', '< 2']
s.add_dependency 'state_machine_job', '~> 3.0'

# Testing framework
s.add_development_dependency 'rspec-rails', '~> 3.7'
Expand Down
4 changes: 2 additions & 2 deletions spec/cert_watch/tasks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
require 'cert_watch/tasks'

require 'support/helpers/doubles'
require 'support/helpers/inline_resque'
require 'support/helpers/perform_jobs'

Rake::Task.define_task(:environment)

RSpec.describe 'tasks', fixture_files: true, inline_resque: true do
RSpec.describe 'tasks', fixture_files: true, perform_jobs: true do
describe 'cert_watch:reinstall:all' do
it 'reinstalls installed certificates' do
create(:certificate, state: 'installed', domain: 'some.example.com')
Expand Down
10 changes: 6 additions & 4 deletions spec/jobs/cert_watch/renew_expiring_certificates_job_spec.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
require 'rails_helper'

require 'support/helpers/perform_jobs'

module CertWatch
RSpec.describe RenewExpiringCertificatesJob do
it 'triggers renewal of installed expiring certificates' do
certificate = create(:certificate, state: 'installed', last_renewed_at: 40.days.ago)
CertWatch.config.renewal_interval = 1.month

RenewExpiringCertificatesJob.perform
RenewExpiringCertificatesJob.perform_now

expect(certificate.reload.state).to eq('renewing')
end
Expand All @@ -15,7 +17,7 @@ module CertWatch
certificate = create(:certificate, state: 'abandoned', last_renewed_at: 40.days.ago)
CertWatch.config.renewal_interval = 1.month

RenewExpiringCertificatesJob.perform
RenewExpiringCertificatesJob.perform_now

expect(certificate.reload.state).to eq('abandoned')
end
Expand All @@ -24,7 +26,7 @@ module CertWatch
certificate = create(:certificate, state: 'installed', last_renewed_at: 10.days.ago)
CertWatch.config.renewal_interval = 1.month

RenewExpiringCertificatesJob.perform
RenewExpiringCertificatesJob.perform_now

expect(certificate.reload.state).to eq('installed')
end
Expand All @@ -35,7 +37,7 @@ module CertWatch
CertWatch.config.renewal_interval = 1.month
CertWatch.config.renewal_batch_size = 1

RenewExpiringCertificatesJob.perform
RenewExpiringCertificatesJob.perform_now

expect(Certificate.where(state: 'renewing').count).to eq(1)
end
Expand Down
4 changes: 2 additions & 2 deletions spec/models/cert_watch/certificate_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
require 'rails_helper'

require 'support/helpers/doubles'
require 'support/helpers/inline_resque'
require 'support/helpers/perform_jobs'

module CertWatch
RSpec.describe Certificate, inline_resque: true do
RSpec.describe Certificate, perform_jobs: true do
describe '.auto_renewable' do
it 'includes auto renewable certificates' do
certificate = create(:certificate, :auto_renewable)
Expand Down
5 changes: 2 additions & 3 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

require 'support/config/factory_girl'
require 'support/config/timecop'
require 'support/config/resque_logger'
require 'support/config/cert_watch'

# Checks for pending migration and applies them before tests are run.
Expand All @@ -31,8 +30,8 @@
# arbitrary gems may also be filtered via:
# config.filter_gems_from_backtrace("gem name")

config.when_first_matching_example_defined(inline_resque: true) do
require 'support/helpers/inline_resque'
config.when_first_matching_example_defined(perform_jobs: true) do
require 'support/helpers/perform_jobs'
end

config.when_first_matching_example_defined(fixture_files: true) do
Expand Down
16 changes: 0 additions & 16 deletions spec/support/config/resque_logger.rb

This file was deleted.

9 changes: 0 additions & 9 deletions spec/support/helpers/inline_resque.rb

This file was deleted.

7 changes: 7 additions & 0 deletions spec/support/helpers/perform_jobs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
RSpec.configure do |config|
config.before(:each) do |example|
ActiveJob::Base.queue_adapter = :test
ActiveJob::Base.queue_adapter.perform_enqueued_jobs =
example.metadata[:perform_jobs]
end
end

0 comments on commit 9938153

Please sign in to comment.