Skip to content

Commit

Permalink
Merge pull request #4601 from scottdillon/3994-fix-background-job-tim…
Browse files Browse the repository at this point in the history
…eout

3994 fix background job timeout
  • Loading branch information
amercader committed Jan 18, 2019
2 parents a342792 + 6382d1e commit 5d7a498
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 2 deletions.
2 changes: 2 additions & 0 deletions ckan/config/deployment.ini_tmpl
Expand Up @@ -191,6 +191,8 @@ ckan.hide_activity_from_users = %(ckan.site_id)s
#smtp.password = your_password
#smtp.mail_from =

## Background Job Settings
ckan.jobs.timeout = 180

## Logging configuration
[loggers]
Expand Down
10 changes: 8 additions & 2 deletions ckan/lib/jobs.py
Expand Up @@ -36,6 +36,7 @@
log = logging.getLogger(__name__)

DEFAULT_QUEUE_NAME = u'default'
DEFAULT_JOB_TIMEOUT = config.get(u'ckan.jobs.timeout', 180)

# RQ job queues. Do not use this directly, use ``get_queue`` instead.
_queues = {}
Expand Down Expand Up @@ -123,7 +124,8 @@ def get_queue(name=DEFAULT_QUEUE_NAME):
return queue


def enqueue(fn, args=None, kwargs=None, title=None, queue=DEFAULT_QUEUE_NAME):
def enqueue(fn, args=None, kwargs=None, title=None, queue=DEFAULT_QUEUE_NAME,
timeout=DEFAULT_JOB_TIMEOUT):
u'''
Enqueue a job to be run in the background.
Expand All @@ -141,14 +143,18 @@ def enqueue(fn, args=None, kwargs=None, title=None, queue=DEFAULT_QUEUE_NAME):
:param string queue: Name of the queue. If not given then the
default queue is used.
:param integer timeout: The timeout, in seconds, to be passed
to the background job via rq.
:returns: The enqueued job.
:rtype: ``rq.job.Job``
'''
if args is None:
args = []
if kwargs is None:
kwargs = {}
job = get_queue(queue).enqueue_call(func=fn, args=args, kwargs=kwargs)
job = get_queue(queue).enqueue_call(func=fn, args=args, kwargs=kwargs,
timeout=timeout)
job.meta[u'title'] = title
job.save()
msg = u'Added background job {}'.format(job.id)
Expand Down
12 changes: 12 additions & 0 deletions ckan/tests/lib/test_jobs.py
Expand Up @@ -74,6 +74,18 @@ def test_enqueue_queue(self):
assert_equal(all_jobs[1].origin,
jobs.add_queue_name_prefix(u'my_queue'))

def test_enqueue_timeout(self):
self.enqueue()
self.enqueue(timeout=0)
self.enqueue(timeout=-1)
self.enqueue(timeout=3600)
all_jobs = self.all_jobs()
assert_equal(len(all_jobs), 4)
assert_equal(all_jobs[0].timeout, 180)
assert_equal(all_jobs[1].timeout, 180)
assert_equal(all_jobs[2].timeout, -1)
assert_equal(all_jobs[3].timeout, 3600)


class TestGetAllQueues(RQTestBase):

Expand Down
6 changes: 6 additions & 0 deletions doc/maintaining/background-tasks.rst
Expand Up @@ -99,6 +99,12 @@ You can also give the job a title which can be useful for identifying it when

jobs.enqueue(log_job, [u'My log message'], title=u'My log job')

A timeout can also be set on a job iwth the ``timeout`` keyword argument::

jobs.enqueue(log_job, [u'My log message'], timeout=3600)

The default background job timeout is 180 seconds. This is set in the
ckan config ``.ini`` file under the ``ckan.jobs.timeout`` item.

Accessing the database from background jobs
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
3 changes: 3 additions & 0 deletions test-core.ini
Expand Up @@ -101,6 +101,9 @@ who.config_file = %(here)s/who.ini
who.log_level = warning
who.log_file = %(cache_dir)s/who_log.ini

## background jobs
ckan.jobs.timeout = 180

# Logging configuration
[loggers]
keys = root, ckan, sqlalchemy
Expand Down

0 comments on commit 5d7a498

Please sign in to comment.