def update_realm_config(realm_id): """update the configuration of a realm""" realm = realms.get(realm_id) try: body = json.loads(request.body.read(4096)) except Exception as exc: raise JSONError(client.BAD_REQUEST, exception=exc, message='Require JSON in request body') lease_time = body.get('default_lease_time', None) if lease_time is not None: if type(lease_time) not in (long, int): raise JSONError(client.BAD_REQUEST, exception='TypeError', message="default_lease_time not int") realm.set_default_lease_time(lease_time) queue_lease_time = queue_lease_time = body.get('queue_lease_time', None) if queue_lease_time is not None: try: queue_id, lease_time = queue_lease_time except (ValueError, TypeError) as err: raise JSONError(client.BAD_REQUEST, exception='ValueError', message='queue_lease_time err - %s' % err) if type(lease_time) not in (long, int): raise JSONError(client.BAD_REQUEST, exception='TypeError', message="default_lease_time not int") realm.set_queue_lease_time(queue_id, lease_time) return {}
def pull_realm_jobs(realm_id): """pull the next set of jobs from the realm""" realm = realms.get(realm_id) count = request.GET.get('count', default=1, type=int) max_queue = request.GET.get('max-queue') job = realm.pull(count=count, max_queue=max_queue) return job
def get_realm_status(realm_id): """return the status of a realm""" realm = realms.get(realm_id) status = realm.status return status
def pull_jobs(realm_id): """pull the next set of jobs from the realm""" realm = realms.get(realm_id) count = request.GET.get('count', default=1, type=int) job = realm.pull(count=count) return job
def get_tag_status(realm_id, tag_id): """return an int of the number of jobs related to tag_id""" realm = realms.get(realm_id) status = realm.get_tag_status(tag_id) return status
def get_tagged_jobs(realm_id, tag_id): """return a dict of all jobs tagged by tag_id""" realm = realms.get(realm_id) jobs = realm.get_tagged_jobs(tag_id) return jobs
def get_job(realm_id, job_id): """Get the status of a job""" realm = realms.get(realm_id) job = realm.get_job(job_id) return job
def delete_tagged_jobs(realm_id, tag_id): """Remove a tag and all of its jobs from a realm""" realm = realms.get(realm_id) realm.remove_tagged_jobs(tag_id) return {}
def move_job(realm_id, job_id, from_q, to_q): """Move the job from one queue to another""" realm = realms.get(realm_id) realm.move_job(job_id, from_q, to_q) return {}
def clear_queue(realm_id, queue_id): """remove all jobs from the given queue""" realm = realms.get(realm_id) realm.clear_queue(queue_id) return {}
import sys import time from pprint import pprint from restq import realms if __name__ == '__main__': jobs = realms.get('test_mem') try: datasize = int(sys.argv[1]) jobcount = int(sys.argv[2]) queuecount = int(sys.argv[3]) taskcount = int(sys.argv[4]) except: print("test_mem.py <datasize> <jobcount> <queuecount> <taskcount>") print("python test_mem.py 20 10000 2 1000") exit() print("Start insert") t = time.time() for job_id in range(jobcount): for task_id in range(taskcount): for queue_id in range(queuecount): jobs.add(job_id, task_id, queue_id, "a" * datasize) t = time.time() - t print("Completed insert in %0.2f" % t) pprint(jobs.status) print("Start dequeue") t = time.time() c = 0 while jobs.pull(5):
def _del_job(realm_id, job_id): realm = realms.get(realm_id) try: realm.remove_job(job_id) except KeyError: pass
def _add_job(realm_id, job_id, queue_id, job): data = job.get('data', None) tags = job.get('tags', []) realm = realms.get(realm_id) realm.add(job_id, queue_id, data, tags=tags)