Skip to content

Commit

Permalink
WIP enabled/disable jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
Cristian Dotta committed Sep 30, 2016
1 parent d9a1a40 commit 0e44175
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 2 deletions.
4 changes: 4 additions & 0 deletions lib/sidekiq-scheduler/job_presenter.rb
Expand Up @@ -45,6 +45,10 @@ def [](key)
@attributes[key]
end

def enabled?
Sidekiq::Scheduler.job_enabled?(@name)
end

# Builds the presenter instances for the schedule hash
#
# @param schedule_hash [Hash] with the redis schedule
Expand Down
5 changes: 5 additions & 0 deletions lib/sidekiq-scheduler/web.rb
Expand Up @@ -18,6 +18,11 @@ def self.registered(app)
Sidekiq::Scheduler.enqueue_job(schedule)
redirect '/recurring-jobs'
end

app.get '/recurring-jobs/:name/toggle' do
Sidekiq::Scheduler.toggle_job_enabled(params[:name])
redirect '/recurring-jobs'
end
end
end
end
Expand Down
39 changes: 37 additions & 2 deletions lib/sidekiq/scheduler.rb
Expand Up @@ -8,7 +8,7 @@ class Scheduler
extend Sidekiq::Util

REGISTERED_JOBS_THRESHOLD_IN_SECONDS = 24 * 60 * 60
RUFUS_METADATA_KEYS = %w(description at cron every in interval)
RUFUS_METADATA_KEYS = %w(description at cron every in interval enabled)

# We expect rufus jobs to have #params
Rufus::Scheduler::Job.module_eval do
Expand Down Expand Up @@ -339,6 +339,16 @@ def next_times_key
'sidekiq-scheduler:next_times'
end

def toggle_job_enabled(name)
state = schedule_state(name)
state['enabled'] = !job_enabled?(name)
set_schedule_state(name, state)
end

def job_enabled?(name)
schedule_state(name).fetch('enabled', Sidekiq.schedule[name].fetch('enabled', true))
end

private

def new_rufus_scheduler
Expand All @@ -353,14 +363,39 @@ def new_job(name, interval_type, config, args)
opts = { :job => true, :tags => [name] }

rufus_scheduler.send(interval_type, *args, opts) do |job, time|
idempotent_job_enqueue(name, time, sanitize_job_config(config))
idempotent_job_enqueue(name, time, sanitize_job_config(config)) if job_enabled?(name)
end
end

def sanitize_job_config(config)
config.reject { |k, _| RUFUS_METADATA_KEYS.include?(k) }
end

# Returns the Redis's key for saving schedule states.
#
# @return [String] with the key
def schedules_state_key
'sidekiq-scheduler:states'
end

# Retrieves a schedule state
#
# @param name [String] with the schedule's name
# @return [Hash] with the schedule's state
def schedule_state(name)
state = Sidekiq.redis { |r| r.hget(schedules_state_key, name) }

state ? MultiJson.decode(state) : {}
end

# Saves a schedule state
#
# @param name [String] with the schedule's name
# @param name [Hash] with the schedule's state
def set_schedule_state(name, state)
Sidekiq.redis { |r| r.hset(schedules_state_key, name, MultiJson.encode(state)) }
end

end
end
end
1 change: 1 addition & 0 deletions sidekiq-scheduler.gemspec
Expand Up @@ -30,6 +30,7 @@ Gem::Specification.new do |s|
s.add_development_dependency 'rspec'
s.add_development_dependency 'mock_redis', '~> 0'
s.add_development_dependency 'simplecov', '~> 0'
s.add_development_dependency 'byebug', '~> 0'

if RUBY_VERSION >= '2.2.2'
s.add_development_dependency 'activejob'
Expand Down
2 changes: 2 additions & 0 deletions web/locales/en.yml
Expand Up @@ -9,3 +9,5 @@ en:
enqueue_now: Enqueue now
next_time: Next Time
no_next_time: no next execution for this job
disable: Disable
enable: Enable
3 changes: 3 additions & 0 deletions web/views/recurring_jobs.erb
Expand Up @@ -31,6 +31,9 @@
<a class="btn btn-warn btn-xs" href="<%= root_path %>recurring-jobs/<%= URI.escape(job.name) %>/enqueue">
<%= t('enqueue_now') %>
</a>
<a class="btn <%= job.enabled? ? "btn-primary" : "btn-warn"%> btn-xs" href="<%= root_path %>recurring-jobs/<%= URI.escape(job.name) %>/toggle">
<%= job.enabled? ? t('disable') : t('enable') %>
</a>
</td>
</tr>
<% end %>
Expand Down

0 comments on commit 0e44175

Please sign in to comment.