-
Notifications
You must be signed in to change notification settings - Fork 801
/
service.go
114 lines (99 loc) · 3.53 KB
/
service.go
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
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package worker
import (
"github.com/uber/cadence/common"
"github.com/uber/cadence/common/metrics"
"github.com/uber/cadence/common/persistence"
"github.com/uber/cadence/common/service"
)
type (
// Service represents the cadence-worker service. This service host all background processing which needs to happen
// for a Cadence cluster. This service runs the replicator which is responsible for applying replication tasks
// generated by remote clusters.
Service struct {
stopC chan struct{}
params *service.BootstrapParams
config *Config
metricsClient metrics.Client
}
// Config contains all the service config for worker
Config struct {
// Replicator settings
ReplicatorConcurrency int
}
)
// NewService builds a new cadence-worker service
func NewService(params *service.BootstrapParams) common.Daemon {
return &Service{
params: params,
config: NewConfig(),
stopC: make(chan struct{}),
}
}
// NewConfig builds the new Config for cadence-worker service
func NewConfig() *Config {
return &Config{
ReplicatorConcurrency: 10,
}
}
// Start is called to start the service
func (s *Service) Start() {
p := s.params
log := p.Logger
log.Infof("%v starting", common.WorkerServiceName)
base := service.New(p)
base.Start()
s.metricsClient = base.GetMetricsClient()
// worker only use the v2
metadataManager, err := persistence.NewCassandraMetadataPersistenceV2(p.CassandraConfig.Hosts,
p.CassandraConfig.Port,
p.CassandraConfig.User,
p.CassandraConfig.Password,
p.CassandraConfig.Datacenter,
p.CassandraConfig.Keyspace,
p.ClusterMetadata.GetCurrentClusterName(),
p.Logger)
if err != nil {
log.Fatalf("failed to create metadata manager: %v", err)
}
metadataManager = persistence.NewMetadataPersistenceClient(metadataManager, base.GetMetricsClient(), log)
history, err := base.GetClientFactory().NewHistoryClient()
if err != nil {
log.Fatalf("failed to create history service client: %v", err)
}
replicator := NewReplicator(p.ClusterMetadata, metadataManager, history, s.config, p.MessagingClient, log,
s.metricsClient)
if err := replicator.Start(); err != nil {
replicator.Stop()
log.Fatalf("Fail to start replicator: %v", err)
}
log.Infof("%v started", common.WorkerServiceName)
<-s.stopC
base.Stop()
}
// Stop is called to stop the service
func (s *Service) Stop() {
select {
case s.stopC <- struct{}{}:
default:
}
s.params.Logger.Infof("%v stopped", common.WorkerServiceName)
}