diff options
author | Paul Sherwood <paul.sherwood@codethink.co.uk> | 2015-10-28 21:10:53 +0000 |
---|---|---|
committer | Paul Sherwood <paul.sherwood@codethink.co.uk> | 2015-10-28 21:10:53 +0000 |
commit | 653ce9248b4bc05ed35dccbcb4fd1fb8ff28d389 (patch) | |
tree | 8bd4ff1037fcd14a6062e40668aa80dcc4c2f62d | |
parent | 095530a0c75db42b5e2936efddd37a22186aad6f (diff) | |
download | ybd-653ce9248b4bc05ed35dccbcb4fd1fb8ff28d389.tar.gz |
First-pass at cross-instance counter
-rw-r--r-- | ybd/app.py | 24 | ||||
-rw-r--r-- | ybd/assembly.py | 4 |
2 files changed, 24 insertions, 4 deletions
@@ -31,6 +31,24 @@ from repos import get_version config = {} +class Counter(object): + def __init__(self, pid): + self._counter_file = os.path.join(config['tmp'], str(pid)) + with open(self._counter_file, 'a') as f: + f.write(str(0)) + + def increment(self): + with open(self._counter_file, 'r') as f: + count = f.read() + with open(self._counter_file, 'w') as f: + f.write(str(int(count) + 1)) + + def get(self): + with open(self._counter_file, 'r') as f: + count = f.read() + return count + + def log(component, message='', data=''): ''' Print a timestamped log. ''' @@ -41,8 +59,8 @@ def log(component, message='', data=''): timestamp = timestamp[:9] + elapsed(config['start-time']) progress = '' if config.get('counter'): - progress = '[%s/%s/%s] ' % (config['counter'], config['tasks'], - config['total']) + count = config['counter'].get() + progress = '[%s/%s/%s] ' % (count, config['tasks'], config['total']) entry = '%s %s[%s] %s %s\n' % (timestamp, progress, name, message, data) if config.get('instances'): entry = str(config.get('fork', 0)) + ' ' + entry @@ -127,6 +145,8 @@ def setup(args): if not config.get('max-jobs'): config['max-jobs'] = cpu_count() / config.get('instances', 1) + config['pid'] = os.getpid() + config['counter'] = Counter(config['pid']) log('SETUP', '%s version is' % config['program'], config['my-version']) log('SETUP', 'Max-jobs is set to', config['max-jobs']) diff --git a/ybd/assembly.py b/ybd/assembly.py index 63b8cda..294d930 100644 --- a/ybd/assembly.py +++ b/ybd/assembly.py @@ -54,7 +54,7 @@ def assemble(defs, target): if get_cache(defs, component): return cache_key(defs, component) if get_remote(defs, component): - app.config['counter'] += 1 + app.config['counter'].increment() return cache_key(defs, component) random.seed(datetime.datetime.now()) @@ -85,7 +85,7 @@ def assemble(defs, target): if 'systems' not in component and not get_cache(defs, component): try: with claim(defs, component): - app.config['counter'] += 1 + app.config['counter'].increment() with app.timer(component, 'build of %s' % component['cache']): build(defs, component) with app.timer(component, 'artifact creation'): |