使用makefile构建应用

develop
ouqiang 2018-02-04 11:28:48 +08:00
parent e1fa3cf645
commit 738b168689
15 changed files with 230 additions and 276 deletions

6
.gitignore vendored
View File

@ -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

114
build.sh
View File

@ -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

View File

@ -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

View File

@ -1,4 +1,6 @@
package cmd
// main gocron
package main
import (
"os"
@ -15,10 +17,24 @@ import (
"gopkg.in/macaron.v1"
)
var AppVersion = "1.4"
// web服务器默认端口
const DefaultPort = 5920
var CmdWeb = cli.Command{
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,
@ -41,6 +57,9 @@ var CmdWeb = cli.Command{
},
}
return []cli.Command{command}
}
func runWeb(ctx *cli.Context) {
// 设置运行环境
setEnvironment(ctx)

View File

@ -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

View File

@ -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)
}

18
makefile Normal file
View File

@ -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

33
makefile.cross-compiles Normal file
View File

@ -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

View File

@ -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("")

114
package.sh Normal file
View File

@ -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

View File

@ -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", "",

View File

Before

Width:  |  Height:  |  Size: 132 KiB

After

Width:  |  Height:  |  Size: 132 KiB

View File

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 14 KiB

View File

Before

Width:  |  Height:  |  Size: 295 KiB

After

Width:  |  Height:  |  Size: 295 KiB

View File

@ -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)