Skip to content

ScheduledTask

Oklahomer edited this page Dec 16, 2017 · 2 revisions

sarah.ScheduledTask interface represents a plugin just like Command, but is executed in a scheduled manner. On its execution, this can return slice of result instance, []*sarah.ScheduledTaskResult, so series of result with different destinations can be returned on one execution. e.g. Send each project's statistical report to each project's chat room on 8:00 a.m.

Any struct that satisfies sarah.ScheduledTask can be fed to sarah.Runner as a sarah.RunnerOption created by sarah.WithScheduledTask. sarah.Runner schedules its execution. When executed task returns more than one sarah.ScheduledTaskResult, corresponding bot's Bot.SendMessage is called to send output to chat service. Using sarah.ScheduledTaskPropsBuilder has more advantages such as live configuration update. See ScheduledTaskPropsBuilder for details.

Simplest form of sarah.ScheduledTask implementation can be as below:

type FixedTimer struct {
}

func (ft *FixedTimer) Identifier() string {
	return "fixed_timer"
}

func (ft *FixedTimer) Execute(context.Context) ([]*sarah.ScheduledTaskResult, error) {
	return []*sarah.ScheduledTaskResult{
		{
			Content: "Good morning!!",
			// When Destination is not given, DefaultDestination is used instead.
			//Destination: slackobject.ChannelID("OUTPUT_SPECIFIC_CHANNEL_ID_COMES_HERE"),
		},
	}, nil
}

func (ft *FixedTimer) DefaultDestination() sarah.OutputDestination {
	return slackobject.ChannelID("CHANNEL_ID_COMES_HERE")
}

func (ft *FixedTimer) Schedule() string {
	return "0 0 7 * * *"
}
Clone this wiki locally