在redis工作中存储“meta”数据不起作用?

时间:2015-01-22 04:54:48

标签: python redis python-rq

我正在尝试测试排队的redis作业,但meta数据似乎没有在任务和发起者之间传递。 job_id似乎匹配所以我很困惑。也许一些新鲜的眼睛可以帮助我解决问题:

任务是as per the documentation

from rq import get_current_job

def do_test(word):
    job = get_current_job()
    print job.get_id()
    job.meta['word'] = word
    job.save()
    print "saved: ", job.meta['word']
    return True

rqworker日志在保存后打印job_id和word

14:32:32 *** Listening on default...
14:33:07 default: labeller.do_test('supercalafragelistic') (a6e2e579-df26-411a-b017-8788d621149f)
a6e2e579-df26-411a-b017-8788d621149f
saved:  supercalafragelistic
14:33:07 Job OK, result = True
14:33:07 Result is kept for 500 seconds.

从unittest调用任务:

class RedisQueueTestCase(unittest.TestCase):
    """
    Requires running "rqworker" on the localhost cmdline
    """
    def setUp(self):
        use_connection()
        self.q = Queue()

    def test_enqueue(self):
        job = self.q.enqueue(do_test, "supercalafragelistic")
        while True:
            print job.get_id(), job.get_status(), job.meta.get('word')
            if job.is_finished:
                print "Result: ", job.result, job.meta.get('word')
                break
            time.sleep(0.25)

并生成此日志,显示相同的job_id和正确的结果,但永远不会填充meta变量word

Testing started at 2:33 PM ...
a6e2e579-df26-411a-b017-8788d621149f queued None
a6e2e579-df26-411a-b017-8788d621149f finished None
Result:  True None

Process finished with exit code 0

我尝试添加一个很长的延迟,以便日志有机会在started中看到任务,但是没有完成状态(如果meta在完成时被清除),但它没有任何区别。

知道我错过了什么吗?

1 个答案:

答案 0 :(得分:2)

在远程端发生save后,本地作业不会自动更新。必须执行refresh才能更新它。在重构之前,这不是必需的,因为我在每次请求时都使用job_id进行fetch_job

因此测试例程需要包含refresh()(或fetch_job)以反映任何更改:

def test_enqueue(self):
    job = self.q.enqueue(do_test, "supercalafragelistic")
    while True:
        job.refresh()     #<--- well, duh, freddy
        print job.get_id(), job.get_status(), job.meta.get('word')
        if job.is_finished:
            print "Result: ", job.result, job.meta.get('word')
            break
        time.sleep(0.25)

哪个更好一点:

Testing started at 5:14 PM ...
6ea0163f-b5d5-411a-906a-f765aa0b3cc6 queued None 0 []
6ea0163f-b5d5-411a-906a-f765aa0b3cc6 started supercalafragelistic
6ea0163f-b5d5-411a-906a-f765aa0b3cc6 finished supercalafragelistic
Result:  True supercalafragelistic

get_status正在更新的事实让我忽略了这一点:get_status()是一种方法,用于查找当前状态,而meta只是指向某些< em>可能是陈旧的数据。