The Schedo Go SDK provides a type-safe and intuitive way to interact with the Schedo API from your Go applications.

Installation

Install the SDK using Go modules:

go get -u github.com/useschedo/golang-sdk

Authentication

First, create a Schedo client with your API key:

import (
    "github.com/useschedo/golang-sdk"
    "github.com/useschedo/golang-sdk/option"
)

client := schedo.NewClient(
    option.WithAPIKey("your_api_key"), // defaults to os.LookupEnv("SCHEDO_API_KEY")
)

Defining a Job

Here’s how to define a new job using the SDK:

package main

import (
    "context"
    "fmt"
    schedo "github.com/useschedo/golang"
)

func main() {
    client := schedo.NewClient(
        option.WithAPIKey("your_api_key"),
    )

    // Define a new job
   err := client.DefineJob(
		"example_job",
		"* * * * *", // Run every minute
		func(ctx *schedo.JobExecutionContext) (string, error) {
			fmt.Println("Executing job...")
			return "Job completed successfully", nil
		},
        schedo.WithTimeout(30*time.Second), // Optional: 30 second timeout
	    schedo.WithMetadata(map[string]any{"category": "reports"}), // Optional: adds metadata
	    schedo.WithBlocking(true), // Optional: makes job execution blocking
	)

	if err != nil {
		fmt.Printf("Failed to define job: %v\n", err)
		return
	}

	// Start listening for job execution events
	sdk.Start()
}

Best Practices

  1. Context Usage: Use job execution context to get more details about the job
    func(ctx *schedo.JobExecutionContext) (string, error) {
         fmt.Println("Execution ID: ", ctx.ExecutionID)
         return "Job completed successfully", nil
    }
    
  2. Error Handling: Always check for errors and handle them appropriately:
    if err != nil {
        log.Printf("Failed to create job: %v", err)
        return
    }