summaryrefslogtreecommitdiff
path: root/service/reload/service.go
blob: f50d6626087cfa9eb3481ef3347763258baebc65 (plain)
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
89
90
91
92
93
94
package reload

import (
	"github.com/spiral/roadrunner/service"
	"os"
	"strings"
	"time"
)

// ID contains default service name.
const ID = "reload"

type Service struct {
	reloadConfig *Config
	container    service.Container
	watcher      *Watcher
}

// Init controller service
func (s *Service) Init(cfg *Config, c service.Container) (bool, error) {
	s.container = c
	s.reloadConfig = cfg

	return true, nil
}

func (s *Service) Serve() error {
	if !s.reloadConfig.Enabled {
		return nil
	}

	var err error
	s.watcher, err = NewWatcher([]WatcherConfig{WatcherConfig{
		serviceName: "test",
		recursive:   false,
		directories: []string{"/service"},
		filterHooks: func(filename, pattern string) error {
			if strings.Contains(filename, pattern) {
				return ErrorSkip
			}
			return nil
		},
		files:   make(map[string]os.FileInfo),
		//ignored: []string{".php"},
	}})
	if err != nil {
		return err
	}



	s.watcher.AddSingle("test", "/service")


	go func() {
		for {
			select {
			case e := <-s.watcher.Event:
				println(e.Name())
			}
		}
		//for e = range w.Event {
		//
		//	println("event")
		//	// todo use status
		//	//svc, _ := s.container.Get("http")
		//	//if svc != nil {
		//	//	if srv, ok := svc.(service.Service); ok {
		//	//		srv.Stop()
		//	//		err = srv.Serve()
		//	//		if err != nil {
		//	//			return err
		//	//		}
		//	//	}
		//	//}
		//
		//	//println("event skipped due to service is nil")
		//}
	}()

	err = s.watcher.StartPolling(time.Second)
	if err != nil {
		return err
	}

	// read events and restart corresponding services

	return nil
}

func (s *Service) Stop() {
	//s.watcher.Stop()

}