Add a total maximum time that a machine is allowed to stick around before we terminate it more forcefully.

This commit is contained in:
Jake Moshenko 2014-12-23 11:18:10 -05:00
parent aac7feb20b
commit 055a6b0c37
2 changed files with 17 additions and 3 deletions

View file

@ -135,8 +135,12 @@ class EphemeralBuilderManager(BaseManager):
ttl = self.setup_time()
expiration = datetime.utcnow() + timedelta(seconds=ttl)
machine_max_expiration = self._manager_config.get('MACHINE_MAX_TIME', 7200)
max_expiration = datetime.utcnow() + timedelta(seconds=machine_max_expiration)
payload = {
'expiration': calendar.timegm(expiration.timetuple()),
'max_expiration': calendar.timegm(max_expiration.timetuple()),
}
try:
@ -154,7 +158,7 @@ class EphemeralBuilderManager(BaseManager):
# Store the builder in etcd associated with the job id
payload['builder_id'] = builder_id
yield From(self._etcd_client.write(job_key, payload, prevExist=True))
yield From(self._etcd_client.write(job_key, payload, prevExist=True, ttl=ttl))
raise Return(True)
@ -192,12 +196,17 @@ class EphemeralBuilderManager(BaseManager):
job_key = self._etcd_job_key(build_job)
build_job_response = yield From(self._etcd_client.read(job_key))
ttl = self.heartbeat_period_sec * 2
max_expiration = datetime.utcfromtimestamp(build_job_response.value['max_expiration'])
max_expiration_remaining = max_expiration - datetime.utcnow()
max_expiration_sec = max(0, int(max_expiration_remaining.total_seconds()))
ttl = min(self.heartbeat_period_sec * 2, max_expiration_sec)
new_expiration = datetime.utcnow() + timedelta(seconds=ttl)
payload = {
'expiration': calendar.timegm(new_expiration.timetuple()),
'builder_id': build_job_response.value['builder_id'],
'max_expiration': build_job_response.value['max_expiration'],
}
yield From(self._etcd_client.write(job_key, payload, ttl=ttl))

View file

@ -159,8 +159,13 @@ class TestEphemeral(unittest.TestCase):
@async_test
def test_heartbeat_response(self):
expiration_timestamp = time.time() + 60
builder_result = Mock(spec=etcd.EtcdResult)
builder_result.value = {'builder_id': '123', 'expiration': '123'}
builder_result.value = {
'builder_id': '123',
'expiration': expiration_timestamp,
'max_expiration': expiration_timestamp,
}
self.etcd_client_mock.read = Mock(return_value=builder_result)
yield From(self.manager.job_heartbeat(self.mock_job))