2014-05-05 08:49:29 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-02-15 20:55:06 +00:00
|
|
|
"""
|
|
|
|
This file contains all jobs that are used in tests. Each of these test
|
|
|
|
fixtures has a slighty different characteristics.
|
|
|
|
"""
|
2014-05-05 08:49:29 +00:00
|
|
|
from __future__ import (absolute_import, division, print_function,
|
|
|
|
unicode_literals)
|
|
|
|
|
2014-09-08 10:31:05 +00:00
|
|
|
import os
|
2012-02-22 17:01:14 +00:00
|
|
|
import time
|
2014-05-05 08:49:29 +00:00
|
|
|
|
|
|
|
from rq import Connection, get_current_job
|
2012-07-23 06:25:31 +00:00
|
|
|
from rq.decorators import job
|
|
|
|
|
2014-09-08 18:29:24 +00:00
|
|
|
|
2014-09-08 10:31:05 +00:00
|
|
|
def say_pid():
|
|
|
|
return os.getpid()
|
2012-02-22 16:39:14 +00:00
|
|
|
|
2014-09-08 18:29:24 +00:00
|
|
|
|
2012-02-15 20:55:06 +00:00
|
|
|
def say_hello(name=None):
|
|
|
|
"""A job with a single argument and a return value."""
|
|
|
|
if name is None:
|
|
|
|
name = 'Stranger'
|
|
|
|
return 'Hi there, %s!' % (name,)
|
|
|
|
|
2012-02-22 16:39:14 +00:00
|
|
|
|
2012-02-15 20:55:06 +00:00
|
|
|
def do_nothing():
|
|
|
|
"""The best job in the world."""
|
|
|
|
pass
|
|
|
|
|
2012-02-22 16:39:14 +00:00
|
|
|
|
2012-02-15 20:55:06 +00:00
|
|
|
def div_by_zero(x):
|
|
|
|
"""Prepare for a division-by-zero exception."""
|
|
|
|
return x / 0
|
|
|
|
|
2012-02-22 16:39:14 +00:00
|
|
|
|
2012-02-15 20:55:06 +00:00
|
|
|
def some_calculation(x, y, z=1):
|
|
|
|
"""Some arbitrary calculation with three numbers. Choose z smartly if you
|
|
|
|
want a division by zero exception.
|
|
|
|
"""
|
|
|
|
return x * y / z
|
|
|
|
|
2012-02-22 16:39:14 +00:00
|
|
|
|
2012-02-15 20:55:06 +00:00
|
|
|
def create_file(path):
|
|
|
|
"""Creates a file at the given path. Actually, leaves evidence that the
|
|
|
|
job ran."""
|
|
|
|
with open(path, 'w') as f:
|
|
|
|
f.write('Just a sentinel.')
|
2012-02-22 17:01:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
def create_file_after_timeout(path, timeout):
|
|
|
|
time.sleep(timeout)
|
|
|
|
create_file(path)
|
2012-07-17 10:48:41 +00:00
|
|
|
|
|
|
|
|
2012-09-02 20:34:11 +00:00
|
|
|
def access_self():
|
|
|
|
job = get_current_job()
|
|
|
|
return job.id
|
|
|
|
|
|
|
|
|
2014-04-11 09:47:31 +00:00
|
|
|
def echo(*args, **kwargs):
|
|
|
|
return (args, kwargs)
|
|
|
|
|
|
|
|
|
2013-04-19 19:18:23 +00:00
|
|
|
class Number(object):
|
|
|
|
def __init__(self, value):
|
|
|
|
self.value = value
|
2012-07-17 10:48:41 +00:00
|
|
|
|
2013-04-19 19:18:23 +00:00
|
|
|
@classmethod
|
|
|
|
def divide(cls, x, y):
|
|
|
|
return x * y
|
|
|
|
|
|
|
|
def div(self, y):
|
|
|
|
return self.value / y
|
2012-07-23 06:25:31 +00:00
|
|
|
|
2012-07-24 10:33:22 +00:00
|
|
|
|
2014-07-26 07:33:58 +00:00
|
|
|
class CallableObject(object):
|
|
|
|
def __call__(self):
|
|
|
|
return u"I'm callable"
|
|
|
|
|
|
|
|
|
2012-07-24 10:33:22 +00:00
|
|
|
with Connection():
|
|
|
|
@job(queue='default')
|
|
|
|
def decorated_job(x, y):
|
|
|
|
return x + y
|
2012-07-26 02:36:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
def long_running_job():
|
|
|
|
time.sleep(10)
|