2016-11-18 05:04:20 +00:00
|
|
|
import datetime
|
|
|
|
import logging
|
|
|
|
|
2016-11-18 05:04:50 +00:00
|
|
|
from redis import RedisError
|
|
|
|
from trollius import From, Return, coroutine
|
|
|
|
|
2015-01-29 19:50:07 +00:00
|
|
|
from data.database import BUILD_PHASE
|
2014-12-23 16:17:23 +00:00
|
|
|
from data import model
|
2016-11-18 05:04:20 +00:00
|
|
|
from buildman.asyncutil import AsyncWrapper
|
2015-09-30 19:15:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
2014-11-14 00:41:17 +00:00
|
|
|
|
2016-11-18 05:04:20 +00:00
|
|
|
|
2014-11-14 00:41:17 +00:00
|
|
|
class StatusHandler(object):
|
|
|
|
""" Context wrapper for writing status to build logs. """
|
|
|
|
|
2014-12-23 16:17:23 +00:00
|
|
|
def __init__(self, build_logs, repository_build_uuid):
|
2014-11-14 00:41:17 +00:00
|
|
|
self._current_phase = None
|
2015-02-11 02:46:58 +00:00
|
|
|
self._current_command = None
|
2014-12-23 16:17:23 +00:00
|
|
|
self._uuid = repository_build_uuid
|
2016-11-18 05:04:50 +00:00
|
|
|
self._build_logs = AsyncWrapper(build_logs)
|
|
|
|
self._sync_build_logs = build_logs
|
|
|
|
self._build_model = AsyncWrapper(model.build)
|
2014-11-14 00:41:17 +00:00
|
|
|
|
|
|
|
self._status = {
|
2016-11-18 05:04:20 +00:00
|
|
|
'total_commands': 0,
|
|
|
|
'current_command': None,
|
|
|
|
'push_completion': 0.0,
|
|
|
|
'pull_completion': 0.0,
|
2014-11-14 00:41:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Write the initial status.
|
|
|
|
self.__exit__(None, None, None)
|
|
|
|
|
2016-11-18 05:04:50 +00:00
|
|
|
@coroutine
|
2014-11-14 00:41:17 +00:00
|
|
|
def _append_log_message(self, log_message, log_type=None, log_data=None):
|
2015-01-29 23:05:05 +00:00
|
|
|
log_data = log_data or {}
|
|
|
|
log_data['datetime'] = str(datetime.datetime.now())
|
2015-09-30 19:15:10 +00:00
|
|
|
|
|
|
|
try:
|
2016-11-18 05:04:50 +00:00
|
|
|
yield From(self._build_logs.append_log_message(self._uuid, log_message, log_type, log_data))
|
2015-09-30 19:15:10 +00:00
|
|
|
except RedisError:
|
|
|
|
logger.exception('Could not save build log for build %s: %s', self._uuid, log_message)
|
2014-11-14 00:41:17 +00:00
|
|
|
|
2016-11-18 05:04:50 +00:00
|
|
|
@coroutine
|
2014-11-14 00:41:17 +00:00
|
|
|
def append_log(self, log_message, extra_data=None):
|
2015-02-11 02:46:58 +00:00
|
|
|
if log_message is None:
|
|
|
|
return
|
|
|
|
|
2016-11-18 05:04:50 +00:00
|
|
|
yield From(self._append_log_message(log_message, log_data=extra_data))
|
2014-11-14 00:41:17 +00:00
|
|
|
|
2016-11-18 05:04:50 +00:00
|
|
|
@coroutine
|
2014-11-14 00:41:17 +00:00
|
|
|
def set_command(self, command, extra_data=None):
|
2015-02-11 02:46:58 +00:00
|
|
|
if self._current_command == command:
|
2016-11-18 05:04:50 +00:00
|
|
|
raise Return()
|
2015-02-11 02:46:58 +00:00
|
|
|
|
|
|
|
self._current_command = command
|
2016-11-18 05:04:50 +00:00
|
|
|
yield From(self._append_log_message(command, self._build_logs.COMMAND, extra_data))
|
2014-11-14 00:41:17 +00:00
|
|
|
|
2016-11-18 05:04:50 +00:00
|
|
|
@coroutine
|
2015-02-12 21:19:44 +00:00
|
|
|
def set_error(self, error_message, extra_data=None, internal_error=False, requeued=False):
|
2016-11-18 05:04:50 +00:00
|
|
|
error_phase = BUILD_PHASE.INTERNAL_ERROR if internal_error and requeued else BUILD_PHASE.ERROR
|
|
|
|
yield From(self.set_phase(error_phase))
|
2014-11-21 19:27:06 +00:00
|
|
|
|
|
|
|
extra_data = extra_data or {}
|
|
|
|
extra_data['internal_error'] = internal_error
|
2016-11-18 05:04:50 +00:00
|
|
|
yield From(self._append_log_message(error_message, self._build_logs.ERROR, extra_data))
|
2014-11-14 00:41:17 +00:00
|
|
|
|
2016-11-18 05:04:50 +00:00
|
|
|
@coroutine
|
2014-11-14 00:41:17 +00:00
|
|
|
def set_phase(self, phase, extra_data=None):
|
|
|
|
if phase == self._current_phase:
|
2016-11-18 05:04:50 +00:00
|
|
|
raise Return(False)
|
2014-11-18 20:45:56 +00:00
|
|
|
|
2014-11-14 00:41:17 +00:00
|
|
|
self._current_phase = phase
|
2016-11-18 05:04:50 +00:00
|
|
|
yield From(self._append_log_message(phase, self._build_logs.PHASE, extra_data))
|
2014-12-23 16:17:23 +00:00
|
|
|
|
|
|
|
# Update the repository build with the new phase
|
2016-11-18 05:04:50 +00:00
|
|
|
raise Return(self._build_model.update_phase(self._uuid, phase))
|
2014-11-14 00:41:17 +00:00
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
return self._status
|
|
|
|
|
|
|
|
def __exit__(self, exc_type, value, traceback):
|
2015-09-30 19:15:10 +00:00
|
|
|
try:
|
2016-11-18 05:04:50 +00:00
|
|
|
self._sync_build_logs.set_status(self._uuid, self._status)
|
2015-09-30 19:15:10 +00:00
|
|
|
except RedisError:
|
|
|
|
logger.exception('Could not set status of build %s to %s', self._uuid, self._status)
|