-
Notifications
You must be signed in to change notification settings - Fork 368
Expand file tree
/
Copy pathscheduler.rb
More file actions
88 lines (77 loc) · 3.26 KB
/
scheduler.rb
File metadata and controls
88 lines (77 loc) · 3.26 KB
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
require 'cloud_controller/deployment_updater/dispatcher'
require 'locket/lock_worker'
require 'locket/lock_runner'
require 'cloud_controller/standalone_metrics_webserver'
module VCAP::CloudController
module DeploymentUpdater
class Scheduler
class << self
def start
with_error_logging('cc.deployment_updater') do
config = CloudController::DependencyLocator.instance.config
if config.get(:publish_metrics) || false
VCAP::CloudController::StandaloneMetricsWebserver.start_for_bosh_job(config.get(:prometheus_port) || 9395)
periodic_updater = CloudController::DependencyLocator.instance.vitals_periodic_updater
periodic_updater.setup_updates
end
statsd_client = CloudController::DependencyLocator.instance.statsd_client
update_step = proc {
update(
update_frequency: config.get(:deployment_updater, :update_frequency_in_seconds),
statsd_client: statsd_client
)
}
if config.get(:locket).nil?
loop(&update_step)
return
end
lock_runner = Locket::LockRunner.new(
key: config.get(:deployment_updater, :lock_key),
owner: config.get(:deployment_updater, :lock_owner),
host: config.get(:locket, :host),
port: config.get(:locket, :port),
client_ca_path: config.get(:locket, :ca_file),
client_key_path: config.get(:locket, :key_file),
client_cert_path: config.get(:locket, :cert_file)
)
lock_worker = Locket::LockWorker.new(lock_runner)
lock_worker.acquire_lock_and_repeatedly_call(&update_step)
end
end
private
def update(update_frequency:, statsd_client:)
logger = Steno.logger('cc.deployment_updater.scheduler')
update_start_time = Time.now
Dispatcher.dispatch
update_duration = Time.now - update_start_time
## NOTE: We're taking time in seconds and multiplying by 1000 because we don't have
## access to time in milliseconds. If you ever get access to reliable time in
## milliseconds, then do know that the lack of precision here is not desired
## so feed in the entire value!
update_duration_ms = update_duration * 1000
statsd_client.timing('cc.deployments.update.duration', update_duration_ms)
logger.info("Update loop took #{update_duration}s")
sleep_duration = update_frequency - update_duration
if sleep_duration > 0
logger.info("Sleeping #{sleep_duration}s")
sleep(sleep_duration)
else
logger.info('Not Sleeping')
end
end
def with_error_logging(error_message)
yield
rescue StandardError => e
logger = Steno.logger('cc.deployment_updater')
error_name = e.is_a?(CloudController::Errors::ApiError) ? e.name : e.class.name
logger.error(
error_message,
error: error_name,
error_message: e.message,
backtrace: e.backtrace.join("\n")
)
end
end
end
end
end