ConcurrencyCron

package module
v0.0.0-...-4053c76 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 23, 2019 License: BSD-2-Clause Imports: 12 Imported by: 0

README

ConcurrentCron: A Golang Job Scheduling Package.

GgoDoc Go ReportCard

ConcurrentCron is a task scheduler that supports high concurrency at the same time which lets you run Go functions periodically at pre-determined interval using a simple, human-friendly syntax.

You can run this scheduler in the following way

package main

import (
	"ConcurrencyCron"
	"context"
	"fmt"
	"github.com/gin-gonic/gin"
	"os"
	"time"
)

var (
	scheduler ConcurrencyCron.Scheduler
)

func test(num string) {
	//fmt.Println("before:im a task:", num)
	//time.Sleep(10 * time.Second)
	fmt.Println("after:im a task:", num, " current time:", time.Now().Format("15:04"))
}

func init() {
	var err error
	ConcurrencyCron.DefaultWriter = os.Stdout
	scheduler, err = ConcurrencyCron.NewScheduler(200)
	if err != nil {
		fmt.Println(err)
	}
	ctx, _ := context.WithCancel(context.Background())
	scheduler.Every(1).Minutes().Do(test, time.Now().Format("15:04"))
	scheduler.Start(ctx)
}

func main() {
	r := gin.Default()
	r.PUT("/addOnce", func(c *gin.Context) {
		tm := time.Now()
		hour := tm.Hour()
		min := tm.Minute() + 1
		tim := fmt.Sprintf("%2d:%2d", hour, min)
		uuid := scheduler.Once().At(tim).Do(test, tim)
		fmt.Println(uuid)
		c.String(200, uuid)
	})
	r.PUT("/addInterval", func(c *gin.Context) {

	})
	r.DELETE("/removeOnce/:uuid", func(c *gin.Context) {
		uuid := c.Param("uuid")
		scheduler.RemoveByUuid(uuid)
	})
	r.Run(":12315")
	ch := make(chan bool)
	<-ch //test
}

This article refers to some of jasonlvhit/gocron's ideas and things, the specific timing tasks are the same as gocron, you can refer to his project

Thank you for the support and understanding ,jasonlvhit!

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DebugMode = false
View Source
var DefaultWriter io.Writer = os.Stdout
View Source
var MAX_POOL_CAPITION uint32 = 10000

*

*@author  wxn
*@project ConcurrencyCron
*@package ConcurrencyCron
*@date    19-8-2 上午10:02

Functions

func SetMaxConcurrent

func SetMaxConcurrent(max uint32) (err error)

Set the maximum number of concurrent

Types

type Scheduler

type Scheduler interface {
	Every(interval uint64) TasksPool
	Once() TasksPool
	Start(ctx context.Context)
	Stop()
	RemoveByUuid(uuid string)
	GetCurrentTicketsNum() uint32
	GetTaskNum() int
	ListTasks() interface{}
}

func NewScheduler

func NewScheduler(threads uint32) (Scheduler, error)

type TasksPool

type TasksPool interface {
	At(tm string) *task                                    //Run at some times
	Seconds() *task                                        //Run every few seconds
	Minutes() *task                                        //Run every few minutes
	Hours() *task                                          //Run every few hours
	Days() *task                                           //Run every few days
	Weekdays() *task                                       //Run every few weeks
	Monday() *task                                         //Run every few weeks on Monday
	Tuesday() *task                                        //Run every few weeks on Tuesday
	Wednesday() *task                                      //Run every few weeks on Wednesday
	Thursday() *task                                       //Run every few weeks on Thursday
	Friday() *task                                         //Run every few weeks on Friday
	Saturday() *task                                       //Run every few weeks on Saturday
	Sunday() *task                                         //Run every few weeks on Sunday
	JudgeRun(tm time.Time) bool                            //Determine if it is going to run
	Run(ticket TicketsPool, tm time.Time)                  //Run and judge the next run time
	GetNext() time.Time                                    //Get nest run time
	Do(taskFunc interface{}, params ...interface{}) string //Add a run function
	GetUuid() string                                       //get uuid
	Done() bool                                            //get once job done
	Once() bool
	GetFunInfo() string
}

*

*@author  wxn
*@project ConcurrencyCron
*@package ConcurrencyCron
*@date    19-8-2 上午11:23

func NewOnceTask

func NewOnceTask(writer io.Writer) TasksPool

func NewTask

func NewTask(interval uint64, writer io.Writer) TasksPool

create a task

type TicketsPool

type TicketsPool interface {
	//take a ticket
	Take()
	//return a ticket
	Return()
	//get the ticket pools' status
	Active() bool
	//total tickets
	Total() uint32
	//remain tickets
	Remain() uint32
	//close pool
	Close()
}

Control the number of concurrent ticket pools

func NewTicketsPool

func NewTicketsPool(total uint32) (TicketsPool, error)

create a tickets pool

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL