/goalarm

Schedule task to be executed periodically or at specified time.

Primary LanguageGoMIT LicenseMIT

goalarm

Build Status codecov

Run job periodically or at specific time. Job is a function that accept context.Context and return interface{} and error. Job will be executed in different goroutine than the caller. Cancel periodically executed job using cancel function returned from context.WithCancel(context.Background()). For more use case take a look at its tests.

Installation

go get -u github.com/kafji/goalarm

Example

ctx := context.Background()

// Print "hello" in 10 seconds.
goalarm.In(ctx, 10*time.Second, func(ctx context.Context) (interface{}, error) {
	fmt.Println("hello")
	return nil, nil
})

// Print "hello" every 10 seconds.
goalarm.Every(ctx, 10*time.Second, 0, func(ctx context.Context) (interface{}, error) {
	fmt.Println("hello")
	return nil, nil
})

Development

Run test

go test $(go list ./... | grep -v /vendor/)