Golang Cron Job Example
Introduction
Automating tasks is a crucial aspect of software development, and cron jobs are a popular way to schedule these tasks. In this tutorial, we’ll show you how to create and manage cron jobs in Golang using the robfig/cron package. We’ll cover everything from installation to running your first scheduled task, along with best practices to ensure your cron jobs run efficiently.
Difference between system cron job and Golang cron job
Aspect
System Cron Job
Golang Cron Job
In this blog we will learn to create the cron job in the Golang using the robfig package:
Golang cron job example using function call
Copy the following program in your main.go file,
package main
import (
"fmt"
"github.com/robfig/cron"
)
func main() {
fmt.Println("Cron Job Example")
// Create a new cron job instance
c := cron.New()
// Add the cron schedule and task to the cron job
c.AddFunc("@every 00h00m10s", GuestGreeting)
// Start the cron job scheduler
c.Start()
// Keep the main program running
select {}
}
func GuestGreeting() {
fmt.Println("Message after 10 seconds")
}
open the main.go directory in the terminal, and run the following command sequentially to import the packages
go mod init
go mod tidy
To run the program , please run the following command
go run main.go
Explanation :
The program imports two packages:
- fmt: For printing output to the console.
- github.com/robfig/cron: A package to create and manage cron jobs in Go.
Main function
- fmt.Println(“Cron Job Example”): Prints a message to the console.
- c := cron.New(): Creates a new instance of a cron job scheduler.
- c.AddFunc(“@every 00h00m10s”, GuestGreeting): Schedules the GuestGreeting function to run every 10 seconds. The schedule format @every 00h00m10s specifies this interval.
- c.Start(): Starts the cron job scheduler, which begins executing scheduled tasks.
- select {}: Keeps the main program running indefinitely. Without this, the program would exit immediately, stopping the cron job scheduler.
GuestGreeting Function:
- This function simply prints “Message after every 10 seconds” to the console. It is the task that gets executed every 10 seconds by the cron job scheduler.
Summary
- The program creates a cron job that runs a specific task GuestGreeting() every 10 seconds.
- The task is to print a greeting message to the console.
- The program keeps running indefinitely to allow the cron job to execute as scheduled.
Golang cron job example using net/http package
Copy the following program in your main.go file,
package main
import (
"fmt"
"io"
"net/http"
"time"
"github.com/robfig/cron"
)
func main() {
// Initialize the cron scheduler
c := InitCronScheduler()
// Defer the stop of the cron scheduler to ensure it stops when main function exits
defer c.Stop()
// Start the HTTP server
StartServer()
}
// InitCronScheduler initializes and starts the cron scheduler
func InitCronScheduler() *cron.Cron {
// Create a new cron instance
c := cron.New()
// Add a cron job that runs every 10 seconds
c.AddFunc("@every 00h00m10s", BackUpLocalDataCall)
// Start the cron scheduler
c.Start()
fmt.Println("Cron scheduler initialized")
return c
}
// BackUpLocalDataCall is the function to be executed by the cron job
func BackUpLocalDataCall() {
resp, err := http.Get("http://localhost:8080")
if err != nil {
fmt.Println("Error while calling the API:", err)
return
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading the response body:", err)
return
}
fmt.Println(string(body))
}
// StartServer starts the HTTP server on port 8080
func StartServer() {
// Set up HTTP server routes
http.HandleFunc("/", BackUpLocalData)
// Start the HTTP server on port 8080
fmt.Println("Starting server on :8080")
if err := http.ListenAndServe(":8080", nil); err != nil {
fmt.Println("Error starting server:", err)
}
}
// BackUpLocalData handles HTTP requests and performs a backup
func BackUpLocalData(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "calling the API after every 10 seconds at %s", time.Now().Format("2006-01-02 15:04:05"))
}
open the main.go directory in the terminal, and run the following command sequentially to import the packages
go mod init
go mod tidy
To run the program , please run the following command
go run main.go
Explanation :
The program makes use of various Packages, including the following:
- fmt: Used for formatted printing.
- io: Used for reading the response body.
- net/http: Used for managing HTTP requests and responses.
- time: Used for functions related to time.
- github.com/robfig/cron: Used for scheduling cron jobs.
Main Function
- The main function serves as the starting point of the program.
- InitCronScheduler is invoked to set up and initiate the cron scheduler.
- A deferred function defer c.Stop() is used to ensure that the cron scheduler halts when the main function concludes.
- StartServer is invoked to initiate the HTTP server.
InitCronScheduler Function
- InitCronScheduler initializes and starts the cron scheduler.
- A new cron instance is created with cron.New().
- A cron job is added with c.AddFunc(“@every 00h00m10s”, BackUpLocalDataCall) to run the BackUpLocalDataCall function every second.
- The cron scheduler is started with c.Start().
- A message is printed to indicate that the cron scheduler is initialized.
- The cron instance is returned.
BackUpLocalDataCall Function
- BackUpLocalDataCall is the function executed by the cron job.
- An HTTP GET request is made to http://localhost:8080 using http.Get.
- If an error occurs, it is printed, and the function returns.
- The response body is read with ioutil.ReadAll.
- If an error occurs while reading the response body, it is printed, and the function returns.
- The response body is printed as a string.
StartServer Function
- The StartServer Function initiates the HTTP server on port 8080.
- Requests to the / route are managed by the BackUpLocalData function using http.HandleFunc.
- The server is launched using http.ListenAndServe(“:8080”, nil).
- Any errors that occur during server startup are printed.
BackUpLocalData Function
- The BackUpLocalData Function handles HTTP requests made to the root path.
- It responds with a message displaying the current time using fmt.Fprintf.
- The current time is formatted in a specific layout using time.Now().Format(“2006-01-02 15:04:05”).
Summary
- To display the time of the backup, the program establishes an HTTP server on port 8080 that responds with a message.
- The cron scheduler, initialized within the program, sends an HTTP GET request to this server endpoint every ten seconds.
- Both the server and the cron scheduler operate concurrently, with the scheduler regularly initiating requests to the server.
In the examples provided above, you’ve seen how cron jobs operate. One might question how to set a cron expression format to schedule tasks at specific intervals or times.
How to set cron syntax ?
The cron expression format used in the robfig cron package (also known as “cron syntax”) allows scheduling tasks based on time and date. Here’s an updated explanation of the format:
A cron expression consists of mandatory five space separated fields representing different time units in orderly manner :
Cron expression format :
Minute (0 - 59)
Hour (0 - 23)
Day of the month (1 - 31)
Month (1 - 12 or JAN - DEC)
Day of the week (0 - 7 or SUN - SAT)
-
The minute parameter specifies the minute of the hour for the task to run, ranging from 0 to 59.
- The hour parameter specifies the hour of the day for the task to run, ranging from 0 to 23.
- The day of the month parameter specifies the day of the month for the task to run, ranging from 1
to 31. - The month parameter specifies the month of the year for the task to run, ranging from 1 to 12, or using the abbreviations JAN to DEC.
- The day of the week parameter specifies the day of the week for the task to run, ranging from 0 to 7, with 0 and 7 representing Sunday.
Field Overview
Field Name
Mandatory ?
Allowed Values
Allowed Special Characters
Special Characters Overview
Character
Description
Example
Meaning
Here are the commonly used cron expression formats:
Expression
Description
For more detailed information, you can refer to the official documentation of the robfig package .
Best Practices
- When dealing with long-running tasks, utilize goroutines and context to prevent blocking the main function.
- Implement monitoring to receive alerts in case of a cron job failure.
- Enable graceful shutdown of your application by stopping the cron scheduler.