Go - Schedule.Cron()

Sets the cron expressions that determines when the schedule triggers and a callback to be triggered.

import (
  "fmt"

  "github.com/nitrictech/go-sdk/handler"
  "github.com/nitrictech/go-sdk/nitric"
)

func main() {
  nitric.NewSchedule("archive-data").Cron("0 1 1 * *", func() {
    // code which archives data
  })

  if err := nitric.Run(); err != nil {
    fmt.Println(err)
  }
}

Parameters

  • Name
    expression
    Required
    Required
    Type
    string
    Description

    The expression that sets when the schedule will be triggered. This value should be a standard 5 value Unix cron expression, e.g., '0 1 1 * *'.

  • Name
    middleware
    Required
    Required
    Type
    ...interface{}
    Description

    One or more callback functions to use as the handler which will run on the defined frequency.

Examples

Create a Schedule

import (
  "fmt"

  "github.com/nitrictech/go-sdk/nitric"
)

func main() {
  // every 15 minutes
  nitric.NewSchedule("check for updates").Cron("0/15 * * * *", func () {
    fmt.Println("checking for updates")
  })

  // at 1:00am on the 1st of every month
  nitric.NewSchedule("delete stale data").Cron("0 1 1 * *", func () {
    fmt.Println("deleting stale data")
  })

  if err := nitric.Run(); err != nil {
    fmt.Println(err)
  }
}

Create a Schedule with multiple middleware/handlers

import (
  "fmt"

  "github.com/nitrictech/go-sdk/nitric/schedules"
  "github.com/nitrictech/go-sdk/nitric"
)

func generateReport(ctx *schedules.Ctx, next nitric.Handler[schedules.Ctx]) (*schedules.Ctx, error) {
  // generate report

  return next(ctx)
}

func sendNotification(ctx *schedules.Ctx, next nitric.Handler[schedules.Ctx]) (*schedules.Ctx, error) {
  // send notification with the report

  return next(ctx)
}

func main() {
  nitric.NewSchedule("check for updates").Cron("0 1 1 * *", generateReport, sendNotification)

  if err := nitric.Run(); err != nil {
    fmt.Println(err)
  }
}