forked from Odinman/ogo
/
run_daemon.go
88 lines (77 loc) · 1.59 KB
/
run_daemon.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// +build daemon
package ogo
/* {{{ import
*/
import (
"fmt"
"os"
"path/filepath"
"reflect"
"runtime"
"time"
"github.com/VividCortex/godaemon"
"github.com/nightlyone/lockfile"
//"github.com/zenazn/goji"
//"github.com/zenazn/goji/web/middleware"
)
/* }}} */
/* {{{ func (mux *Mux) Run()
* Run ogo application.
*/
func (mux *Mux) Run() {
defer func() {
if err := recover(); err != nil {
WriteMsg("App crashed with error:", err)
for i := 1; ; i++ {
_, file, line, ok := runtime.Caller(i)
if !ok {
break
}
WriteMsg(file, line)
}
//panic要输出到console
fmt.Println("App crashed with error:", err)
}
}()
if env.Daemonize {
godaemon.MakeDaemon(&godaemon.DaemonAttr{})
}
//check&write pidfile, added by odin
dir := filepath.Dir(env.PidFile)
if _, err := os.Stat(dir); err != nil {
if os.IsNotExist(err) {
//mkdir
if err := os.Mkdir(dir, 0755); err != nil {
panic(err)
}
}
}
if l, err := lockfile.New(env.PidFile); err == nil {
if le := l.TryLock(); le != nil {
panic(le)
}
} else {
panic(err)
}
var mainErr error
Debug("will run worker: %v", env.Worker)
if worker, ok := DMux.Workers[env.Worker]; ok {
vw := reflect.New(worker.WorkerType)
execWorker, ok := vw.Interface().(WorkerInterface)
if !ok {
panic("worker is not WorkerInterface")
}
//Init
execWorker.Init(DMux, env.Worker)
//Main
mainErr = execWorker.Main()
} else {
mainErr = fmt.Errorf("not found worker: %s", env.Worker)
}
if mainErr != nil {
panic(mainErr)
}
//睡一段时间再结束
time.Sleep(1000 * time.Microsecond)
}
/* }}} */