Skip to content

Commit

Permalink
Add sidekiq_cron:clear task
Browse files Browse the repository at this point in the history
The new task will allow for clearing loaded Sidekiq::Cron tasks
manually. This was happening as part of our load, but also giving the
option to clear without loading.
  • Loading branch information
berfarah committed Jun 14, 2017
1 parent a147a35 commit 671fac5
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 10 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ cache: bundler
gemfile:
- Gemfile
- gemfiles/Gemfile.rails-4.2-stable

services:
- redis-server
17 changes: 10 additions & 7 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
sidekiq-cron-tasks (0.2.0)
sidekiq-cron-tasks (0.3.0)
capistrano (>= 3.4, < 4.0)
capistrano-rails (~> 1.1)
railties (>= 4.1.0, < 5.2)
Expand Down Expand Up @@ -47,16 +47,19 @@ GEM
i18n (~> 0.7)
minitest (~> 5.1)
tzinfo (~> 1.1)
airbrussh (1.2.0)
sshkit (>= 1.6.1, != 1.7.0)
arel (8.0.0)
builder (3.2.3)
capistrano (3.4.1)
capistrano (3.8.1)
airbrussh (>= 1.0.0)
i18n
rake (>= 10.0.0)
sshkit (~> 1.3)
sshkit (>= 1.9.0)
capistrano-bundler (1.2.0)
capistrano (~> 3.1)
sshkit (~> 1.2)
capistrano-rails (1.1.8)
capistrano-rails (1.2.3)
capistrano (~> 3.1)
capistrano-bundler (~> 1.1)
coderay (1.1.1)
Expand All @@ -80,7 +83,7 @@ GEM
minitest (5.10.2)
net-scp (1.2.1)
net-ssh (>= 2.6.5)
net-ssh (3.2.0)
net-ssh (4.1.0)
nio4r (2.0.0)
nokogiri (1.7.2)
mini_portile2 (~> 2.1.0)
Expand Down Expand Up @@ -118,7 +121,7 @@ GEM
thor (>= 0.18.1, < 2.0)
rake (12.0.0)
redis (3.3.3)
rufus-scheduler (3.4.0)
rufus-scheduler (3.4.2)
et-orbi (~> 1.0)
sidekiq (5.0.0)
concurrent-ruby (~> 1.0)
Expand All @@ -136,7 +139,7 @@ GEM
actionpack (>= 4.0)
activesupport (>= 4.0)
sprockets (>= 3.0.0)
sshkit (1.11.4)
sshkit (1.13.1)
net-scp (>= 1.1.2)
net-ssh (>= 2.8.0)
thor (0.19.4)
Expand Down
12 changes: 12 additions & 0 deletions lib/capistrano/tasks/sidekiq_cron.rake
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ namespace :deploy do
after "deploy:updated", "deploy:sidekiq_cron"
end

namespace :sidekiq_cron do
task clear: [:set_rails_env] do
on fetch(:sidekiq_cron_servers) do
within release_path do
with rails_env: fetch(:rails_env) do
execute :rake, "sidekiq_cron:clear"
end
end
end
end
end

namespace :load do
task :defaults do
set :sidekiq_cron_role, fetch(:sidekiq_cron_role, :app)
Expand Down
2 changes: 1 addition & 1 deletion lib/sidekiq/cron/tasks/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Cron
module Tasks
class Railtie < ::Rails::Railtie
rake_tasks do
load "tasks/sidekiq_cron/load.rake"
load "tasks/sidekiq_cron/tasks.rake"
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/sidekiq/cron/tasks/version.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Sidekiq
module Cron
module Tasks
VERSION = '0.2.0'
VERSION = '0.3.0'
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ namespace :sidekiq_cron do
new_hash[prefixed_name] = options
end

Sidekiq::Cron::Job.all.select { |job| job.name =~ /\A#{config.prefix} / }.each(&:destroy)
Rake::Task['sidekiq_cron:clear'].invoke
Sidekiq::Cron::Job.load_from_hash prefixed_hash
end

desc "Clear Sidekiq Cron entries"
task clear: :environment do
prefix = Sidekiq::Cron::Tasks.config.prefix
Sidekiq::Cron::Job.all.select { |job| job.name =~ /\A#{prefix} / }.each(&:destroy)
end
end
26 changes: 26 additions & 0 deletions test/sidekiq/cron/rake_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require "test_helper"
require "minitest/spec"

module Sidekiq::Cron
class RakeTest < ActiveSupport::TestCase
extend Minitest::Spec::DSL

let(:rake) { Rake::Application.new }

%w[sidekiq_cron:load sidekiq_cron:clear].each do |task_name|
before do
Rake.application = rake
Rake.load_rakefile(
GEM_LIB_PATH.join("tasks/sidekiq_cron/tasks.rake").to_s
)
Rake::Task.define_task(:environment)
end

describe task_name do
it "runs" do
rake[task_name].invoke
end
end
end
end
end
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Configure Rails Environment
ENV["RAILS_ENV"] = "test"

GEM_LIB_PATH = Pathname.new(File.expand_path("../../lib", __FILE__))
DUMMY_APP_PATH = Pathname.new(File.expand_path("../../test/dummy", __FILE__))
require DUMMY_APP_PATH.join("config/environment.rb")
require "rails/test_help"
Expand Down

0 comments on commit 671fac5

Please sign in to comment.