mirror of https://github.com/peter4431/rq.git
Workers can listen to external commands via pubsub (#1363)
* Added a way to send shutdown command via pubsub * Added kill-horse command * Added kill horse command * Added send_kill_horse_command() and send_shutdown_command() * Document worker commandsmain
parent
0e65bab10b
commit
a721db34b1
@ -0,0 +1,25 @@
|
||||
import json
|
||||
|
||||
|
||||
PUBSUB_CHANNEL_TEMPLATE = 'rq:pubsub:%s'
|
||||
|
||||
|
||||
def send_command(redis, worker_name, command):
|
||||
"""Use Redis' pubsub mechanism to send a command"""
|
||||
payload = {'command': command}
|
||||
redis.publish(PUBSUB_CHANNEL_TEMPLATE % worker_name, json.dumps(payload))
|
||||
|
||||
|
||||
def parse_payload(payload):
|
||||
"""Returns a dict of command data"""
|
||||
return json.loads(payload.get('data').decode())
|
||||
|
||||
|
||||
def send_shutdown_command(redis, worker_name):
|
||||
"""Send shutdown command"""
|
||||
send_command(redis, worker_name, 'shutdown')
|
||||
|
||||
|
||||
def send_kill_horse_command(redis, worker_name):
|
||||
"""Tell worker to kill it's horse"""
|
||||
send_command(redis, worker_name, 'kill-horse')
|
@ -0,0 +1,45 @@
|
||||
import time
|
||||
|
||||
from multiprocessing import Process
|
||||
|
||||
from tests import RQTestCase
|
||||
from tests.fixtures import long_running_job
|
||||
|
||||
from rq import Queue, Worker
|
||||
from rq.command import send_command, send_kill_horse_command, send_shutdown_command
|
||||
|
||||
|
||||
class TestCommands(RQTestCase):
|
||||
|
||||
def test_shutdown_command(self):
|
||||
"""Ensure that shutdown command works properly."""
|
||||
connection = self.testconn
|
||||
worker = Worker('foo', connection=connection)
|
||||
|
||||
def _send_shutdown_command():
|
||||
time.sleep(0.25)
|
||||
send_shutdown_command(connection, worker.name)
|
||||
|
||||
p = Process(target=_send_shutdown_command)
|
||||
p.start()
|
||||
worker.work()
|
||||
p.join(1)
|
||||
|
||||
def test_kill_horse_command(self):
|
||||
"""Ensure that shutdown command works properly."""
|
||||
connection = self.testconn
|
||||
queue = Queue('foo', connection=connection)
|
||||
job = queue.enqueue(long_running_job, 4)
|
||||
worker = Worker('foo', connection=connection)
|
||||
|
||||
def _send_kill_horse_command():
|
||||
"""Waits 0.25 seconds before sending kill-horse command"""
|
||||
time.sleep(0.25)
|
||||
send_kill_horse_command(connection, worker.name)
|
||||
|
||||
p = Process(target=_send_kill_horse_command)
|
||||
p.start()
|
||||
worker.work(burst=True)
|
||||
p.join(1)
|
||||
job.refresh()
|
||||
self.assertTrue(job.id in queue.failed_job_registry)
|
Loading…
Reference in New Issue