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
- Cofig your
redis
mysql
http server port
log
andJWT key
. 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.- Open your browser and type http://localhost:8888/swagger/index.html in
- add a cron job
Need kown
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.
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) }
jobs stored in redis and jobs' logs stored in mysql
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
新手,希望大家多多指点,给予好的意见。
有疑问加站长微信联系(非本文作者)
