summaryrefslogtreecommitdiff
path: root/cmd/_____/server.go
blob: 5542e7c9d4f9f47ed4abe352f6013fc86438c38a (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
package roadrunner

import (
	"os/exec"
	"sync"
)

const (
	// EventNewPool triggered when server creates new pool.
	EventNewPool = 60

	// EventDestroyPool triggered when server destroys existed pool.
	EventDestroyPool = 61
)

// Service manages pool creation and swapping.
type Server struct {
	// configures server, pool, cmd creation and factory.
	scfg *ServerConfig

	// worker command creator
	cmd func() *exec.Cmd

	// observes pool events (can be attached to multiple pools at the same time)
	observer func(event int, ctx interface{})

	// creates and connects to workers
	factory Factory

	// protects pool while the switch
	mu sync.Mutex
}

// todo: do assignment

// Reconfigure configures underlying pool and destroys it's previous version if any.
func (r *Server) Configure(cfg Config) error {
	r.mu.Lock()
	previous := r.pool
	r.mu.Unlock()

	pool, err := NewPool(r.cmd, r.factory, cfg)
	if err != nil {
		return err
	}

	r.throw(EventNewPool, pool)

	r.mu.Lock()

	r.cfg, r.pool = cfg, pool
	r.pool.Observe(r.poolObserver)

	r.mu.Unlock()

	if previous != nil {
		go func(p Pool) {
			r.throw(EventDestroyPool, p)
			p.Destroy()
		}(previous)
	}

	return nil
}