Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to Implement Cron Job/Execute tasks at a specific time In Go

This golang tutorial help to create schedule a jon to execute at specific time interval.This is very helpful feature to run a process or tasks as background, that can be a job that pull data from third party server, sync data between two server, send an email notification at a specific inerval.

I will use golang Cron v2 package to schedule job in golang application.This package cron implements a cron spec parser and job runner.You can schedule job/tasks at the given time interval.

Field name   | Mandatory? | Allowed values  
----------   | ---------- | -------------- 
Seconds      | Yes        | 0-59            
Minutes      | Yes        | 0-59            
Hours        | Yes        | 0-23            
Day of month | Yes        | 1-31            
Month        | Yes        | 1-12 or JAN-DEC
Day of week  | Yes        | 0-6 or SUN-SAT

We will create a fucntiona that will run every 1 hour interval, Lets start to create simple golang application.We will import cron ve packages at the top server.go file,
gopkg.in/robfig/cron.v2

We will run below go command to import packages into scr folder,
go get

We will create cron instance into server.go file,

//renew token
c := cron.New()
c.AddFunc("@every 1h", Test)
c.Start()

We will create Test method into file,

func Test() {
   fmt.Println('i was call')
}

So now on each hour, the Test() method will call and display message 'i was call' into command line.

The post How to Implement Cron Job/Execute tasks at a specific time In Go appeared first on Rest Api Example.



This post first appeared on Rest Api Example, please read the originial post: here

Share the post

How to Implement Cron Job/Execute tasks at a specific time In Go

×

Subscribe to Rest Api Example

Get updates delivered right to your inbox!

Thank you for your subscription

×