Skip to content

Instantly share code, notes, and snippets.

@jmettraux
Created June 20, 2017 20:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jmettraux/230aa2e06507a457455a63ad04487481 to your computer and use it in GitHub Desktop.
Save jmettraux/230aa2e06507a457455a63ad04487481 to your computer and use it in GitHub Desktop.
to help Ragesh
require 'rufus-scheduler'
SCHEDULER = Rufus::Scheduler.new
# in this example, the scheduler is placed in a "global constant"
def help
puts
puts "in {interval} \"message\" ~~> print message after {interval}"
puts "every {interval} \"message\" ~~> print message every {interval}"
puts "cron {cron string} \"message\" ~~> print message along {cron string}"
puts
puts "unschedule {job id} ~~> unschedule job"
puts
puts "exit ~~> leave this shell"
puts
end
def schedule(cmd, tim, msg)
SCHEDULER.send(cmd, tim) do |job|
now = Time.now
now = "#{now.strftime('%Y-%m-%d %H:%M:%S')}.#{sprintf('%06d', now.usec)}"
$stdout.puts("#{now} : #{msg.inspect} : (#{job.id})"); $stdout.flush
end
end
def unschedule(_, job_id, _)
SCHEDULER.unschedule(job_id)
nil
end
loop do
print "rufus-scheduler-shell> "
t = $stdin.gets; break unless t
t = t.strip
if t == 'exit'; break; end
if t == 'help'; help; next; end
if t == ''; next; end
m = t.match(/\A([^\s]+)\s+([^"]+)(?:\s*"([^"]+))?/)
cmd, tim, msg = m ? m[1, 3] : nil
tim = tim.strip if tim
msg = msg.strip if msg
if cmd == nil; puts "invalid command, try \"help\""; next; end
mth =
case cmd
when 'in', 'every', 'cron' then 'schedule'
else cmd
end
begin
job_id = send(mth, cmd, tim, msg)
puts " --> job id: #{job_id}" if job_id
rescue NoMethodError
puts "invalid command #{cmd.inspect}, try \"help\""
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment