Go 实现的定时任务管理

gensword · 2019-08-19 20:25:14 · 2645 次点击 · 大约8小时之前 开始浏览    置顶
这是一个创建于 2019-08-19 20:25:14 的主题,其中的信息可能已经有所发展或是发生改变。

Go 实现的定时任务管理

定时任务 是平常比较常见的需求,linux crontab 只提供分钟级别的任务执行,并且定时任务一多管理起来比较麻烦,还有可能 crontab 文件被误删除,没有备份的话比较悲剧。 因此用 go 实现了一个定时任务管理的项目,通过 Swagger api 来进行定时任务的管理,可以添加、删除、修改和查看定时任务,同时收集定时任务的输出作为日志可以查看,并且可以提供秒级定时任务。 定时任务是基于https://github.com/robfig/cron 这个包来做的。让定时任务管理起来更方便。read me 如下:

Features
  • cron jobs(crud)
  • logs for cron jobs

  • swagger apis

Steps
  1. Cofig your redis mysql http server port log and JWT key .
  2. go run main/main.go -create_table to run the server and create tables in mysql database at first time, go run main/main.go just run the server.
  3. Open your browser and type http://localhost:8888/swagger/index.html in
  4. add a cron job
Need kown
  1. Jobs will stop if you stop the server, and jobs will start which status is 1(1 means run, 0 means stop) when the server restart.

  2. If you want to exec mutiple commands please post params below:

    {
      "command": "cd /data/test;python3 test.py", // mutiple commands split by ';'
      "name": "test1", // job name
      "spec": "*/10 * * * * *", // run every ten seconds
      "status": 1  // 1 means add the job and run, 0 means add the job but not run(only run when status is 1)
    }
    
  3. jobs stored in redis and jobs' logs stored in mysql

  4. cron jobs based on https://github.com/robfig/cron, so make sure you job spec are correct or it will return bad request when you add or update a cron job.Documentation here:https://godoc.org/github.com/robfig/cron

Github here https://github.com/gensword/cronmanager

Go 新手,希望大家多多指点,给予好的意见。


有疑问加站长微信联系(非本文作者)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

2645 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传