Log once off jobs and enqueue on db:migrate

This commit is contained in:
Robin Ward 2016-04-07 14:32:31 -04:00
parent 855f72deb6
commit 078b3bc87e
5 changed files with 43 additions and 6 deletions

25
app/jobs/onceoff.rb Normal file
View file

@ -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

View file

@ -1,10 +1,8 @@
module Jobs module Jobs
class GrantEmoji < Jobs::Base class GrantEmoji < Jobs::Onceoff
sidekiq_options retry: false
def execute(args)
def execute_onceoff(args)
to_award = {} to_award = {}
Post.secured(Guardian.new).visible.public_posts.find_in_batches(batch_size: 5000) do |group| Post.secured(Guardian.new).visible.public_posts.find_in_batches(batch_size: 5000) do |group|

View file

@ -0,0 +1,2 @@
class OnceoffLog < ActiveRecord::Base
end

View file

@ -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

View file

@ -7,6 +7,8 @@ end
task 'db:migrate' => ['environment', 'set_locale'] do task 'db:migrate' => ['environment', 'set_locale'] do
SeedFu.seed SeedFu.seed
Jobs::Onceoff.enqueue_all
SiteSetting.last_vacuum = Time.now.to_i if SiteSetting.last_vacuum == 0 SiteSetting.last_vacuum = Time.now.to_i if SiteSetting.last_vacuum == 0
if SiteSetting.vacuum_db_days > 0 && if SiteSetting.vacuum_db_days > 0 &&
@ -82,7 +84,7 @@ task 'db:rebuild_indexes' => 'environment' do
begin begin
puts index_name puts index_name
User.exec_sql("DROP INDEX public.#{index_name}") User.exec_sql("DROP INDEX public.#{index_name}")
rescue ActiveRecord::StatementInvalid => e rescue ActiveRecord::StatementInvalid
# It's this: # It's this:
# PG::Error: ERROR: cannot drop index category_users_pkey because constraint category_users_pkey on table category_users requires it # 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. # 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| index_definitions[table_name].each do |index_def|
begin begin
User.exec_sql(index_def) User.exec_sql(index_def)
rescue ActiveRecord::StatementInvalid => e rescue ActiveRecord::StatementInvalid
# Trying to recreate a primary key # Trying to recreate a primary key
end end
end end