Task Scheduling (cronjob)
In this document, we will |explain how to create a cronjob service in Go. The code consists of multiple packages, including the main
package, cmd
package, and user
package. We will go through each package and explain how they contribute to creating the cronjob service.
Package main
(main.go)
The main
package is the entry point of the application. It checks the value of the Service
configuration from the environment and executes the corresponding command. In this case, if the Service
is set to consts.ServiceCronjob
, it calls the CronjobRun()
function from the cmd
package.
package main
import (
"log"
"gitlab.finema.co/finema/dga/dga-uc/uc-backend/cmd"
"gitlab.finema.co/finema/dga/dga-uc/uc-backend/consts"
core "gitlab.finema.co/finema/idin-core"
)
func main() {
// Check the service type from the configuration
serviceType := core.NewEnv().Config().Service
switch serviceType {
case consts.ServiceCronjob:
// Start the Cronjob service
cmd.CronjobRun()
default:
// Service type not supported, log an error and exit
log.Fatal("Service not supported")
}
}
Package cmd
(cmd/cronjob.go)
The cmd
package contains the CronjobRun()
function, which sets up the necessary components for running cron jobs. It initializes the database connection, creates a cron job context, and starts the cron scheduler.
package cmd
import (
"fmt"
"os"
"time"
"gitlab.finema.co/finema/dga/dga-uc/uc-backend/modules/dashboard"
"gitlab.finema.co/finema/dga/dga-uc/uc-backend/modules/user"
core "gitlab.finema.co/finema/idin-core"
)
// CronjobRun is the main function to start the Cronjob service
func CronjobRun() {
env := core.NewEnv()
// Create a new Cronjob context
e := core.NewCronjobContext(&core.CronjobContextOptions{
TimeLocation: time.UTC,
ContextOptions: &core.ContextOptions{
ENV: env,
},
})
// Add user-related Cronjob tasks
user.NewUserCronjob(e)
// Add dashboard-related Cronjob tasks
dashboard.NewDashboardCronjob(e)
// Start the Cronjob service
e.Start()
}
Package user
(modules/user/user.cronjob.go)
The user
package is responsible for defining and scheduling cron jobs related to user management. It provides the NewUserCronjob
function, which accepts a core.ICronjobContext
interface and adds various jobs to it.
package user
import (
core "gitlab.finema.co/finema/idin-core"
)
func NewUserCronjob(e core.ICronjobContext) {
user := &UserController{}
// Add your Cronjob tasks here using e.AddJob() function
// Example tasks:
e.AddJob(e.Job().Every(2).Minutes(), user.MyTask1)
e.AddJob(e.Job().Every(1).Day().At("18:00"), user.MyTask2)
}
The NewUserCronjob
function creates an instance of the UserController
and adds several jobs to the cron job context using the AddJob
method. Each job is scheduled to run at a specific interval or time.
Code flow
To create the cronjob service, follow these steps:
- Set up the project structure and import the required packages.
- Create a
main
package with the entry point function. - In the
main
function, check the value of theService
configuration. - If the
Service
is set toconsts.ServiceCronjob
, call theCronjobRun()
function from thecmd
package. - In the
cmd
package, initialize the necessary components such as the database connection and cron job context. - Create the desired cron jobs in separate packages (e.g.,
user
anddashboard
). - In the
user
package, define theNewUserCronjob
function and add jobs to the cron job context using theAddJob
method. - Customize the scheduling intervals and actions based on your requirements.
- Build and run the application.