planer

package module
v0.0.0-...-ef1db16 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2023 License: MIT Imports: 3 Imported by: 0

README

Planer

内部业务使用到的轻量级时间戳计划任务

1. 介绍

Planer 是一个轻量级基于时间戳的精确计划任务, 可以用于业务的计划任务, 比如 2023-02-22 11:33:12 执行某个任务.

为什么不用 cron 呢?因为普通的年龄 40+ 人类难以理解 cron, 哪怕做好 UI 提示, 还是多个选项让他们选择目标时间比较简单.

2. 使用

超级简单, 最多只需要调五个函数就行...


package main

import (
	"fmt"
	"time"

	"github.com/ClarkQAQ/planer"
)

func main() {
	p := planer.New()
	p.SetWaitDuration(time.Second) // 设置没有任务时的循环时间

	// 添加任务
	p.AddJob(time.Now().Unix()+3, func() {
		fmt.Println("hello world")
	})

	// 启动
	p.Start()
	defer p.Stop() // 停止, 如果有没有执行的任务将会清空

	// 等待
	time.Sleep(time.Second * 5)
}

  1. Benchmark

删除了插入排序改成了读取排序, 插入速度提升了, 读取速度虽慢, 但是对于一个秒级的计划任务来说, 并没影响


[clark@ArchOwO planer]$ go test -benchmem -run=^$ -bench ^Benchmark.*$ github.com/ClarkQAQ/planer
goos: linux
goarch: amd64
pkg: github.com/ClarkQAQ/planer
cpu: AMD Ryzen 7 5800H with Radeon Graphics         
BenchmarkPlaner_AddJob-16       17171708                78.37 ns/op           60 B/op          1 allocs/op
BenchmarkJobs_Insert-16         14351600                85.20 ns/op           58 B/op          1 allocs/op
Benchmark_Pop-16                206814886                5.516 ns/op           0 B/op          0 allocs/op
Benchmark_PopWithInsert-16      11885229               111.2 ns/op            57 B/op          1 allocs/op
PASS
ok      github.com/ClarkQAQ/planer      5.671s

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Job

type Job struct {
	Unix int64
	Job  func()
}

type Jobs

type Jobs struct {
	// contains filtered or unexported fields
}

func (*Jobs) Len

func (j *Jobs) Len() int

func (*Jobs) Less

func (j *Jobs) Less(i, k int) bool

func (*Jobs) Pop

func (j *Jobs) Pop() interface{}

func (*Jobs) Push

func (j *Jobs) Push(x interface{})

func (*Jobs) Swap

func (j *Jobs) Swap(i, k int)

type Planer

type Planer struct {
	// contains filtered or unexported fields
}

func New

func New() *Planer

func (*Planer) AddJob

func (p *Planer) AddJob(unix int64, job func())

func (*Planer) SetWaitDuration

func (p *Planer) SetWaitDuration(d time.Duration)

SetWaitDuration 设置空闲等待时间 SetWaitDuration sets the idle wait time

func (*Planer) Start

func (p *Planer) Start()

func (*Planer) Stop

func (p *Planer) Stop()

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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