Skip to content

Commit

Permalink
Simplify the internal API, removing JobWrapper and InlineScheduler (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
bensheldon authored Mar 22, 2020
1 parent 1603a09 commit 8c03271
Show file tree
Hide file tree
Showing 19 changed files with 218 additions and 182 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
# Dependencies
- name: Install missing libs (This is slated to be added to the base image soon!)
run: sudo apt-get -yqq install libpq-dev
- name: Setup Ruby
- name: Set up Ruby
uses: ruby/setup-ruby@v1
- name: Ruby gem cache
uses: actions/cache@v1
Expand Down Expand Up @@ -64,7 +64,7 @@ jobs:
# Dependencies
- name: Install missing libs (This is slated to be added to the base image soon!)
run: sudo apt-get -yqq install libpq-dev
- name: Setup Ruby
- name: Set up Ruby
uses: ruby/setup-ruby@v1
- name: Ruby gem cache
uses: actions/cache@v1
Expand All @@ -81,7 +81,7 @@ jobs:
run: bundle exec appraisal install --path $BUNDLE_PATH
# /Dependencies

- name: Setup test database
- name: Set up test database
run: |
cd spec/dummy
bin/rails db:test:prepare
Expand Down
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
--color
--order random
--require spec_helper
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ GEM
public_suffix (>= 2.0.2, < 5.0)
ast (2.4.0)
builder (3.2.4)
coderay (1.1.2)
concurrent-ruby (1.1.6)
crass (1.0.6)
database_cleaner (1.8.3)
Expand Down Expand Up @@ -125,6 +126,9 @@ GEM
parser (2.7.0.4)
ast (~> 2.4.0)
pg (1.2.2)
pry (0.12.2)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
public_suffix (4.0.3)
rack (2.2.2)
rack-test (1.1.0)
Expand Down Expand Up @@ -216,6 +220,7 @@ DEPENDENCIES
gem-release
github_changelog_generator
good_job!
pry
rspec-rails
rubocop
rubocop-rspec
Expand Down
9 changes: 7 additions & 2 deletions gemfiles/rails_5.1.gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ GIT
PATH
remote: ..
specs:
good_job (0.2.1)
good_job (0.2.2)
concurrent-ruby (>= 1.0.2)
pg (>= 1.0.0)
rails (>= 5.1.0)
Expand Down Expand Up @@ -61,6 +61,7 @@ GEM
arel (8.0.0)
ast (2.4.0)
builder (3.2.4)
coderay (1.1.2)
concurrent-ruby (1.1.6)
crass (1.0.6)
database_cleaner (1.8.3)
Expand Down Expand Up @@ -104,7 +105,10 @@ GEM
parallel (1.19.1)
parser (2.7.0.4)
ast (~> 2.4.0)
pg (1.2.2)
pg (1.2.3)
pry (0.12.2)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
public_suffix (4.0.3)
rack (2.2.2)
rack-test (1.1.0)
Expand Down Expand Up @@ -192,6 +196,7 @@ DEPENDENCIES
gem-release
github_changelog_generator
good_job!
pry
rails (~> 5.1.0)
rspec-rails
rubocop
Expand Down
9 changes: 7 additions & 2 deletions gemfiles/rails_5.2.gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ GIT
PATH
remote: ..
specs:
good_job (0.2.1)
good_job (0.2.2)
concurrent-ruby (>= 1.0.2)
pg (>= 1.0.0)
rails (>= 5.1.0)
Expand Down Expand Up @@ -65,6 +65,7 @@ GEM
arel (9.0.0)
ast (2.4.0)
builder (3.2.4)
coderay (1.1.2)
concurrent-ruby (1.1.6)
crass (1.0.6)
database_cleaner (1.8.3)
Expand Down Expand Up @@ -111,7 +112,10 @@ GEM
parallel (1.19.1)
parser (2.7.0.4)
ast (~> 2.4.0)
pg (1.2.2)
pg (1.2.3)
pry (0.12.2)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
public_suffix (4.0.3)
rack (2.2.2)
rack-test (1.1.0)
Expand Down Expand Up @@ -200,6 +204,7 @@ DEPENDENCIES
gem-release
github_changelog_generator
good_job!
pry
rails (~> 5.2.0)
rspec-rails
rubocop
Expand Down
9 changes: 7 additions & 2 deletions gemfiles/rails_6.0.gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ GIT
PATH
remote: ..
specs:
good_job (0.2.1)
good_job (0.2.2)
concurrent-ruby (>= 1.0.2)
pg (>= 1.0.0)
rails (>= 5.1.0)
Expand Down Expand Up @@ -78,6 +78,7 @@ GEM
public_suffix (>= 2.0.2, < 5.0)
ast (2.4.0)
builder (3.2.4)
coderay (1.1.2)
concurrent-ruby (1.1.6)
crass (1.0.6)
database_cleaner (1.8.3)
Expand Down Expand Up @@ -124,7 +125,10 @@ GEM
parallel (1.19.1)
parser (2.7.0.4)
ast (~> 2.4.0)
pg (1.2.2)
pg (1.2.3)
pry (0.12.2)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
public_suffix (4.0.3)
rack (2.2.2)
rack-test (1.1.0)
Expand Down Expand Up @@ -216,6 +220,7 @@ DEPENDENCIES
gem-release
github_changelog_generator
good_job!
pry
rails (~> 6.0.0)
rspec-rails
rubocop
Expand Down
1 change: 1 addition & 0 deletions good_job.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "database_cleaner"
spec.add_development_dependency "gem-release"
spec.add_development_dependency "github_changelog_generator"
spec.add_development_dependency "pry"
spec.add_development_dependency "rspec-rails"
spec.add_development_dependency "rubocop"
spec.add_development_dependency "rubocop-rspec"
Expand Down
3 changes: 1 addition & 2 deletions lib/good_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
require 'good_job/logging'
require 'good_job/lockable'
require 'good_job/job'
require 'good_job/inline_scheduler'
require "good_job/scheduler"
require "good_job/job_wrapper"
require 'good_job/adapter'
require 'good_job/pg_locks'

module GoodJob
include Logging
Expand Down
37 changes: 16 additions & 21 deletions lib/good_job/adapter.rb
Original file line number Diff line number Diff line change
@@ -1,41 +1,36 @@
module GoodJob
class Adapter
def initialize(options = {})
@options = options
@scheduler = InlineScheduler.new if inline?
def initialize(inline: false)
@inline = inline
end

def enqueue(job)
enqueue_at(job, nil)
def enqueue(active_job)
enqueue_at(active_job, nil)
end

def enqueue_at(job, timestamp)
params = {
queue_name: job.queue_name,
priority: job.priority,
serialized_params: job.serialize,
}
params[:scheduled_at] = Time.at(timestamp) if timestamp
def enqueue_at(active_job, timestamp)
good_job = GoodJob::Job.enqueue(
active_job,
scheduled_at: timestamp ? Time.at(timestamp) : nil,
create_with_advisory_lock: inline?
)

good_job = GoodJob::Job.create(params)
job.provider_job_id = good_job.id

GoodJob.tag_logger do
ActiveSupport::Notifications.instrument("create.good_job", { good_job: good_job, job: job })
@scheduler.enqueue(good_job) if inline?
if inline?
good_job.perform
good_job.advisory_unlock
end

good_job
end

def shutdown(wait: true)
@scheduler&.shutdown(wait: wait)
def shutdown(wait: true) # rubocop:disable Lint/UnusedMethodArgument
nil
end

private

def inline?
@options.fetch(:inline, false)
@inline
end
end
end
10 changes: 0 additions & 10 deletions lib/good_job/inline_scheduler.rb

This file was deleted.

33 changes: 33 additions & 0 deletions lib/good_job/job.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,39 @@
module GoodJob
class Job < ActiveRecord::Base
include Lockable

self.table_name = 'good_jobs'

def self.enqueue(active_job, scheduled_at: nil, create_with_advisory_lock: false)
good_job = nil
ActiveSupport::Notifications.instrument("enqueue_job.good_job", { active_job: active_job, scheduled_at: scheduled_at, create_with_advisory_lock: create_with_advisory_lock }) do |instrument_payload|
good_job = GoodJob::Job.new(
queue_name: active_job.queue_name,
priority: active_job.priority,
serialized_params: active_job.serialize,
scheduled_at: scheduled_at,
create_with_advisory_lock: create_with_advisory_lock
)

instrument_payload[:good_job] = good_job

good_job.save!
active_job.provider_job_id = good_job.id
end

good_job
end

def perform
ActiveSupport::Notifications.instrument("before_perform_job.good_job", { good_job: self })
ActiveSupport::Notifications.instrument("perform_job.good_job", { good_job: self }) do
params = serialized_params.merge(
"provider_job_id" => id
)
ActiveJob::Base.execute(params)

destroy!
end
end
end
end
16 changes: 0 additions & 16 deletions lib/good_job/job_wrapper.rb

This file was deleted.

Loading

0 comments on commit 8c03271

Please sign in to comment.