Log once off jobs and enqueue on `db:migrate`
This commit is contained in:
parent
855f72deb6
commit
078b3bc87e
|
@ -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
|
|
@ -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|
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
class OnceoffLog < ActiveRecord::Base
|
||||||
|
end
|
|
@ -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
|
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue