Go Setup
Get started with Schedo in your Go application in minutes.
Installation
Install the Schedo Go client using go get:
go
1go get github.com/schedoio/schedo-go
Basic Usage
Import and initialize the Schedo client with your API key:
go
1package main
2
3import (
4 "log"
5 "github.com/schedoio/schedo-go"
6)
7
8"text-gray-500">// Initialize with your API key
9client, err := schedo.NewClient("YOUR_API_KEY")
10if err != nil {
11 log.Fatal(err)
12}
Scheduling Jobs
Schedule your first job with a cron expression:
go
1"text-gray-500">// Schedule a job to run every day at 2 AM
2job := &schedo.Job{
3 Name: "daily-cleanup",
4 Schedule: "0 2 * * *",
5 Handler: func() error {
6 "text-gray-500">// Your job logic here
7 return cleanupOldRecords()
8 },
9}
10
11"text-gray-500">// Schedule a job to run every 5 minutes
12metrics := &schedo.Job{
13 Name: "check-metrics",
14 Schedule: "*/5 * * * *",
15 Handler: func() error {
16 return checkSystemMetrics()
17 },
18}
19
20"text-gray-500">// Register the jobs
21if err := client.RegisterJob(job); err != nil {
22 log.Fatal(err)
23}
24if err := client.RegisterJob(metrics); err != nil {
25 log.Fatal(err)
26}
Error Handling
Handle job failures and retries:
go
1job := &schedo.Job{
2 Name: "process-orders",
3 Schedule: "0 */2 * * *",
4 MaxRetries: 3,
5 Handler: func(ctx *schedo.Context) error {
6 err := processOrders()
7 if err != nil {
8 ctx.Logger.Error("Failed to process orders:", err)
9 return err "text-gray-500">// Schedo will handle retries
10 }
11 return nil
12 },
13}
Documentation
For more detailed information and advanced features, please refer to our documentation: