Skip to content

Scheduled

Cron-scheduled workflows using PocketBase's built-in scheduler.

What to notice:

  • Scheduled workflows must accept time.Time as input
  • WithSchedule("0 * * * *") runs the workflow every hour
  • No HTTP endpoint needed — the scheduler triggers it automatically
  • No app.OnServe handler — the app just starts and the schedule runs
go
package main

import (
	"context"
	"log"
	"time"

	"github.com/YakirOren/turbine"
	"github.com/pocketbase/pocketbase"
)

func DoCleanup(ctx context.Context) (int, error) {
	// do cleanup work
	return 42, nil
}

func Cleanup(ctx turbine.Context, scheduledAt time.Time) (string, error) {
	_, err := turbine.Do(ctx, DoCleanup, turbine.WithStepName("cleanup"))
	if err != nil {
		return "", err
	}
	return "cleaned up", nil
}

func main() {
	app := pocketbase.New()

	rt := turbine.Setup(app, turbine.Config{})

	// Run cleanup every hour
	turbine.Register(rt, Cleanup, turbine.WithSchedule("0 * * * *"))

	if err := app.Start(); err != nil {
		log.Fatal(err)
	}
}