Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Job List Limit #8070

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions changes/8070.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
job_list action now only returns 200 jobs by default.
Use `limit` or `ckan.jobs.default_list_limit` config option now.
22 changes: 17 additions & 5 deletions ckan/cli/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,35 @@ def worker(burst: bool, queues: list[str]):


@jobs.command(name=u"list", short_help=u"List jobs.")
@click.option("-l", "--limit", type=click.INT,
help="Number of jobs to return. Default: %s" %
bg_jobs.DEFAULT_JOB_LIST_LIMIT,
default=bg_jobs.DEFAULT_JOB_LIST_LIMIT)
@click.option("-i", "--ids", is_flag=True, type=click.BOOL,
help="Only return a list of job ids.", default=False)
@click.argument(u"queues", nargs=-1)
def list_jobs(queues: list[str]):
def list_jobs(queues: list[str],
limit: int = bg_jobs.DEFAULT_JOB_LIST_LIMIT, ids: bool = False):
"""List currently enqueued jobs from the given queues. If no queue
names are given then the jobs from all queues are listed.
"""
data_dict = {
u"queues": list(queues),
"limit": limit,
"ids_only": ids,
}
jobs = logic.get_action(u"job_list")({u"ignore_auth": True}, data_dict)
if not jobs:
return click.secho(u"There are no pending jobs.", fg=u"green")
for job in jobs:
if job[u"title"] is None:
job[u"title"] = u""
if ids:
click.secho(job)
else:
job[u"title"] = u'"{}"'.format(job[u"title"])
click.secho(u"{created} {id} {queue} {title}".format(**job))
if job[u"title"] is None:
job[u"title"] = u""
else:
job[u"title"] = u'"{}"'.format(job[u"title"])
click.secho(u"{created} {id} {queue} {title}".format(**job))


@jobs.command(short_help=u"Show details about a specific job.")
Expand Down
1 change: 1 addition & 0 deletions ckan/lib/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
log = logging.getLogger(__name__)

DEFAULT_QUEUE_NAME = u'default'
DEFAULT_JOB_LIST_LIMIT = 200

# RQ job queues. Do not use this directly, use ``get_queue`` instead.
_queues: dict[str, rq.Queue] = {}
Expand Down
27 changes: 23 additions & 4 deletions ckan/logic/action/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -3160,22 +3160,41 @@ def job_list(context: Context, data_dict: DataDict) -> ActionResult.JobList:
:param list queues: Queues to list jobs from. If not given then the
jobs from all queues are listed.

:param int limit: Number to limit the list of jobs by.

:param bool ids_only: Whether to return only a list if job IDs or not.

:returns: The currently enqueued background jobs.
:rtype: list

Will return the list in the way that RQ workers will execute the jobs.
Thus the left most non-empty queue will be emptied first
before the next right non-empty one.

.. versionadded:: 2.7
'''
_check_access(u'job_list', context, data_dict)
dictized_jobs: ActionResult.JobList = []
jobs_list: ActionResult.JobList = []
queues: Any = data_dict.get(u'queues')
limit = data_dict.get('limit', config.get('ckan.jobs.default_list_limit',
jobs.DEFAULT_JOB_LIST_LIMIT))
ids_only = asbool(data_dict.get('ids_only', False))
if queues:
queues = [jobs.get_queue(q) for q in queues]
else:
queues = jobs.get_all_queues()
for queue in queues:
for job in queue.jobs:
dictized_jobs.append(jobs.dictize_job(job))
return dictized_jobs
if ids_only:
jobs_list += queue.job_ids[:limit]
else:
# type_ignore_reason: pyright does not know return from queue.jobs
jobs_list += [jobs.dictize_job(j) # type: ignore
for j in queue.jobs[:limit]]
if limit > 0 and len(jobs_list) > limit:
# we need to return here if there is a limit
# and it has been surpassed
return jobs_list[:limit]
return jobs_list


def job_show(context: Context, data_dict: DataDict) -> ActionResult.JobShow:
Expand Down
5 changes: 4 additions & 1 deletion ckan/logic/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -782,10 +782,13 @@ def update_configuration_schema():

@validator_args
def job_list_schema(
ignore_missing: Validator, list_of_strings: Validator
ignore_missing: Validator, list_of_strings: Validator,
int_validator: Validator, boolean_validator: Validator
) -> Schema:
return {
u'queues': [ignore_missing, list_of_strings],
'limit': [ignore_missing, int_validator],
'ids_only': [ignore_missing, boolean_validator],
}


Expand Down
2 changes: 1 addition & 1 deletion ckan/types/logic/action_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
HelpShow = Optional[str]
ConfigOptionShow = Any
ConfigOptionList = List[str]
JobList = List[AnyDict]
JobList = Union[List[AnyDict], List[str]]
JobShow = AnyDict
ApiTokenList = List[AnyDict]

Expand Down