From 5382ccdd9e07c3284e0d5f9ed8e67de28cd31ddf Mon Sep 17 00:00:00 2001 From: Eric Tsai Date: Wed, 9 Oct 2019 13:57:49 +0800 Subject: [PATCH 1/3] create rake task to remove preview bulletins --- lib/tasks/bulletin_tasks.rake | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/tasks/bulletin_tasks.rake b/lib/tasks/bulletin_tasks.rake index cfe4f33..ea6425e 100644 --- a/lib/tasks/bulletin_tasks.rake +++ b/lib/tasks/bulletin_tasks.rake @@ -1,4 +1,8 @@ -# desc "Explaining what the task does" -# task :bulletin do -# # Task goes here -# end +desc 'Remove duplicated bulletins (announcements) created by preview' + +namespace :bulletin do + task :remove_preview_bulletins do + bulletins = Bulletin.where(is_preview: true) + bulletins.destroy_all + end +end From 585397f32fcf041d51c25fd63d812bdce5e02bd3 Mon Sep 17 00:00:00 2001 From: Eric Tsai Date: Wed, 9 Oct 2019 16:41:17 +0800 Subject: [PATCH 2/3] let rake task use orbit site's environment --- lib/tasks/bulletin_tasks.rake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tasks/bulletin_tasks.rake b/lib/tasks/bulletin_tasks.rake index ea6425e..434204a 100644 --- a/lib/tasks/bulletin_tasks.rake +++ b/lib/tasks/bulletin_tasks.rake @@ -1,7 +1,7 @@ desc 'Remove duplicated bulletins (announcements) created by preview' namespace :bulletin do - task :remove_preview_bulletins do + task :remove_preview_bulletins => [:environment] do bulletins = Bulletin.where(is_preview: true) bulletins.destroy_all end From 7471f40e3f39b0ca96795e302a84b194dd936aa0 Mon Sep 17 00:00:00 2001 From: Eric Tsai Date: Wed, 9 Oct 2019 16:43:37 +0800 Subject: [PATCH 3/3] delete preview bulletins at 02:43 daily, using ruby process rather than cronjob --- announcement.gemspec | 2 ++ config/initializers/scheduler.rb | 9 +++++++++ 2 files changed, 11 insertions(+) create mode 100644 config/initializers/scheduler.rb diff --git a/announcement.gemspec b/announcement.gemspec index 29337cc..90c1f23 100644 --- a/announcement.gemspec +++ b/announcement.gemspec @@ -16,4 +16,6 @@ Gem::Specification.new do |s| s.files = Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.rdoc"] s.test_files = Dir["test/**/*"] + + s.add_dependency "rufus-scheduler", "~> 3.6.0" end diff --git a/config/initializers/scheduler.rb b/config/initializers/scheduler.rb new file mode 100644 index 0000000..24f6666 --- /dev/null +++ b/config/initializers/scheduler.rb @@ -0,0 +1,9 @@ +require 'rufus-scheduler' + +scheduler = Rufus::Scheduler.new + +#return if defined?(Rails::Console) || Rails.env.test? || File.split($0).last == 'rake' + +scheduler.cron '43 2 * * *' do + system('bundle exec rake bulletin:remove_preview_bulletins') +end