Beispiel #1
0
def complete_inprogress_job(device_id):
    key = get_job_key(device_id)
    job_info = get_job(device_id)
    assert job_info['status'] == IN_PROGRESS
    job_info['status'] = COMPLETED
    CacheRepository.store(key, json.dumps(job_info), expire_in_seconds=None)
    logger.info('made job %s %s' % (device_id, COMPLETED))
Beispiel #2
0
def schedule(sc):
    device_key_pattern = get_device_key_pattern()
    job_key_pattern = get_job_key_pattern()
    device_iter = CacheRepository.get_scan_iter(device_key_pattern)
    job_iter = CacheRepository.get_scan_iter(job_key_pattern)

    for job_key in job_iter:
        old_device_id = re.search(get_job_key_regex(),
                                  job_key.decode()).group(1)
        job_info = get_job(old_device_id)
        if job_info['status'] == COMPLETED:
            continue
        new_device_key = next(device_iter, None)
        if new_device_key is None:
            logger.info(
                'scheduler: found no active device to send %s job %s' % (job_info['status'], job_key))
            break
        new_device_id = re.search(get_device_key_regex(),
                                  new_device_key.decode()).group(1)
        id = job_info['job']
        executable_url = job_info['executable_url']
        input_file_url = job_info['input_file_url']
        index = job_info['index']
        new_job = MockJob(id, executable_url, input_file_url, index)
        submit_job_to_device(new_device_id, new_job, index)
        # TODO we can change the key structure instead of removing the record
        remove_job(old_device_id)
        logger.info('scheduler: send job %s to device %s with index %s' %
                    (new_job.id, new_device_id, index))

    s.enter(SCHEDULING_INTERVAL, 1, schedule, (sc,))
Beispiel #3
0
def add_job(device_id, job, index, status):
    key = get_job_key(device_id)
    value = json.dumps({
        'job': str(job.id),
        'executable_url': job.executable.url,
        'input_file_url': job.input_file.url,
        'index': index,
        'num_devices_per_job': NUM_DEVICES_PER_JOB,
        'status': status
    })
    CacheRepository.store(key, value, expire_in_seconds=None)
Beispiel #4
0
def get_job(device_id):
    key = get_job_key(device_id)
    value = CacheRepository.get(key)
    if value is None:
        logger.error(
            'no job found for device with id %s . something went wrong' % device_id)
        return None
    else:
        return json.loads(value)
Beispiel #5
0
def submit_job(job):
    device_key_pattern = get_device_key_pattern()
    scan_iter = CacheRepository.get_scan_iter(device_key_pattern)
    i = 0
    for key in scan_iter:
        if i == NUM_DEVICES_PER_JOB:
            break
        device_id = re.search(get_device_key_regex(),
                              key.decode()).group(1)
        submit_job_to_device(device_id, job, i)
        logger.info('send job %s to device %s with index %s' %
                    (job.id, device_id, i))
        i = i + 1

    for j in range(i, NUM_DEVICES_PER_JOB):
        device_id = 'not_found_' + str(uuid.uuid4())
        add_job(device_id, job, j, FAILED)
        logger.info('send job %s to device %s with index %s' %
                    (job.id, device_id, j))
Beispiel #6
0
def remove_job(device_id):
    key = get_job_key(device_id)
    CacheRepository.delete(key)
Beispiel #7
0
def get_device(device_id):
    key = get_device_key(device_id)
    return CacheRepository.get(key)
Beispiel #8
0
def add_device(device_id):
    key = get_device_key(device_id)
    CacheRepository.store(key, '1', expire_in_seconds=None)
Beispiel #9
0
def remove_device(device_id):
    key = get_device_key(device_id)
    CacheRepository.delete(key)