diff --git a/.gitignore b/.gitignore index ab76740d3..cc931ff04 100644 --- a/.gitignore +++ b/.gitignore @@ -30,10 +30,12 @@ data conf profile/* public/resource/javascript/vue.js -gocron +/gocron gocron.exe -gocron-node +/gocron-node gocron-node.exe +/bin +/packages node_modules package.json \ No newline at end of file diff --git a/build.sh b/build.sh deleted file mode 100644 index cb92a3d72..000000000 --- a/build.sh +++ /dev/null @@ -1,114 +0,0 @@ -#!/usr/bin/env bash - -# set -x -u -# 构建应用, 生成压缩包 gocron.zip或gocron.tar.gz -# ./build.sh -p windows -a amd64 -v 1.4 -# 参数含义 -# -p 指定平台(windows|linux|darwin) -# -a 指定体系架构(amd64|386), 默认amd64 -# -v 版本号 - -TEMP_DIR=`date +%s`-temp-`echo $RANDOM` - -# 目标平台 windows,linux,darwin -OS='' -# 目标平台架构 -ARCH='' -# 应用名称 -APP_NAME='gocron' -# 版本号 -VERSION='' -# 可执行文件名 -EXEC_NAME='' -# 压缩包名称 -COMPRESS_FILE='' - - -# -p 平台 -a 架构 -while getopts "p:a:v:" OPT; -do - case $OPT in - p) OS=$OPTARG - ;; - a) ARCH=$OPTARG - ;; - v) VERSION=$OPTARG - ;; - esac -done - -if [[ -z $OS ]];then - echo "平台不能为空" - exit 1 -fi - -if [[ $OS != 'windows' && $OS != 'linux' && $OS != 'darwin' ]];then - echo '平台错误,支持的平台 windows linux darmin(osx)' - exit 1 -fi - -if [[ -z $ARCH ]];then - ARCH='amd64' -fi - -if [[ $ARCH != '386' && $ARCH != 'amd64' ]];then - echo 'arch错误,仅支持 386 amd64' - exit 1 -fi - -if [[ -z $VERSION ]];then - echo '版本号不能为空' - exit 1 -fi - -echo '开始编译调度器' -if [[ $OS = 'windows' ]];then - CGO_ENABLED=0 GOOS=$OS GOARCH=$ARCH go build -tags gocron -ldflags '-w' -else - CGO_ENABLED=0 GOOS=$OS GOARCH=$ARCH go build -tags gocron -ldflags '-w' -fi - -if [[ $? != 0 ]];then - exit 1 -fi -echo '编译完成' - -if [[ $OS = 'windows' ]];then - EXEC_NAME=${APP_NAME}.exe - COMPRESS_FILE=${APP_NAME}-v${VERSION}-${OS}-${ARCH}.zip -else - EXEC_NAME=${APP_NAME} - COMPRESS_FILE=${APP_NAME}-v${VERSION}-${OS}-${ARCH}.tar.gz -fi - -mkdir -p $TEMP_DIR/$APP_NAME -if [[ $? != 0 ]]; then - exit 1 -fi - -# 需要打包的文件 -PACKAGE_FILENAME=(public templates ${EXEC_NAME}) - -echo '复制文件到临时目录' -# 复制文件到临时目录 -for i in ${PACKAGE_FILENAME[*]} -do - cp -r $i $TEMP_DIR/$APP_NAME -done - -echo '压缩文件' -# 压缩文件 -cd $TEMP_DIR -if [[ $OS = 'windows' ]];then - zip -rq $COMPRESS_FILE * -else - tar czf $COMPRESS_FILE * -fi -mv $COMPRESS_FILE ../ -cd ../ - -rm $EXEC_NAME -rm -rf $TEMP_DIR - -echo '打包完成' -echo '生成压缩文件--' $COMPRESS_FILE diff --git a/build_node.sh b/build_node.sh deleted file mode 100644 index 4521997fa..000000000 --- a/build_node.sh +++ /dev/null @@ -1,93 +0,0 @@ -#!/usr/bin/env bash - -# set -x -u -# 任务节点打包, 生成压缩包 gocron-node.zip或gocron-node.tar.gz -# ./build-node.sh -p windows -a amd64 -v 1.4 -# 参数含义 -# -p 指定平台(windows|linux|darwin) -# -a 指定体系架构(amd64|386), 默认amd64 -# -v 版本号 - - -# 目标平台 windows,linux,darwin -OS='' -# 目标平台架构 -ARCH='' -# 应用名称 -APP_NAME='gocron-node' -# 版本号 -VERSION='' -# 可执行文件名 -EXEC_NAME='' -# 压缩包名称 -COMPRESS_FILE='' - - -# -p 平台 -a 架构 -while getopts "p:a:v:" OPT; -do - case $OPT in - p) OS=$OPTARG - ;; - a) ARCH=$OPTARG - ;; - v) VERSION=$OPTARG - ;; - esac -done - -if [[ -z $OS ]];then - echo "平台不能为空" - exit 1 -fi - -if [[ $OS != 'windows' && $OS != 'linux' && $OS != 'darwin' ]];then - echo '平台错误,支持的平台 windows linux darmin(osx)' - exit 1 -fi - -if [[ -z $ARCH ]];then - ARCH='amd64' -fi - -if [[ $ARCH != '386' && $ARCH != 'amd64' ]];then - echo 'arch错误,仅支持 386 amd64' - exit 1 -fi - -if [[ -z $VERSION ]];then - echo '版本号不能为空' - exit 1 -fi - -if [[ $OS = 'windows' ]];then - EXEC_NAME=${APP_NAME}.exe - COMPRESS_FILE=${APP_NAME}-v${VERSION}-${OS}-${ARCH}.zip -else - EXEC_NAME=${APP_NAME} - COMPRESS_FILE=${APP_NAME}-v${VERSION}-${OS}-${ARCH}.tar.gz -fi - -echo '开始编译任务节点' -if [[ $OS = 'windows' ]];then - CGO_ENABLED=0 GOOS=$OS GOARCH=$ARCH go build -tags node -ldflags '-w' -o $EXEC_NAME -else - CGO_ENABLED=0 GOOS=$OS GOARCH=$ARCH go build -tags node -ldflags '-w' -o $EXEC_NAME -fi - -if [[ $? != 0 ]];then - exit 1 -fi -echo '编译完成' - -if [[ $OS = 'windows' ]];then - zip -rq $COMPRESS_FILE $EXEC_NAME -else - tar czf $COMPRESS_FILE $EXEC_NAME -fi - - -rm $EXEC_NAME - -echo '打包完成' -echo '生成压缩文件--' $COMPRESS_FILE diff --git a/cmd/web.go b/cmd/gocron/gocron.go similarity index 79% rename from cmd/web.go rename to cmd/gocron/gocron.go index 724d99c2d..8d7a1a4f6 100644 --- a/cmd/web.go +++ b/cmd/gocron/gocron.go @@ -1,4 +1,6 @@ -package cmd +// main gocron + +package main import ( "os" @@ -15,30 +17,47 @@ import ( "gopkg.in/macaron.v1" ) +var AppVersion = "1.4" + // web服务器默认端口 const DefaultPort = 5920 -var CmdWeb = cli.Command{ - Name: "web", - Usage: "run web server", - Action: runWeb, - Flags: []cli.Flag{ - cli.StringFlag{ - Name: "host", - Value: "0.0.0.0", - Usage: "bind host", - }, - cli.IntFlag{ - Name: "port,p", - Value: DefaultPort, - Usage: "bind port", - }, - cli.StringFlag{ - Name: "env,e", - Value: "prod", - Usage: "runtime environment, dev|test|prod", +func main() { + cliApp := cli.NewApp() + cliApp.Name = "gocron" + cliApp.Usage = "gocron service" + cliApp.Version = AppVersion + cliApp.Commands = getCommands() + cliApp.Flags = append(cliApp.Flags, []cli.Flag{}...) + cliApp.Run(os.Args) +} + +// getCommands +func getCommands() []cli.Command { + command := cli.Command{ + Name: "web", + Usage: "run web server", + Action: runWeb, + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "host", + Value: "0.0.0.0", + Usage: "bind host", + }, + cli.IntFlag{ + Name: "port,p", + Value: DefaultPort, + Usage: "bind port", + }, + cli.StringFlag{ + Name: "env,e", + Value: "prod", + Usage: "runtime environment, dev|test|prod", + }, }, - }, + } + + return []cli.Command{command} } func runWeb(ctx *cli.Context) { diff --git a/gocron-node.go b/cmd/node/node.go similarity index 96% rename from gocron-node.go rename to cmd/node/node.go index c53c9f061..57c115c8e 100644 --- a/gocron-node.go +++ b/cmd/node/node.go @@ -1,20 +1,20 @@ -// +build node -// 任务节点 +// main gocron-node package main import ( "flag" "fmt" - "github.com/ouqiang/gocron/modules/rpc/auth" - "github.com/ouqiang/gocron/modules/rpc/server" - "github.com/ouqiang/gocron/modules/utils" "os" "runtime" "strings" + + "github.com/ouqiang/gocron/modules/rpc/auth" + "github.com/ouqiang/gocron/modules/rpc/server" + "github.com/ouqiang/gocron/modules/utils" ) -const AppVersion = "1.3" +var AppVersion = "1.4" func main() { var serverAddr string diff --git a/gocron.go b/gocron.go deleted file mode 100644 index 51f1640c3..000000000 --- a/gocron.go +++ /dev/null @@ -1,25 +0,0 @@ -// +build gocron -// 调度中心 - -package main - -import ( - "github.com/urfave/cli" - "os" - - "github.com/ouqiang/gocron/cmd" -) - -const AppVersion = "1.3" - -func main() { - app := cli.NewApp() - app.Name = "gocron" - app.Usage = "gocron service" - app.Version = AppVersion - app.Commands = []cli.Command{ - cmd.CmdWeb, - } - app.Flags = append(app.Flags, []cli.Flag{}...) - app.Run(os.Args) -} diff --git a/makefile b/makefile new file mode 100644 index 000000000..033f379ae --- /dev/null +++ b/makefile @@ -0,0 +1,18 @@ + +all: build + +build: gocron node + +gocron: + + go build -o bin/gocron ./cmd/gocron + + +node: + + go build -o bin/gocron-node ./cmd/node + +clean: + + rm bin/gocron + rm bin/gocron-node \ No newline at end of file diff --git a/makefile.cross-compiles b/makefile.cross-compiles new file mode 100644 index 000000000..b5e47bcfb --- /dev/null +++ b/makefile.cross-compiles @@ -0,0 +1,33 @@ + +LDFLAGS = -w -s -X main.AppVersion=${VERSION} + + +all: build + + +build: gocron node + + +gocron: + + env CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -ldflags "$(LDFLAGS)" -o ./gocron_darwin_amd64/gocron ./cmd/gocron + env CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "$(LDFLAGS)" -o ./gocron_linux_amd64/gocron ./cmd/gocron + env CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -ldflags "$(LDFLAGS)" -o ./gocron_windows_amd64/gocron.exe ./cmd/gocron + +node: + + env CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -ldflags "$(LDFLAGS)" -o ./gocron-node_darwin_amd64/gocron-node ./cmd/node + env CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "$(LDFLAGS)" -o ./gocron-node_linux_amd64/gocron-node ./cmd/node + env CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -ldflags "$(LDFLAGS)" -o ./gocron-node_windows_amd64/gocron-node.exe ./cmd/node + + +clean: + + rm -rf gocron_darwin_amd64 + rm -rf gocron_linux_amd64 + rm -rf gocron_windows_amd64 + + rm -rf gocron-node_darwin_amd64 + rm -rf gocron-node_linux_amd64 + rm -rf gocron-node_windows_amd64 + diff --git a/modules/setting/setting.go b/modules/setting/setting.go index 7e8d54105..81cf30467 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -63,7 +63,7 @@ func Read(filename string) (*Setting, error) { s.ApiKey = section.Key("api.key").MustString("") s.ApiSecret = section.Key("api.secret").MustString("") s.ApiSignEnable = section.Key("api.sign.enable").MustBool(true) - s.ConcurrencyQueue = section.Key("concurrency.queue").MustInt(1000) + s.ConcurrencyQueue = section.Key("concurrency.queue").MustInt(500) s.EnableTLS = section.Key("enable_tls").MustBool(false) s.CAFile = section.Key("ca_file").MustString("") diff --git a/package.sh b/package.sh new file mode 100644 index 000000000..423ed6a97 --- /dev/null +++ b/package.sh @@ -0,0 +1,114 @@ +#!/usr/bin/env bash + +#set -x -u +# 构建应用, 生成压缩包 gocron.zip或gocron.tar.gz +# ./package.sh -v 1.4 + +VERSION='' +GOCRON_APP_NAME='gocron' +GOCRON_NODE_APP_NAME='gocron-node' +PACKAGE_DIR='./packages' + + +# 用法 +usage() { + echo 'usage: ./package.sh -v version' +} + +# 初始化 +init() { + rm -rf ${PACKAGE_DIR} + mkdir -p ${PACKAGE_DIR} +} + +# 构建应用 +build() { + make -f makefile.cross-compiles VERSION=${VERSION} + + if [[ $? -ne 0 ]];then + echo 'make error' + exit 1 + fi +} + +# 清理 +clean() { + make -f makefile.cross-compiles clean +} + +# 打包gocron +package_gocron() { + local OS=$1 + local GOCRON_COMPRESS_FILE='' + local PLATFORM_NAME=${GOCRON_APP_NAME}_${OS}_amd64 + + if [[ ! -d ${PACKAGE_DIR}/${PLATFORM_NAME} ]];then + mkdir ${PACKAGE_DIR}/${PLATFORM_NAME} + fi + + for file in public templates LICENSE README.md Dockerfile-release; do + cp -r ${file} ${PLATFORM_NAME} + done + + if [[ ${OS} = 'windows' ]];then + GOCRON_COMPRESS_FILE=${GOCRON_APP_NAME}-v${VERSION}-${OS}-amd64.zip + zip -rq ${PACKAGE_DIR}/${PLATFORM_NAME}/${GOCRON_COMPRESS_FILE} ${PLATFORM_NAME} + else + GOCRON_COMPRESS_FILE=${GOCRON_APP_NAME}-v${VERSION}-${OS}-amd64.tar.gz + tar czf ${PACKAGE_DIR}/${PLATFORM_NAME}/${GOCRON_COMPRESS_FILE} ${PLATFORM_NAME} + fi +} + +# 打包gocron-node +package_gocron_node() { + local OS=$1 + local GOCRON_NODE_COMPRESS_FILE='' + local PLATFORM_NAME=${GOCRON_NODE_APP_NAME}_${OS}_amd64 + + if [[ ! -d ${PACKAGE_DIR}/${PLATFORM_NAME} ]];then + mkdir ${PACKAGE_DIR}/${PLATFORM_NAME} + fi + + if [[ ${OS} = 'windows' ]];then + GOCRON_NODE_COMPRESS_FILE=${GOCRON_NODE_APP_NAME}-v${VERSION}-${OS}-amd64.zip + zip -rq ${PACKAGE_DIR}/${PLATFORM_NAME}/${GOCRON_NODE_COMPRESS_FILE} ${PLATFORM_NAME} + else + GOCRON_NODE_COMPRESS_FILE=${GOCRON_NODE_APP_NAME}-v${VERSION}-${OS}-amd64.tar.gz + tar czf ${PACKAGE_DIR}/${PLATFORM_NAME}/${GOCRON_NODE_COMPRESS_FILE} ${PLATFORM_NAME} + fi +} + + +package_multi() { + for os in darwin linux windows; do + package_gocron ${os} + package_gocron_node ${os} + done +} + +while getopts "v:" OPT; do + case ${OPT} in + v) VERSION=${OPTARG} + ;; + esac +done + +if [[ -z ${VERSION} ]]; then + usage + exit 1 +fi + + +run() { + init + build + package_multi + clean +} + + +run + + + + diff --git a/routers/install/install.go b/routers/install/install.go index 28f9845c6..84a75f07d 100644 --- a/routers/install/install.go +++ b/routers/install/install.go @@ -121,7 +121,7 @@ func writeConfig(form InstallForm) error { "api.key", "", "api.secret", "", "enable_tls", "false", - "concurrency.queue", "1000", + "concurrency.queue", "500", "ca_file", "", "cert_file", "", "key_file", "", diff --git a/screenshot_slack.png b/screenshot/notification.png similarity index 100% rename from screenshot_slack.png rename to screenshot/notification.png diff --git a/scheduler.png b/screenshot/scheduler.png similarity index 100% rename from scheduler.png rename to screenshot/scheduler.png diff --git a/screenshot_task.png b/screenshot/task.png similarity index 100% rename from screenshot_task.png rename to screenshot/task.png diff --git a/service/task.go b/service/task.go index 47084ce25..d265a66cd 100644 --- a/service/task.go +++ b/service/task.go @@ -39,21 +39,21 @@ var ( // 并发队列 type ConcurrencyQueue struct { - queue chan bool + queue chan struct{} } -func (cq *ConcurrencyQueue) Push() { - cq.queue <- true +func (cq *ConcurrencyQueue) Add() { + cq.queue <- struct{}{} } -func (cq *ConcurrencyQueue) Pop() { +func (cq *ConcurrencyQueue) Done() { <-cq.queue } // 任务计数 type TaskCount struct { wg sync.WaitGroup - exit chan bool + exit chan struct{} } func (tc *TaskCount) Add() { @@ -88,7 +88,7 @@ func (i *Instance) has(key int) bool { } func (i *Instance) add(key int) { - i.m.Store(key, true) + i.m.Store(key, struct{}{}) } func (i *Instance) done(key int) { @@ -107,8 +107,8 @@ type TaskResult struct { func (task Task) Initialize() { serviceCron = cron.New() serviceCron.Start() - concurrencyQueue = ConcurrencyQueue{queue: make(chan bool, app.Setting.ConcurrencyQueue)} - taskCount = TaskCount{sync.WaitGroup{}, make(chan bool)} + concurrencyQueue = ConcurrencyQueue{queue: make(chan struct{}, app.Setting.ConcurrencyQueue)} + taskCount = TaskCount{sync.WaitGroup{}, make(chan struct{})} go taskCount.Wait() logger.Info("开始初始化定时任务") @@ -303,21 +303,21 @@ func createJob(taskModel models.Task) cron.FuncJob { return nil } taskFunc := func() { + taskCount.Add() + defer taskCount.Done() + taskLogId := beforeExecJob(taskModel) if taskLogId <= 0 { return } - taskCount.Add() - defer taskCount.Done() - if taskModel.Multi == 0 { runInstance.add(taskModel.Id) defer runInstance.done(taskModel.Id) } - concurrencyQueue.Push() - defer concurrencyQueue.Pop() + concurrencyQueue.Add() + defer concurrencyQueue.Done() logger.Infof("开始执行任务#%s#命令-%s", taskModel.Name, taskModel.Command) taskResult := execJob(handler, taskModel, taskLogId)