2016-04-29 15:51:22 +00:00
|
|
|
import logging
|
2016-04-29 16:20:30 +00:00
|
|
|
from datetime import datetime, timedelta
|
2016-04-29 15:51:22 +00:00
|
|
|
|
2017-11-10 20:46:09 +00:00
|
|
|
from app import app, instance_keys, metric_queue
|
2017-07-12 13:37:32 +00:00
|
|
|
from workers.servicekeyworker.models_pre_oci import pre_oci_model as model
|
2016-04-29 15:51:22 +00:00
|
|
|
from workers.worker import Worker
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2017-07-12 13:37:51 +00:00
|
|
|
|
2016-04-29 15:51:22 +00:00
|
|
|
class ServiceKeyWorker(Worker):
|
|
|
|
def __init__(self):
|
|
|
|
super(ServiceKeyWorker, self).__init__()
|
2017-07-12 13:19:30 +00:00
|
|
|
self.add_operation(self._refresh_service_key,
|
2017-07-12 13:37:51 +00:00
|
|
|
app.config.get('INSTANCE_SERVICE_KEY_REFRESH', 60) * 60)
|
2016-04-29 15:51:22 +00:00
|
|
|
|
2017-07-12 13:19:30 +00:00
|
|
|
def _refresh_service_key(self):
|
2016-04-29 16:20:30 +00:00
|
|
|
"""
|
2017-07-12 13:19:30 +00:00
|
|
|
Refreshes the instance's active service key so it doesn't get garbage collected.
|
2016-04-29 16:20:30 +00:00
|
|
|
"""
|
2017-11-10 20:46:09 +00:00
|
|
|
expiration_time = timedelta(minutes=instance_keys.service_key_expiration)
|
|
|
|
new_expiration = datetime.utcnow() + expiration_time
|
|
|
|
|
|
|
|
logger.debug('Starting automatic refresh of service key %s to new expiration %s',
|
|
|
|
instance_keys.local_key_id, new_expiration)
|
|
|
|
try:
|
|
|
|
model.set_key_expiration(instance_keys.local_key_id, new_expiration)
|
|
|
|
except Exception as ex:
|
|
|
|
logger.exception('Failure for automatic refresh of service key %s with new expiration %s',
|
|
|
|
instance_keys.local_key_id, new_expiration)
|
|
|
|
metric_queue.instance_key_renewal_failure.Inc(labelvalues=[instance_keys.local_key_id])
|
|
|
|
raise ex
|
|
|
|
|
|
|
|
logger.debug('Finished automatic refresh of service key %s with new expiration %s',
|
|
|
|
instance_keys.local_key_id, new_expiration)
|
|
|
|
metric_queue.instance_key_renewal_success.Inc(labelvalues=[instance_keys.local_key_id])
|
2016-04-29 15:51:22 +00:00
|
|
|
|
2017-07-12 13:37:51 +00:00
|
|
|
|
2016-04-29 15:51:22 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
worker = ServiceKeyWorker()
|
|
|
|
worker.start()
|