diff --git a/app/jobs/onceoff.rb b/app/jobs/onceoff.rb new file mode 100644 index 00000000000..79872fc2026 --- /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 ea01e8802d4..c5a47704024 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 00000000000..88e43f19b4b --- /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 00000000000..0d91643ec5f --- /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 e34ac22d418..7249afa3770 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