go task scheduling 2 (cron call of linux)

Keywords: Go github

cron format:
Time Sharing Day, Month and Week

Execute every five minutes: / 5 * echo Hello > / TMP / X. log

Execute every 1-5 minutes: 1-5 echo Hello >/tmp/x.log

Execute once every day at 10 o'clock and 22 o'clock: 0.10,22 * echo Hello >/tmp/x.log

Execute every 1-5 minutes: 1-5 echo Hello >/tmp/x.log

Parse(): parsing and checking Cron expressions
Next(): Calculate the next scheduling time based on the current time

package main

import (
    "fmt"
    "github.com/gorhill/cronexpr"
    "time"
)

func main()  {
    var (
        expr *cronexpr.Expression
        err error
        now time.Time
        nextTime time.Time
    )

    //Execute every 5 minutes (this library supports 7 bits, accurate to seconds, years)
    if expr, err = cronexpr.Parse("*/5 * * * * * *"); err != nil {
        fmt.Println(err)
        return
    }

    //current time
    now = time.Now()

    //Next scheduling time
    nextTime = expr.Next(now)

    //Waiting for this timer to expire
    time.AfterFunc(nextTime.Sub(now), func() {
        fmt.Println("Has been dispatched.:", nextTime)
    }) //Next time minus current time

    time.Sleep(10 * time.Second)

}

Call a cron
Output: Scheduled: 2019-04-19 17:00:30+0800 CST

package main

import (
    "fmt"
    "github.com/gorhill/cronexpr"
    "time"
)

//Represent a task
type CronJob struct {
    expr *cronexpr.Expression
    nextTime time.Time //expr.Next(time.Now()) gets the next schedule time
}

func main()  {
    //There needs to be a Scheduling Coordinator that checks all Cron tasks regularly and executes whoever expires.

    var (
        cronJob *CronJob
        expr *cronexpr.Expression
        now time.Time
        scheduleTable map[string] *CronJob //key: Task name,
    )

    scheduleTable = make(map[string]*CronJob)

    //current time
    now = time.Now()

    //Define the first cronjob
    expr = cronexpr.MustParse("*/5 * * * * * *")
    cronJob = &CronJob{
        expr: expr,
        nextTime: expr.Next(now),
    }
    //Tasks are registered with the scheduling table
    scheduleTable["job1"] = cronJob

    //Define the second cronjob
    expr = cronexpr.MustParse("*/5 * * * * * *")
    cronJob = &CronJob{
        expr: expr,
        nextTime: expr.Next(now),
    }
    //Tasks are registered with the scheduling table
    scheduleTable["job2"] = cronJob

    //Start Scheduling Cooperative
    go func() {
        var (
            jobName string
            cronJob *CronJob
            now time.Time
        )
        //Check whether the task schedule is due at regular intervals
        for {
            now = time.Now()
            for jobName, cronJob = range scheduleTable {
                //Determine whether or not it is overdue (if the next scheduling time is earlier than or equal to the current time, it means that it has expired)
                if cronJob.nextTime.Before(now) || cronJob.nextTime.Equal(now) {
                    //Start a consortium to perform this task
                    go func(jobName string) {
                        fmt.Println("implement:", jobName)
                    }(jobName)
                    //Calculate the next scheduling time
                    cronJob.nextTime = cronJob.expr.Next(now)
                    fmt.Println(jobName, "Next execution time:", cronJob.nextTime)
                }
            }

            select {
            //Sleep for 100 milliseconds (don't let it take up too much cpu)
            case <- time.NewTimer(100 * time.Millisecond).C: //It will be readable in 100 milliseconds and returned
            }

        }
    }()

    time.Sleep(100 * time.Second)

}

Call multiple cron s and output at intervals:
Implementation: job1
job1 Next Execution Time: 2019-04-19 17:31:20+0800 CST
job2 Next Execution Time: 2019-04-19 17:31:20+0800 CST
Implementation: job2
Implementation: job1
job1 next execution time: 2019-04-19 17:31:25+0800 CST
Implementation: job2
job2 Next Execution Time: 2019-04-19 17:31:25+0800 CST
Implementation: job1
job1 Next Execution Time: 2019-04-19 17:31:30+0800 CST
job2 Next Execution Time: 2019-04-19 17:31:30+0800 CST
Implementation: job2
......

Posted by PoOP on Fri, 19 Apr 2019 18:00:33 -0700