Skip to content

Commit

Permalink
Upgrade GoodJob, pre: 2.0 (#5338)
Browse files Browse the repository at this point in the history
* Rename migration back to default good_job name, used for migration detection

* bin/rails g good_job:update

- Temporarily added a symlink from db/migrate => db/worker_jobs_migrate
  to work around bensheldon/good_job#352

* Patch GoodJob migration to use correct base class
  • Loading branch information
zachmargolis authored Aug 26, 2021
1 parent ac88f62 commit dc13a13
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ gem 'dotiw', '>= 4.0.1'
gem 'exception_notification', '>= 4.4.0'
gem 'faraday'
gem 'foundation_emails'
gem 'good_job'
gem 'good_job', '< 2.0.0'
gem 'hashie', '~> 4.1'
gem 'hiredis', '~> 0.6.0'
gem 'http_accept_language'
Expand Down
6 changes: 3 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ GEM
rake
formatador (0.2.5)
foundation_emails (2.2.1.0)
fugit (1.5.0)
fugit (1.5.1)
et-orbi (~> 1.1, >= 1.1.8)
raabro (~> 1.4)
geocoder (1.6.7)
Expand All @@ -298,7 +298,7 @@ GEM
mail (>= 2.2.1)
gmail_xoauth (0.4.2)
oauth (>= 0.3.6)
good_job (1.12.0)
good_job (1.99.0)
activejob (>= 5.2.0)
activerecord (>= 5.2.0)
concurrent-ruby (>= 1.0.2)
Expand Down Expand Up @@ -726,7 +726,7 @@ DEPENDENCIES
faraday
foundation_emails
gmail (>= 0.7.1)
good_job
good_job (< 2.0.0)
guard-rspec
hashie (~> 4.1)
hiredis (~> 0.6.0)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true
class AddActiveJobIdConcurrencyKeyCronKeyToGoodJobs < ActiveRecord::Migration[5.2]
def change
reversible do |dir|
dir.up do
# Ensure this incremental update migration is idempotent
# with monolithic install migration.
return if connection.column_exists?(:good_jobs, :active_job_id)
end
end

add_column :good_jobs, :active_job_id, :uuid
add_column :good_jobs, :concurrency_key, :text
add_column :good_jobs, :cron_key, :text
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true
class AddActiveJobIdIndexAndConcurrencyKeyIndexToGoodJobs < ActiveRecord::Migration[5.2]
disable_ddl_transaction!

UPDATE_BATCH_SIZE = 1_000

class GoodJobJobs < WorkerJobApplicationRecord
self.table_name = "good_jobs"
end

def change
reversible do |dir|
dir.up do
# Ensure this incremental update migration is idempotent
# with monolithic install migration.
return if connection.index_name_exists?(:good_jobs, :index_good_jobs_on_active_job_id_and_created_at)
end
end

add_index :good_jobs, [:active_job_id, :created_at], algorithm: :concurrently, name: :index_good_jobs_on_active_job_id_and_created_at
add_index :good_jobs, :concurrency_key, where: "(finished_at IS NULL)", algorithm: :concurrently, name: :index_good_jobs_on_concurrency_key_when_unfinished
add_index :good_jobs, [:cron_key, :created_at], algorithm: :concurrently, name: :index_good_jobs_on_cron_key_and_created_at

reversible do |dir|
dir.up do
start_time = Time.current
loop do
break if GoodJobJobs.where(active_job_id: nil, finished_at: nil).where("created_at < ?", start_time).limit(UPDATE_BATCH_SIZE).update_all("active_job_id = (serialized_params->>'job_id')::uuid").zero?
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true
class AddRetriedGoodJobIdToGoodJobs < ActiveRecord::Migration[5.2]
def change
reversible do |dir|
dir.up do
# Ensure this incremental update migration is idempotent
# with monolithic install migration.
return if connection.column_exists?(:good_jobs, :retried_good_job_id)
end
end

add_column :good_jobs, :retried_good_job_id, :uuid
end
end
3 changes: 2 additions & 1 deletion db/worker_jobs_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2021_08_09_201322) do
ActiveRecord::Schema.define(version: 2021_08_26_192729) do

# These are extensions that must be enabled in order to support this database
enable_extension "pgcrypto"
Expand All @@ -29,6 +29,7 @@
t.uuid "active_job_id"
t.text "concurrency_key"
t.text "cron_key"
t.uuid "retried_good_job_id"
t.index ["active_job_id", "created_at"], name: "index_good_jobs_on_active_job_id_and_created_at"
t.index ["concurrency_key"], name: "index_good_jobs_on_concurrency_key_when_unfinished", where: "(finished_at IS NULL)"
t.index ["cron_key", "created_at"], name: "index_good_jobs_on_cron_key_and_created_at"
Expand Down

0 comments on commit dc13a13

Please sign in to comment.