Giter Site home page Giter Site logo

edwardljh / cron Goto Github PK

View Code? Open in Web Editor NEW

This project forked from lizongying/cron

0.0 0.0 0.0 811 KB

基于时间轮实现的定时任务,更准时,并发性能更高。支持crontab格式或every 1 second|minute|hour|day|month|week格式

Home Page: https://github.com/lizongying/cron

Go 100.00%

cron's Introduction

Cron

基于时间轮实现的定时任务,更准时,并发性能更高。支持crontab格式或every 1 second|minute|hour|day|month|week格式

cron

如果没有crontab格式要求,建议使用simple版本,占用内存更少。 cron-simple-v2

注意:v1.0.0 是基于cron-simple-v2

Features

  • 基于时间轮实现,更准时,并发性能更高。
  • 支持crontab或every 1 second|minute|hour|day|month|week格式
  • 修正执行时间,会在整秒/分开始的时候才执行,所以初次执行会有不到1秒/1分的延时
  • 支持立即或整时执行

Install

go get -u github.com/lizongying/cron

Usage

job field

  • Divisibility: 整时执行,默认false。
  • Callback: 回调方法。

cron options

  • WithSecond 设置时间轮的间隔为秒,即定时任务最小间隔为一秒。此项为非默认设置。
WithSecond() Options
  • WithMinute 设置时间轮的间隔为分钟,即定时任务最小间隔为一分钟。此项为默认设置。
WithMinute() Options
  • WithLogger 设置使用自定义日志
WithLogger(logger Logger) Options
  • WithStdout 设置日志输出到控制台
WithStdout() Options
  • WithDivisibility 设置整点执行
WithDivisibility() Options

run

package main

import (
	"github.com/lizongying/cron"
	"time"
)

func main() {
	begin := time.Now()
	logger := cron.NewLoggerStdout()
	c := cron.New(cron.WithSecond(), cron.WithStdout())
	id := c.MustAddJob("every 3 seconds", func() {
		logger.Info(time.Now().Sub(begin))
	})
	logger.Info("id", id)
	c.MustStart()
	select {}
}

stop

package main

import (
	"github.com/lizongying/cron"
)

func main() {
	c := cron.New()
	c.MustStop()
}

Tips

  • 建议秒级别最大任务控制在4,000,000(Apple M1 Pro, 32 GB))以内,防止任务超时。可能支持更大数量,请自行测试。
  • 经测试,内存和并发性能均优于github.com/robfig/cron,可参考cron-simple

TODO

cron's People

Contributors

lizongying avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.