summaryrefslogtreecommitdiff
path: root/service/reload/service.go
blob: 359b33314b797cdd1214d5bc84ef36eaf6c0d6c7 (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
129
130
131
132
package reload

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

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

type Service struct {
	cfg     *Config
	log     *logrus.Logger
	watcher *Watcher
}

// Init controller service
func (s *Service) Init(cfg *Config, log *logrus.Logger, c service.Container) (bool, error) {
	if cfg == nil || len(cfg.Services) == 0 {
		return false, nil
	}

	s.cfg = cfg
	s.log = log

	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.cfg.Services {
		if cfg.Services[serviceName].service == nil {
			continue
		}
		ignored, err := ConvertIgnored(config.Ignore)
		if err != nil {
			return false, err
		}
		configs = append(configs, WatcherConfig{
			serviceName: serviceName,
			recursive:   config.Recursive,
			directories: config.Dirs,
			filterHooks: func(filename string, patterns []string) error {
				for i := 0; i < len(patterns); i++ {
					if strings.Contains(filename, patterns[i]) {
						return nil
					}
				}
				return ErrorSkip
			},
			files:        make(map[string]os.FileInfo),
			ignored:      ignored,
			filePatterns: append(config.Patterns, cfg.Patterns...),
		})
	}

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

	return true, nil
}

func (s *Service) Serve() error {
	if s.cfg.Interval < time.Second {
		return errors.New("reload interval is too fast")
	}

	go func() {
		for e := range s.watcher.Event {

			println(fmt.Sprintf("[UPDATE] Service: %s, path to file: %s, filename: %s", e.service, e.path, e.info.Name()))

			srv := s.cfg.Services[e.service]

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

	//go func() {
	//	for {
	//		select {
	//		case <-update:
	//			updated = append(updated, update)
	//		case <-time.Ticker:
	//			updated = updated[0:0]
	//			err := sv.Server().Reset()
	//			s.log.Debugf(
	//				"reload %s, found file changes in %s",
	//				strings.Join(updated, ","),
	//			)
	//		case <-exit:
	//		}
	//	}
	//}()

	err := s.watcher.StartPolling(s.cfg.Interval)
	if err != nil {
		return err
	}

	return nil
}

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