Skip to content
This repository has been archived by the owner on Mar 27, 2021. It is now read-only.

Commit

Permalink
refactor recurring_jobs scheduling & add test_now
Browse files Browse the repository at this point in the history
  • Loading branch information
obi-a committed Jul 9, 2017
1 parent 3cb84ae commit 981e992
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
3 changes: 1 addition & 2 deletions bin/recurring_jobs_ctl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
require config

puts "starting out"
Celluloid.shutdown_timeout=1_000_000
receiver = Ragios::RecurringJobs::Receiver.new
#trap("INT") { puts "Shutting down."; pull.terminate; exit}

trap("INT") { puts "Shutting down."; exit}
receiver.run
end
27 changes: 13 additions & 14 deletions lib/ragios/monitors/manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ def add(options)
monitor_options = options.merge({created_at_: event_time, status_: 'active', type: "monitor"})
validate_monitor(monitor_options)
model.save(monitor_id, monitor_options)
schedule(monitor_id, monitor_options[:every], first_run = true)
publisher.log_event(
schedule(monitor_id, monitor_options[:every], :run_now_and_schedule)
publisher.log_event!(
monitor_id: monitor_id,
event: {"monitor status" => "created"},
state: "create",
Expand Down Expand Up @@ -60,7 +60,7 @@ def stop(monitor_id)
try_monitor(monitor_id) do
unschedule(monitor_id)
!!model.update(monitor_id, status_: "stopped")
!!publisher.log_event(
!!publisher.log_event!(
monitor_id: monitor_id,
event: {"monitor status" => "stopped"},
state: "stopped",
Expand All @@ -76,7 +76,7 @@ def delete(monitor_id)
try_monitor(monitor_id) do
!!model.delete(monitor_id)
unschedule(monitor_id)
!!publisher.log_event(
!!publisher.log_event!(
monitor_id: monitor_id,
event: {"monitor status" => "deleted"},
state: "deleted",
Expand All @@ -100,7 +100,7 @@ def update(monitor_id, options)
validate_monitor(new_monitor)
model.update(monitor_id, options)
reschedule(monitor_id, options[:every]) if options.keys.include?(:every)
publisher.log_event(
publisher.log_event!(
monitor_id: monitor_id,
state: "updated",
event: {"monitor status" => "updated"},
Expand Down Expand Up @@ -129,9 +129,8 @@ def start(monitor_id)
Contract Monitor_id => Bool
def test_now(monitor_id)
try_monitor(monitor_id) do
#monitor = model.find(monitor_id)
#!!perform(generic_monitor(monitor))
puts "ask worker to perform job"
monitor = model.find(monitor_id)
schedule(monitor[:_id], monitor[:every], :trigger_work)
true
end
end
Expand Down Expand Up @@ -190,21 +189,22 @@ def get_current_state(monitor_id)
#check if rufu-scheduler can be rescheduled without being manually stopped & rescheduled
def reschedule(monitor_id, interval)
unschedule(monitor_id)
schedule(monitor_id, interval )
schedule(monitor_id, interval)
end

def schedule(monitor_id, interval, first_run = false)

def schedule(monitor_id, interval, perform = :schedule_and_run_later)
add_to_scheduler({
monitor_id: monitor_id,
interval: interval,
first_run: first_run
perform: perform
})
end

def unschedule(monitor_id)
add_to_scheduler({
monitor_id: monitor_id,
unschedule: true
perform: :unschedule
})
end

Expand All @@ -214,7 +214,6 @@ def add_to_scheduler(options)
pusher.terminate
end


def validate_monitor(options)
!!Ragios::Monitors::GenericMonitor.new(options)
rescue Ragios::PluginTestCommandNotFound,
Expand All @@ -228,7 +227,7 @@ def validate_monitor(options)
private

def log_monitor_start(monitor_id, monitor)
publisher.log_event(
publisher.log_event!(
monitor_id: monitor_id,
event: {"monitor status" => "started"},
state: "started",
Expand Down
5 changes: 3 additions & 2 deletions lib/ragios/recurring_jobs/scheduler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ def trigger_work(options)
)
end

def unschedule(monitor_id)
jobs = find(monitor_id)
def unschedule(options)
jobs = find(options[:monitor_id])
jobs.each do |job|
job.unschedule
end
puts "unscheduling #{jobs.inspect}"
end

def find(monitor_id)
Expand Down

0 comments on commit 981e992

Please sign in to comment.