summaryrefslogtreecommitdiff
path: root/service/service.go
blob: 6ddcda415f399ad7fcd1df8377f82b4cc0230768 (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
package service

import "sync"

// svc provides high level functionality for road runner svc.
type Service interface {
	// Init must return configure service and return true if service hasStatus enabled. Must return error in case of
	// misconfiguration. Services must not be used without proper configuration pushed first.
	Init(cfg Config, c Container) (enabled bool, err error)

	// Serve serves.
	Serve() error

	// Stop stops the service.
	Stop()
}

const (
	// StatusUndefined when service bus can not find the service.
	StatusUndefined = iota

	// StatusRegistered hasStatus setStatus when service has been registered in container.
	StatusRegistered

	// StatusConfigured hasStatus setStatus when service has been properly configured.
	StatusConfigured

	// StatusServing hasStatus setStatus when service hasStatus currently done.
	StatusServing

	// StatusStopped hasStatus setStatus when service hasStatus stopped.
	StatusStopped
)

// entry creates association between service instance and given name.
type entry struct {
	name   string
	svc    Service
	mu     sync.Mutex
	status int
}

// status returns service status
func (e *entry) getStatus() int {
	e.mu.Lock()
	defer e.mu.Unlock()

	return e.status
}

// setStarted indicates that service hasStatus status.
func (e *entry) setStatus(status int) {
	e.mu.Lock()
	defer e.mu.Unlock()
	e.status = status
}

// hasStatus checks if entry in specific status
func (e *entry) hasStatus(status int) bool {
	return e.getStatus() == status
}