From f3506ce57ae144f1a6447940f3eb0285767d16dd Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 24 Mar 2018 13:58:58 +0200 Subject: Add: Reporter class for reporting progress to controller --- ick2/client.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'ick2/client.py') diff --git a/ick2/client.py b/ick2/client.py index bf708ae..99b8487 100644 --- a/ick2/client.py +++ b/ick2/client.py @@ -15,6 +15,7 @@ import json import logging +import time import requests @@ -170,6 +171,46 @@ class ControllerClient: self._api.post(url, headers=headers, body=body) +class Reporter: # pragma: no cover + + def __init__(self, api, work): + self._api = api + self._work = dict(work) + + def report(self, exit_code, stdout, stderr): + result = dict(self._work) + result['exit_code'] = exit_code + result['stdout'] = self.make_string(stdout) + result['stderr'] = self.make_string(stderr) + for key in result: + logging.debug('Reporter: result[%r]=%r', key, result[key]) + self._api.report_work(result) + + def make_string(self, thing): + if isinstance(thing, bytes): + return thing.decode('UTF-8') + if isinstance(thing, str): + return thing + if thing is None: + return thing + return repr(thing) + + def get_work_result(self, work): + return { + 'build_id': work['build_id'], + 'worker': work['worker'], + 'project': work['project'], + 'pipeline': work['pipeline'], + 'stdout': '', + 'stderr': '', + 'exit_code': None, + 'timestamp': self.now(), + } + + def now(self): + return time.strftime('%Y-%m-%dT%H:%M:%S') + + class BlobClient: def __init__(self): -- cgit v1.2.1