def get_batch_job_statuses(*, batch_name, job_index=None): key = dict(name='batcho_job_status_strings', batch_name=batch_name) obj = ca.getValue(key=key, subkey='-', parse_json=True) if not obj: return dict() return obj
def _clear_job_results(*, jobs, incomplete_only=True): for job in jobs: val = ca.getValue(key=job) if val: if (not incomplete_only) or (val.startswith('in-process')) or ( val.startswith('error')): print('Clearing job: ' + job['label']) ca.setValue(key=job, value=None)
def get_batch_names_for_compute_resource(compute_resource): key0 = dict( name='compute_resource_batch_names', compute_resource=compute_resource ) obj = ca.getValue(key=key0, subkey='-', parse_json=True) if not obj: return [] batch_names = list(obj.keys()) return batch_names
def _download_recordings(*, jobs): for ii, job in enumerate(jobs): val = ca.getValue(key=job) if not val: if 'recording' in job: if 'directory' in job['recording']: dsdir = job['recording']['directory'] fname = dsdir + '/raw.mda' print('REALIZING FILE: ' + fname) ca.realizeFile(path=fname)
def _run_job(job): val = ca.getValue(key=job) if val: return code = ''.join(random.choice(string.ascii_uppercase) for x in range(10)) if not ca.setValue(key=job, value='in-process-' + code, overwrite=False): return status = dict(time_started=_make_timestamp(), status='running') _set_job_status(job, status) print('Running job: ' + job['label']) try: result = _do_run_job(job) except: status['time_finished'] = _make_timestamp() status['status'] = 'error' status['error'] = 'Exception in _do_run_job' val = ca.getValue(key=job) if val == 'in-process-' + code: _set_job_status(job, status) raise val = ca.getValue(key=job) if val != 'in-process-' + code: print( 'Not saving result because in-process code does not match {} <> {}.' .format(val, 'in-process-' + code)) return status['time_finished'] = _make_timestamp() status['result'] = result if 'error' in result: print('Error running job: ' + result['error']) status['status'] = 'error' status['error'] = result['error'] _set_job_status(job, status) ca.setValue(key=job, value='error-' + code) return status['status'] = 'finished' ca.saveObject( key=job, object=result ) # Not needed in future, because we should instead use the status object
def _retrieve_batch(batch_name): print('Retrieving batch {}'.format(batch_name)) key = dict(name='batcho_batch', batch_name=batch_name) a = ca.getValue(key=key) if not a: print('Unable to retrieve batch {}. Not found in pairio.'.format(batch_name)) return None obj = ca.loadObject(key=key) if not obj: print( 'Unable to retrieve batch {}. Object not found on kbucket.'.format(batch_name)) return None if 'jobs' not in obj: raise Exception( 'batch object does not contain jobs field for batch_name={}'.format(batch_name)) return obj
def _take_next_batch_job_index_to_run(*, batch_name): key = dict(name='batcho_next_batch_job_index_to_run', batch_name=batch_name) last_attempted_job_index = -1 last_attempted_job_index_timestamp = time.time() while True: val = ca.getValue(key=key) if val is None: return None job_index = int(val) if _acquire_job_lock(batch_name=batch_name, job_index=job_index): ca.setValue(key=key, value=str(job_index+1)) return job_index else: if job_index == last_attempted_job_index: elapsed0 = time.time()-last_attempted_job_index_timestamp if elapsed0 > 10: raise Exception('Unexpected problem where we cannot obtain the job lock, and yet the current job index remains at {} for {} seconds.'.format( job_index, elapsed0)) last_attempted_job_index = job_index last_attempted_job_index_timestamp = time.time() time.sleep(random.uniform(0, 2))
def _get_batch_code(batch_name): return ca.getValue(key=dict(name='batcho_batch_code', batch_name=batch_name))
def _get_job_status_string(*, batch_name, job_index): key = dict(name='batcho_job_status_strings', batch_name=batch_name) return ca.getValue(key=key, subkey=str(job_index))
from cairio import client as ca print('------------------------------------------------') # Local key/value store for associating relatively short strings (<=80 characters) with arbitrary keys (strings or dicts) # Setting values (these should be short strings, <=80 characters) ca.setValue(key='some-key1', value='hello 1') ca.setValue(key=dict(name='some_name', number=2), value='hello 2') # Getting values val1 = ca.getValue(key='some-key1') val2 = ca.getValue(key=dict(name='some_name', number=2)) print(val1) print(val2) print('------------------------------------------------') # Setting password-protected values ca.setValue(key='some_key2', password='******', value='the-secret-*y$#a') # Retrieving password-protected values print(ca.getValue(key='some_key2', password='******')) print('------------------------------------------------') # Local storage of data and files, retrievable by SHA-1 hash path = ca.saveText('This is some text', basename='test.txt') print(path) # Output: sha1://482cb0cfcbed6740a2bcb659c9ccc22a4d27b369/test.txt