forked from snail007/goproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.go
48 lines (44 loc) · 865 Bytes
/
service.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
package services
import (
"fmt"
"log"
"runtime/debug"
)
type Service interface {
Start(args interface{}) (err error)
Clean()
}
type ServiceItem struct {
S Service
Args interface{}
Name string
}
var servicesMap = map[string]*ServiceItem{}
func Regist(name string, s Service, args interface{}) {
servicesMap[name] = &ServiceItem{
S: s,
Args: args,
Name: name,
}
}
func Run(name string) (service *ServiceItem, err error) {
service, ok := servicesMap[name]
if ok {
go func() {
defer func() {
err := recover()
if err != nil {
log.Fatalf("%s servcie crashed, ERR: %s\ntrace:%s", name, err, string(debug.Stack()))
}
}()
err := service.S.Start(service.Args)
if err != nil {
log.Fatalf("%s servcie fail, ERR: %s", name, err)
}
}()
}
if !ok {
err = fmt.Errorf("service %s not found", name)
}
return
}