用python执行rails项目sidekiq任务
我们先看一下如何ruby如何推一个任务到sidekiq
https://github.com/mperham/sidekiq/wiki/FAQ#how-do-i-push-a-job-to-sidekiq-without-ruby
require 'securerandom' require 'json' redis = Redis.new(:url => 'redis://hostname:port/db') msg = { "class" => 'MyWorker', "queue" => 'default', "args" => [1, 2, 3], 'retry' => true, 'jid' => SecureRandom.hex(12), 'created_at' => Time.now.to_f, 'enqueued_at' => Time.now.to_f } redis.lpush("queue:default", JSON.dump(msg))
本质上是在redis的一个队列key 'queue:default'里面塞msg。然后sidekiq再在'queue:default'取出来执行。
python如何调用ruby项目的sidekiq
import redis import json from random import choice from datetime import datetime sidekiq_queues = redis.Redis(host=host, port=port, db=db, password=password) queue = 'my_queue' job = 'MyJob' arguments = 123456789 value = { "class": job, "queue": queue, "args": [arguments], "retry": 'true', "jid": ''.join([choice('qwertyuiopasdfghjklzxcvbnm1234567890') for i in range(24)]), "created_at": datetime.now().timestamp(), "enqueued_at": datetime.now().timestamp()} sidekiq_queues.sadd("queues", queue) sidekiq_queues.lpush("queue:{queue}" , json.dumps(value))