Skip to content

ScheduledTaskPropsBuilder

Oklahomer edited this page Dec 16, 2017 · 4 revisions

Its Goal

ScheduledTask wiki page introduced that any implementation of sarah.ScheduledTask interface can be a scheduled task. That is the simplest form of sarah.ScheduledTask creation. Use of sarah.ScheduledTaskPropsBuilder is an alternate to construct sarah.ScheduledTask on the fly. Its goal is...

  • to validate input and create a non-contradicting set of arguments to construct sarah.ScheduledTask
  • to ease sarah.ScheduledTask creation by providing handy builder method without damaging customizability

To use Live Configuration Update, the use of sarah.ScheduledTaskProps returned from ScheduledTaskPropsBuilder.Build is required. This is covered in later section.

Usage

Basic

Here is the simplest form of sarah.ScheduledTaskPropsBuilder usage.

var SlackProps = sarah.NewScheduledTaskPropsBuilder().
	BotType(slack.SLACK).
	Identifier("fixed_timer").
	Func(&timerConfig{}, func(_ context.Context) ([]*sarah.ScheduledTaskResult, error) {
		return []*sarah.ScheduledTaskResult{
			{
				Content:     "Howdy!!",
			},
		}, nil
	}).
 	DefaultDestination("#CHANNEL_ID_COMES_HERE").      
	Schedule("@every 1m").
	MustBuild()

Live Configuration Update

Live Configuration Update feature detects configuration file update event, and applies changes to corresponding configuration struct. During this process, sarah.ScheduledTask is re-built. Since sarah.ScheduledTaskProps represents a non-contradicting set of arguments, sarah.Runner does not have to worry about arguments' inconsistency.

This feature is especially useful to have schedule and output destination settings in a configuration file, and update the settings without bot reboot.

type timerConfig struct {
	ChannelID slackobject.ChannelID `yaml:"channel_id"`
	TaskSchedule string `yaml:"task_schedule"`
}

// timerConfig implements DestinatedConfig to ensure default destination is provided by this configuration struct.
var _ sarah.DestinatedConfig = (*timerConfig)(nil)

// timerConfig implements ScheduledConfig to ensure task schedule is provided by this configuration struct.
var _ sarah.ScheduledConfig = (*timerConfig)(nil)

func (cfg *timerConfig) DefaultDestination() OutputDestination {
	return cfg.ChannelID
}

func (cfg *timerConfig) Schedule() string {
	return cfg.TaskSchedule
}

cfg := &timerConfig{}
var SlackProps = sarah.NewScheduledTaskPropsBuilder().
	BotType(slack.SLACK).
	Identifier("fixed_timer").
	ConfigurableFunc(cfg, func(_ context.Context, config sarah.TaskConfig) ([]*sarah.ScheduledTaskResult, error) {
		return []*sarah.ScheduledTaskResult{
			{
				Content:     "Howdy!!",
				Destination: config.(*timerConfig).DefaultDestination(),
			},
		}, nil
	}).
	MustBuild()

Returned sarah.ScheduledTaskProps

On ScheduledTaskPropsBuilder.Build or ScheduledTaskPropsBuilder.MustBuild, previously given arguments are validated and, if they are valid, an instance that represents non-contradicting set of arguments to construct sarah.ScheduledTask is returned. That is sarah.ScheduledTaskProps. This can be passed to sarah.Runner as sarah.RunnerOption.

var SlackProps = sarah.NewScheduledTaskPropsBuilder().
	BotType(slack.SLACK).
	Identifier("fixed_timer").
	Func(&timerConfig{}, func(_ context.Context) ([]*sarah.ScheduledTaskResult, error) {
		return []*sarah.ScheduledTaskResult{
			{
				Content:     "Howdy!!",
			},
		}, nil
	}).
 	DefaultDestination("#CHANNEL_ID_COMES_HERE").      
	Schedule("@every 1m").
	MustBuild()

runner, _ := sarah.NewRunner(config, sarah.WithScheduledTaskProps(props))
runner.Run(context.TODO())