Class: TimerAbilities::Schedule

Inherits:
Object
  • Object
show all
Includes:
SlackRubyBot::Loggable
Defined in:
src/abilities/timer_abilities.rb

Instance Method Summary collapse

Instance Method Details

#at(time, method, *args, save: true) ⇒ void

This method returns an undefined value.

Schedule an action to be run once at the given time

The action will inherit the context in which the timer was set. This means the current channel, thread and initiating user will all be accessible from inside the action.

Examples:

listen_for 'remind' do
  schedule.at 10.minutes.from_now, :remind, 'something'
end

def remind(about)
  say "10 minutes are up, you need to do #{about}"
end

Parameters:

  • time (Time)

    The time to remind at.

  • method (Symbol)

    The method to call once the time is reached.

  • *args (Object*)

    Any parameters to pass to the method. If save is true, then these will be serialized to YAML (and thus must be serializable).

  • save (Boolean)

    Should this timer be saved to storage? Unsaved timers will disappear if the bot process is restarted. Saved timers will be restored from storage on bot startup and will not be lost.



40
41
42
43
# File 'src/abilities/timer_abilities.rb', line 40

def at(time, method, *args, save: true)
  logger.info "Scheduled #{method}(#{args.inspect}) at #{time}"
  @skill.schedule @context, time, method, args, save: save
end

#cron(time, method, *args, save: false) ⇒ void

This method returns an undefined value.

Schedule an action to be run at every interval described by a cron string

Examples:

on_start do
  in_channel '#random-food' do
    schedule.cron '50 11 * * 1-5', :remind, 'something'
  end
end

def remind(about)
  say "Reminding about #{about}. Will ping again tomorrow at 11:50"
end

Parameters:

  • time (String)

    The cron string.

  • method (Symbol)

    The method to call.

  • *args (Object*)

    Any parameters to pass to the method. If save is true, then these will be serialized to YAML (and thus must be serializable).

  • save (Boolean)

    Should this timer be saved to storage? Be careful saving an interval because multiple ones may be started if you are not careful.



91
92
93
94
# File 'src/abilities/timer_abilities.rb', line 91

def cron(time, method, *args, save: false)
  logger.info "Scheduled #{method}(#{args.inspect}) at cron #{time}"
  @skill.schedule_cron @context, time, method, args, save: save
end

#every(interval, method, *args, save: false) ⇒ void

This method returns an undefined value.

Schedule an action to be run every interval

Examples:

on_start do
  in_channel '#random-food' do
    schedule.every 10.minutes, :remind, 'something'
  end
end

def remind(about)
  say "Reminding about #{about}. Will ping again in 10 minutes"
end

Parameters:

  • interval (Integer)

    The interval. Use the ActiveSupport methods here (e.g. 15.seconds, 10.minutes, 53.hours, etc.)

  • method (Symbol)

    The method to call.

  • *args (Object*)

    Any parameters to pass to the method. If save is true, then these will be serialized to YAML (and thus must be serializable).

  • save (Boolean)

    Should this timer be saved to storage? Be careful saving an interval because multiple ones may be started if you are not careful.



66
67
68
69
# File 'src/abilities/timer_abilities.rb', line 66

def every(interval, method, *args, save: false)
  logger.info "Scheduled #{method}(#{args.inspect}) every #{interval}"
  @skill.shedule_interval @context, interval, method, args, save: save
end