The Go SDK is currently in experimental status. If you would like to provide feedback, please reach out to us with your suggestions and comments on our Discord.
Go - Schedule.Every()
Sets the frequency and one or many handlers to be triggered.
import (
"fmt"
"github.com/nitrictech/go-sdk/handler"
"github.com/nitrictech/go-sdk/nitric"
)
func main() {
// Create a schedule that runs every 3 minutes
nitric.NewSchedule("send reminder").Every("3 minutes", func(ctx *handler.IntervalContext, _ handler.IntervalHandler) (*handler.IntervalContext, error) {
// code which sends a reminder
return ctx, nil
})
if err := nitric.Run(); err != nil {
fmt.Println(err)
}
}
Parameters
- Name
rate
- Required
- Required
- Type
- string
- Description
The rate to run the schedule, e.g., '7 days'. All rates accept a number and a frequency. Valid frequencies are 'days', 'hours', or 'minutes'.
- Name
middleware
- Required
- Required
- Type
- EventMiddleware
- Description
One or more middleware functions to use as the handler which will run on the defined frequency.
Examples
Create a Schedule to run every 3 minutes
import (
"fmt"
"github.com/nitrictech/go-sdk/handler"
"github.com/nitrictech/go-sdk/nitric"
)
func main() {
// Create a schedule that runs every 3 minutes
nitric.NewSchedule("send reminder").Every("3 minutes", func(ctx *handler.IntervalContext, _ handler.IntervalHandler) (*handler.IntervalContext, error) {
// code which sends a reminder
return ctx, nil
})
if err := nitric.Run(); err != nil {
fmt.Println(err)
}
}
Create a Schedule with multiple middleware/handlers
import (
"fmt"
"github.com/nitrictech/go-sdk/handler"
"github.com/nitrictech/go-sdk/nitric"
)
func generateReport(ctx *handler.IntervalContext, next handler.IntervalHandler) (*handler.IntervalContext, error) {
// generate report
return next(ctx)
}
func sendNotification(ctx *handler.IntervalContext, next handler.IntervalHandler) (*handler.IntervalContext, error) {
// send notification with the report
return next(ctx)
}
func main() {
nitric.NewSchedule("aggregate data").Every("3 days", handler.ComposeIntervalMiddleware(generateReport, sendNotification))
if err := nitric.Run(); err != nil {
fmt.Println(err)
}
}