summaryrefslogtreecommitdiff
path: root/service/reload/service.go
blob: 9b3ac2f08482286799d2b75604cc409220c01bb4 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package reload

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

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

type Service struct {
	reloadConfig *Config
	watcher      *Watcher

}

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

	var err error
	var configs []WatcherConfig

	// mount Services to designated services
	for serviceName, _ := range cfg.Services {
		svc, _ := c.Get(serviceName)
		if ctrl, ok := svc.(roadrunner.Controllable); ok {
			tmp := cfg.Services[serviceName]
			tmp.service = &ctrl
			cfg.Services[serviceName] = tmp
		}
	}



	for serviceName, config := range s.reloadConfig.Services {
		if cfg.Services[serviceName].service == nil {
			continue
		}
		configs = append(configs, WatcherConfig{
			serviceName: serviceName,
			recursive:   config.Recursive,
			directories: config.Dirs,
			filterHooks: func(filename, pattern string) error {
				if strings.Contains(filename, pattern) {
					return ErrorSkip
				}
				return nil
			},
			files: make(map[string]os.FileInfo),
			//ignored:
		})
	}

	s.watcher, err = NewWatcher(configs)
	if err != nil {
		return false, err
	}

	for serviceName, config := range s.reloadConfig.Services {
		svc, _ := c.Get(serviceName)
		if ctrl, ok := svc.(*roadrunner.Controllable); ok {
			(*ctrl).Server().Reset()
		}

		configs = append(configs, WatcherConfig{
			serviceName: serviceName,
			recursive:   config.Recursive,
			directories: config.Dirs,
			filterHooks: func(filename, pattern string) error {
				if strings.Contains(filename, pattern) {
					return ErrorSkip
				}
				return nil
			},
			files: make(map[string]os.FileInfo),
			//ignored:

		})
	}

	return true, nil
}

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

	go func() {
		for {
			select {
			case e := <-s.watcher.Event:
				println(fmt.Sprintf("type is:%s, oldPath:%s, path:%s, name:%s", e.Type, e.OldPath, e.Path, e.FileInfo.Name()))

				srv := s.reloadConfig.Services[e.Type]

				if srv.service != nil {
					s := *srv.service
					err := s.Server().Reset()
					if err != nil {
						fmt.Println(err)
					}
				} else {
					s.watcher.mu.Lock()
					delete(s.watcher.watcherConfigs, e.Type)
					s.watcher.mu.Unlock()
				}
			}
		}
	}()

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

	return nil
}

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

}