SvelteKit
GitHub
Back

Build a CRON job with GoCron

Introduction

Traditionally a cron is a job scheduler that allows the user to run "jobs" at specifically timed intervals. The most common application for creating cron-Jobs is scheduling repetitive tasks .

In this post we'll go over setting up a simple cron using the go-co-op/gocron package and logging a message to our log file. This sets up the ground work for adding actual jobs into the cron later down the line.

Setup & Go

First things first let's set up our new project.

$ mkdir examplecron && cd examplecron

Now that we have a project directory, create a main.go file in the root of that directory so we have a place to write our code.

As a personal preference, I create my main.go and then follow it with initializing go-mod

$ go mod init

After creating a main.go we need to install a reference to the gocron package to be able to use it in our project.

$ go get github.com/go-co-op/gocron

Now we can start writing some code. Let's begin by setting up our initial go project by declaring a package and initiating a main function

package main

func main() {
}

Now create a function to simple print to console for now so we can test our progress so far. In this case I'll call the function printOut and it will take a single string as the parameter.

package main

import "fmt"

func printOut(msg string) {
    fmt.Println(msg)
}

func main() {
   printOut("Hello, world!")
}

Run the program!

$ go run main.go
>> Hello, world!

Nice, we haven't broken anything yet.. At last we can pull in gocron and create a cron job that can run out printOut() function for us on a schedule. I'll make note of each step below.

package main

import (
    "fmt"
    "time"
    "github.com/go-co-op/gocron" // 1
)

func printOut(msg string) {
    fmt.Println(msg)
}

func runJobs() {
    s := gocron.NewScheduler(time.UTC) // 2

    s.Every(1).Minutes().Do(func() { // 3
        printOut("Hello, world!")  // 4
    }

    s.StartBlocking() // 5
}

func main() {
    runJobs() // 6
}

Alright! For simplicity I've numbered the actions so I can describe them more in-depth below.

  1. Here we import the gocron library for use in our code
  2. We ask gocron to create a new scheduler. The s variable here will hold onto the scheduler so we can use it in the future to create our scheduled jobs. Into the scheduler we pass time.UTC which is a reference to *time.Location to tell the scheduler the time zone associated with our time.
  3. We declare our cron task here. More specifically we tell our scheduler to do something every 1 minute.
  4. This is where the execution lives, or the action we want our cron to perform within the interval we requested. In this case we want it to run our printOut function.
  5. This line starts the scheduler in non-blocking mode which blocks the current execution path.
  6. Here we'll execute our runJobs function which will start our cron.

And that's it!

If we run our program at this point we should see "Hello, World!" print out every minute in our terminal.

$ go run main.go
>> Hello, World!
>> Hello, World!
>> Hello, World!

We have successfully created and scheduled a cron job using Go and the gocron library! There are a handful other libraries to take into consideration, but this one has worked for me in many cases. Thanks for reading!