DEV Community

Carlos Armando Marcano Vargas
Carlos Armando Marcano Vargas

Posted on • Originally published at carlosmv.hashnode.dev on

How to Schedule Tasks in Go

In this article, we are going to learn how to schedule tasks using the schedule package using examples taken from its documentation.

As its README page says, this package provides a simple scheduler for Go.

It can run a function at a given time, in a given duration, or repeatedly at a given interval.

package main

import (
    "atomicgo.dev/schedule"
    "fmt"
    "time"
)

func main() {
    task := schedule.Every(time.Second, func() bool {
        fmt.Println("1 second is over!")
        return true
    })

    fmt.Println("Some stuff happening...")

    time.Sleep(10 * time.Second)

    task.Stop()
}

Enter fullscreen mode Exit fullscreen mode

In the code above we use the schedule package to print in the console the message "1 second is over!" every second. Then this task stops after 10 seconds.

The function Stop() stops the scheduler.

We execute this program and we should see the following message in the console:


package main

import (
    "fmt"
    "time"

    "atomicgo.dev/schedule"
)

func main() {
    task := schedule.At(time.Now().Add(5*time.Second), func() {
        fmt.Println("5 seconds are over!")
    })

    fmt.Println("Some stuff happening...")

    task.Wait()
}

Enter fullscreen mode Exit fullscreen mode

In this example, the program prints the message "5 seconds are over!" after 5 seconds of the current time.

The Wait() method blocks until the scheduler is stopped. After() and At() will stop automatically after the task is executed.

We execute this program and we should see the following message in the console:

The After() function executes a task after a given duration.

package main

import (
    "fmt"
    "time"

    "atomicgo.dev/schedule"
)

func main() {
    task := schedule.After(5*time.Second, func() {
        fmt.Println("5 seconds are over!")
    })

    fmt.Println("Some stuff happening...")

    task.Wait()
}

Enter fullscreen mode Exit fullscreen mode

In the code above we use the After() function to print the message "5 seconds are over!" after 5 seconds are passed.

We execute this program and we should see the following message in the console:


package main

import (
    "atomicgo.dev/schedule"
    "fmt"
    "time"
)

func main() {
    task := schedule.At(time.Now().Add(5*time.Second), func() {
        fmt.Println("5 seconds are over!")
    })

    fmt.Println("Some stuff happening...")

    fmt.Println(task.IsActive())
    task.Wait()
    fmt.Println(task.IsActive())
}

Enter fullscreen mode Exit fullscreen mode

The IsActive() function returns true if a task is executing and false if not.

We execute this program and we should see the following message in the console:

I think this is a useful function to use when we want to execute a task that depends on whether another task is running or not.

package main

import (
    "atomicgo.dev/schedule"
    "fmt"
    "time"
)

func main() {
    task := schedule.At(time.Now().Add(5*time.Second), func() {
        fmt.Println("5 seconds are over!")
    })

    fmt.Println("Some stuff happening...")

    if task.IsActive() == true {
        fmt.Println("There is a task running")
    }
    task.Wait()

}

Enter fullscreen mode Exit fullscreen mode

In the code above, we check if a task is running and if is true, the program prints the message "There is a task running" in the console.

We execute this program and we should see the following message in the console:

Conclusion

In this article, we discuss how to use some of the functions in the schedule package, like performing tasks at a certain time or tasks repeatedly at a specific interval of time.

This article doesn't cover all the functions and features of the schedule package. To know more about you can read its README page.

Thank you for taking the time to read this article.

If you have any recommendations about other packages, architectures, how to improve my code, my English, or anything; please leave a comment or contact me through Twitter, or LinkedIn.

References

Schedule README

Top comments (0)