From 078b3bc87ecb55939c4b35a461e57499c398d6b8 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Thu, 7 Apr 2016 14:32:31 -0400 Subject: [PATCH] Log once off jobs and enqueue on `db:migrate` --- app/jobs/onceoff.rb | 25 +++++++++++++++++++ app/jobs/onceoff/grant_emoji.rb | 6 ++--- app/models/onceoff_log.rb | 2 ++ .../20160407180149_create_onceoff_logs.rb | 10 ++++++++ lib/tasks/db.rake | 6 +++-- 5 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 app/jobs/onceoff.rb create mode 100644 app/models/onceoff_log.rb create mode 100644 db/migrate/20160407180149_create_onceoff_logs.rb diff --git a/app/jobs/onceoff.rb b/app/jobs/onceoff.rb new file mode 100644 index 000000000..79872fc20 --- /dev/null +++ b/app/jobs/onceoff.rb @@ -0,0 +1,25 @@ +class Jobs::Onceoff < Jobs::Base + sidekiq_options retry: false + + def self.name_for(klass) + klass.name.sub(/^Jobs\:\:/, '') + end + + # Pass `force: true` to force it happen again + def execute(args) + job_name = self.class.name_for(self.class) + + if args[:force] || !OnceoffLog.where(job_name: job_name).exists? + execute_onceoff(args) + OnceoffLog.create(job_name: job_name) + end + end + + def self.enqueue_all + ObjectSpace.each_object(Class).select { |klass| klass < self }.each do |klass| + job_name = name_for(klass).underscore.to_sym + Jobs.enqueue(job_name) + end + end + +end diff --git a/app/jobs/onceoff/grant_emoji.rb b/app/jobs/onceoff/grant_emoji.rb index ea01e8802..c5a477040 100644 --- a/app/jobs/onceoff/grant_emoji.rb +++ b/app/jobs/onceoff/grant_emoji.rb @@ -1,10 +1,8 @@ module Jobs - class GrantEmoji < Jobs::Base - sidekiq_options retry: false - - def execute(args) + class GrantEmoji < Jobs::Onceoff + def execute_onceoff(args) to_award = {} Post.secured(Guardian.new).visible.public_posts.find_in_batches(batch_size: 5000) do |group| diff --git a/app/models/onceoff_log.rb b/app/models/onceoff_log.rb new file mode 100644 index 000000000..88e43f19b --- /dev/null +++ b/app/models/onceoff_log.rb @@ -0,0 +1,2 @@ +class OnceoffLog < ActiveRecord::Base +end diff --git a/db/migrate/20160407180149_create_onceoff_logs.rb b/db/migrate/20160407180149_create_onceoff_logs.rb new file mode 100644 index 000000000..0d91643ec --- /dev/null +++ b/db/migrate/20160407180149_create_onceoff_logs.rb @@ -0,0 +1,10 @@ +class CreateOnceoffLogs < ActiveRecord::Migration + def change + create_table :onceoff_logs do |t| + t.string :job_name + t.timestamps null: false + end + + add_index :onceoff_logs, :job_name + end +end diff --git a/lib/tasks/db.rake b/lib/tasks/db.rake index e34ac22d4..7249afa37 100644 --- a/lib/tasks/db.rake +++ b/lib/tasks/db.rake @@ -7,6 +7,8 @@ end task 'db:migrate' => ['environment', 'set_locale'] do SeedFu.seed + Jobs::Onceoff.enqueue_all + SiteSetting.last_vacuum = Time.now.to_i if SiteSetting.last_vacuum == 0 if SiteSetting.vacuum_db_days > 0 && @@ -82,7 +84,7 @@ task 'db:rebuild_indexes' => 'environment' do begin puts index_name User.exec_sql("DROP INDEX public.#{index_name}") - rescue ActiveRecord::StatementInvalid => e + rescue ActiveRecord::StatementInvalid # It's this: # PG::Error: ERROR: cannot drop index category_users_pkey because constraint category_users_pkey on table category_users requires it # HINT: You can drop constraint category_users_pkey on table category_users instead. @@ -94,7 +96,7 @@ task 'db:rebuild_indexes' => 'environment' do index_definitions[table_name].each do |index_def| begin User.exec_sql(index_def) - rescue ActiveRecord::StatementInvalid => e + rescue ActiveRecord::StatementInvalid # Trying to recreate a primary key end end