Skip to content

Commit

Permalink
add daemon script support
Browse files Browse the repository at this point in the history
  • Loading branch information
sjqzhang committed Mar 8, 2019
1 parent b486262 commit 27fb69b
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 3 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,16 @@ http://10.1.xx.60:8080/repair_stat
问题可以直接提issue
```
- 如何后台运行程序?
```
请使用control 对程序进行后面运行,具体操作如下:
一、 chmod +x control
二、 确保control与fileserver在同一个目录
三、 ./control start|stop|status #对和序进行启动,停止,查看状态等。

```
- 能不能在一台机器部置多个服务端?
```
不能,在设计之初就已考虑到集群的高可用问题,为了保证集群的真正可用,必须为不同的ip,ip 不能用 127.0.0.1
Expand Down
108 changes: 108 additions & 0 deletions control
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#!/bin/bash

WORKSPACE=$(cd $(dirname $0)/; pwd)
cd $WORKSPACE

mkdir -p log conf

module=
app=fileserver
conf=conf/cfg.json
pidfile=conf/app.pid
logfile=log/app.log

function check_pid() {
if [ -f $pidfile ];then
pid=`cat $pidfile`
if [ -n $pid ]; then
running=`ps -p $pid|grep -v "PID TTY" |wc -l`
return $running
fi
fi
return 0
}

function start() {
check_pid
running=$?
if [ $running -gt 0 ];then
echo -n "$app now is running already, pid="
cat $pidfile
return 1
fi

nohup ./$app &> $logfile &
echo $! > $pidfile
echo "$app started..., pid=$!"
}

function stop() {
pid=`cat $pidfile`
kill $pid
echo "$app stoped..."
}

function restart() {
stop
sleep 1
start
}

function status() {
check_pid
running=$?
if [ $running -gt 0 ];then
echo -n "$app now is running, pid="
cat $pidfile
else
echo "$app is stoped"
fi
}

function tailf() {
tail -f $logfile
}

function build() {
go build
if [ $? -ne 0 ]; then
exit $?
fi
mv $module $app
./$app -v | grep -v "config"
}

function pack() {
build
git log -1 --pretty=%h > gitversion
version=`./$app -v|grep -v config`
file_list="control cfg.example.json $app"
tar zcf $app-$version.tar.gz gitversion $file_list
}

function packbin() {
build
git log -1 --pretty=%h > gitversion
version=`./$app -v|grep -v config`
tar zcvf $app-bin-$version.tar.gz $app gitversion
}

function help() {
echo "$0 start|stop|restart|status|tail"
}

if [ "$1" == "" ]; then
help
elif [ "$1" == "stop" ];then
stop
elif [ "$1" == "start" ];then
start
elif [ "$1" == "restart" ];then
restart
elif [ "$1" == "status" ];then
status
elif [ "$1" == "tail" ];then
tailf
else
help
fi
6 changes: 3 additions & 3 deletions fileserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ const (
"addr": ":8080",
"PeerID": "集群内唯一,请使用0-9的单字符,默认自动生成",
"peer_id": "%s",
"本主机地址": "本机http地址,默认自动生成,必段为内网,自动生成不为内网请自行修改,下同",
"本主机地址": "本机http地址,默认自动生成(注意端口必须与addr中的端口一致),必段为内网,自动生成不为内网请自行修改,下同",
"host": "%s",
"集群": "集群列表,注意为了高可用,IP必须不能是同一个,同一不会自动备份,且不能为127.0.0.1,且必须为内网IP,默认自动生成",
"peers": ["%s"],
Expand Down Expand Up @@ -2071,7 +2071,7 @@ func (this *Server) SaveUploadFile(file multipart.File, header *multipart.FileHe
fileInfo.Md5 = v
//fileInfo.Path = folder //strings.Replace( folder,DOCKER_DIR,"",1)
fileInfo.Path = strings.Replace(folder, DOCKER_DIR, "", 1)
fileInfo.Peers = append(fileInfo.Peers, fmt.Sprintf("http://%s", r.Host))
fileInfo.Peers = append(fileInfo.Peers, fmt.Sprintf("http://%s", this.host))
//fmt.Println("upload",fileInfo)
return fileInfo, nil
}
Expand Down Expand Up @@ -2174,7 +2174,7 @@ func (this *Server) Upload(w http.ResponseWriter, r *http.Request) {
return
}
}
this.saveFileMd5Log(&fileInfo, CONST_FILE_Md5_FILE_NAME)//maybe slow
this.saveFileMd5Log(&fileInfo, CONST_FILE_Md5_FILE_NAME) //maybe slow
go this.postFileToPeer(&fileInfo)
if fileInfo.Size <= 0 {
log.Error("file size is zero")
Expand Down

0 comments on commit 27fb69b

Please sign in to comment.