首页 > 其他分享 >go添加一个定时任务

go添加一个定时任务

时间:2023-05-09 22:03:41浏览次数:38  
标签:taskList ok string cron 添加 func go 定时 taskName

package timer

import (
"sync"

"github.com/robfig/cron/v3"
)

type Timer interface {
AddTaskByFunc(taskName string, spec string, task func(), option ...cron.Option) (cron.EntryID, error)
AddTaskByJob(taskName string, spec string, job interface{ Run() }, option ...cron.Option) (cron.EntryID, error)
FindCron(taskName string) (*cron.Cron, bool)
StartTask(taskName string)
StopTask(taskName string)
Remove(taskName string, id int)
Clear(taskName string)
Close()
}

// timer 定时任务管理
type timer struct {
taskList map[string]*cron.Cron
sync.Mutex
}

// AddTaskByFunc 通过函数的方法添加任务
func (t *timer) AddTaskByFunc(taskName string, spec string, task func(), option ...cron.Option) (cron.EntryID, error) {
t.Lock()
defer t.Unlock()
if _, ok := t.taskList[taskName]; !ok {
t.taskList[taskName] = cron.New(option...)
}
id, err := t.taskList[taskName].AddFunc(spec, task)
t.taskList[taskName].Start()
return id, err
}

// AddTaskByJob 通过接口的方法添加任务
func (t *timer) AddTaskByJob(taskName string, spec string, job interface{ Run() }, option ...cron.Option) (cron.EntryID, error) {
t.Lock()
defer t.Unlock()
if _, ok := t.taskList[taskName]; !ok {
t.taskList[taskName] = cron.New(option...)
}
id, err := t.taskList[taskName].AddJob(spec, job)
t.taskList[taskName].Start()
return id, err
}

// FindCron 获取对应taskName的cron 可能会为空
func (t *timer) FindCron(taskName string) (*cron.Cron, bool) {
t.Lock()
defer t.Unlock()
v, ok := t.taskList[taskName]
return v, ok
}

// StartTask 开始任务
func (t *timer) StartTask(taskName string) {
t.Lock()
defer t.Unlock()
if v, ok := t.taskList[taskName]; ok {
v.Start()
}
}

// StopTask 停止任务
func (t *timer) StopTask(taskName string) {
t.Lock()
defer t.Unlock()
if v, ok := t.taskList[taskName]; ok {
v.Stop()
}
}

// Remove 从taskName 删除指定任务
func (t *timer) Remove(taskName string, id int) {
t.Lock()
defer t.Unlock()
if v, ok := t.taskList[taskName]; ok {
v.Remove(cron.EntryID(id))
}
}

// Clear 清除任务
func (t *timer) Clear(taskName string) {
t.Lock()
defer t.Unlock()
if v, ok := t.taskList[taskName]; ok {
v.Stop()
delete(t.taskList, taskName)
}
}

// Close 释放资源
func (t *timer) Close() {
t.Lock()
defer t.Unlock()
for _, v := range t.taskList {
v.Stop()
}
}

func NewTimerTask() Timer {
return &timer{taskList: make(map[string]*cron.Cron)}
}

标签:taskList,ok,string,cron,添加,func,go,定时,taskName
From: https://www.cnblogs.com/cheyunhua/p/17386411.html

相关文章

  • 一道Promise面试题,并对比向其代码中添加await关键字后的变化
    标准代码:(function(){console.log(1);window.setTimeout(()=>{console.log(2);},100);newPromise((resolve)=>{console.log(3);resolve();}).then(()=>{console.log(4);......
  • Nginx+uwsgi+django 搭建web服务器
    1、下载安装相关软件centos环境下安装:安装组件:yumgroupinstall"Developmenttools"yuminstallzlib-develbzip2-develpcre-developenssl-develncurses-develsqlite-develreadline-develtk-devel 安装python:cd~wgethttps://pypi.python.org/packages/source/d......
  • .Net Core 2. VS2022 + Core6.0 + Razor 添加模型
    这里基本是按照微软的文档示例整理的 添加数据模型在项目中新增Models文件夹,用于存放数据模型 新增movie类usingSystem.ComponentModel.DataAnnotations;namespaceStandardCoreStudy.Models{publicclassMovie{///<summary>///ID......
  • Django REST framework创建api
    我们将创建一个简单的允许管理员用户查看和编辑系统中的用户和组的API。项目设置创建一个名为 tutorial 的新django项目,然后启动一个名为 quickstart 的新app。#创建项目目录mkdirtutorialcdtutorial#创建一个virtualenv来隔离我们本地的包依赖关系virtualenvenv......
  • linux定时备份日志文件脚本,重启jar简易脚本
     1.编写脚本文件saveLogs.sh (备份文件到指定位置重命名,然后情况文件继续写入)  cp/opt/zcgl/zcgl.log/opt/zcgl/logs/zcgl-`date+%Y%m%d`.log&  sleep2  >zcgl.log2. 定时脚本crontab 打开设置:crontab-e 输入内容:5923***sh/opt/zcgl/saveLogs.......
  • php:7-cli-apline安装mysql redis mongo扩展模块
    apkadd--no-cachebuild-dependenciesbuild-baseopenssl-devautoconfg++libtoolmakecurl-devlibxml2-devlinux-headersdocker-php-ext-install-j2mysqlidocker-php-ext-installpdo_mysqlpeclinstallmongodb-1.2.2echo"extension=mongodb.so"......
  • Docker中部署mongodb
    1、拉取镜像dockerpullmongo:4.42、创建mongo数据持久化目录(防止容器被删数据库也被删了)mkdir-p/xx/xx/xx3、运行mongodbdockerrun-itd--namemongo-v/xx/xx/xx:/data/db-p27017:27017mongo:4.4--auth-v:将宿主机的/xx/xx/xx映射到容器的/data/db目录......
  • 直播平台软件开发,判断当前时间是否在规定时间内
    直播平台软件开发,判断当前时间是否在规定时间内//判断是否在选择的时间段内使用if(!"".equals(Public.mapTo(couponsEntity.getUseStartTime(),""))){  //存在设置时间区间时  SimpleDateFormatdf=newSimpleDateFormat("HH:mm:ss");//设置日期格式  Datenow=n......
  • linux上使用yum部署mongodb4.2+分片副本
    系统配置Linux是有文件句柄限制的,而且默认不是很高,一般都是1024,作为一台生产服务器,其实很容易就达到这个数量。为防止服务因toomanyopenfiles错误出现宕机,这里需要对linux系统句柄数进行调整。##临时调整,系统重启即失效ulimit-SHn65535##永久调整,编辑/etc/security/limi......
  • golang中xorm自动维护表结构自动导入数据的实现
    Xorm简介Go标准库提供的数据库接口database/sql比较底层,使用它来操作数据库非常繁琐,而且容易出错。因而社区开源了不少第三方库,有各式各样的ORM(ObjectRelationalMapping,对象关系映射库),如gorm和xorm。其中xorm是一个简单但强大的ORM库,使用它可以大大简化我们的数据库操作,笔......