This repository has been archived by the owner on Jan 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #318 from chop-dbhi/async-worker
Methods and jobs API to support async result retrieval
- Loading branch information
Showing
11 changed files
with
516 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
from django_rq import get_worker, get_queue | ||
|
||
from avocado.conf import settings | ||
from avocado.query import utils | ||
|
||
|
||
def run_jobs(): | ||
""" | ||
Execute all the pending jobs. | ||
""" | ||
get_worker(settings.ASYNC_QUEUE).work(burst=True) | ||
|
||
|
||
def get_job(job_id): | ||
""" | ||
Return the job for the specified ID or None if it cannot be found. | ||
Args: | ||
job_id(uuid): The ID of the job to retrieve. | ||
Returns: | ||
The job with the matching ID or None if no job with the supplied job | ||
ID could be found. | ||
""" | ||
queue = get_queue(settings.ASYNC_QUEUE) | ||
return queue.fetch_job(job_id) | ||
|
||
|
||
def get_job_count(): | ||
""" | ||
Returns the current number of jobs in the queue. | ||
""" | ||
return get_queue(settings.ASYNC_QUEUE).count | ||
|
||
|
||
def get_job_result(job_id): | ||
""" | ||
Returns the result of the job with the supplied ID. | ||
If the job could not be found or the job is not finished yet, None will | ||
be returned as the job result. | ||
Args: | ||
job_id(uuid): The ID of the job to retrieve the result for. | ||
Returns: | ||
The result of the job with the matching ID or None if the job could | ||
not be found or is not finished. | ||
""" | ||
return get_job(job_id).result | ||
|
||
|
||
def get_jobs(): | ||
""" | ||
Returns a collection of all the pending jobs. | ||
""" | ||
return get_queue(settings.ASYNC_QUEUE).jobs | ||
|
||
|
||
def cancel_job(job_id): | ||
""" | ||
Cancel the job and its associated query if they exist. | ||
Args: | ||
job_id(uuid): The ID of the job to cancel | ||
Returns: | ||
The cancellation result of the job's query if it had one. If the job | ||
could not be found or the job had no query, this method returns None. | ||
""" | ||
job = get_job(job_id) | ||
|
||
if job is None: | ||
return None | ||
|
||
result = None | ||
query_name = job.meta.get('query_name') | ||
if query_name: | ||
canceled = utils.cancel_query(query_name) | ||
result = { | ||
'canceled': canceled | ||
} | ||
|
||
job.cancel() | ||
return result | ||
|
||
|
||
def cancel_all_jobs(): | ||
""" | ||
Cancels all jobs. | ||
""" | ||
get_queue(settings.ASYNC_QUEUE).empty() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,4 @@ ordereddict | |
sqlparse | ||
mysql-python | ||
psycopg2 | ||
django_rq |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
'modeltree>=1.1.9', | ||
'South==1.0.2', | ||
'jsonfield==1.0.0', | ||
'django_rq', | ||
] | ||
|
||
if sys.version_info < (2, 7): | ||
|
Oops, something went wrong.