def setup(self): self.Popen = self.patching('celery.apps.multi.Popen') self.kill = self.patching('os.kill') self.gethostname = self.patching('celery.apps.multi.gethostname') self.gethostname.return_value = 'example.com' self.Pidfile = self.patching('celery.apps.multi.Pidfile') self.cluster = Cluster( [ Node('*****@*****.**'), Node('*****@*****.**'), Node('*****@*****.**') ], on_stopping_preamble=Mock(name='on_stopping_preamble'), on_send_signal=Mock(name='on_send_signal'), on_still_waiting_for=Mock(name='on_still_waiting_for'), on_still_waiting_progress=Mock(name='on_still_waiting_progress'), on_still_waiting_end=Mock(name='on_still_waiting_end'), on_node_start=Mock(name='on_node_start'), on_node_restart=Mock(name='on_node_restart'), on_node_shutdown_ok=Mock(name='on_node_shutdown_ok'), on_node_status=Mock(name='on_node_status'), on_node_signal=Mock(name='on_node_signal'), on_node_signal_dead=Mock(name='on_node_signal_dead'), on_node_down=Mock(name='on_node_down'), on_child_spawn=Mock(name='on_child_spawn'), on_child_signalled=Mock(name='on_child_signalled'), on_child_failure=Mock(name='on_child_failure'), )
def test_findsig(self): m = Cluster([]) self.assertEqual(m._find_sig_argument(['a', 'b', 'c', '-1']), 1) self.assertEqual(m._find_sig_argument(['--foo=1', '-9']), 9) self.assertEqual(m._find_sig_argument(['-INT']), signal.SIGINT) self.assertEqual(m._find_sig_argument([]), signal.SIGTERM) self.assertEqual(m._find_sig_argument(['-s']), signal.SIGTERM) self.assertEqual(m._find_sig_argument(['-log']), signal.SIGTERM)
def setup(self): self.Popen = self.patching('celery.apps.multi.Popen') self.kill = self.patching('os.kill') self.gethostname = self.patching('celery.apps.multi.gethostname') self.gethostname.return_value = 'example.com' self.Pidfile = self.patching('celery.apps.multi.Pidfile') self.cluster = Cluster( [Node('*****@*****.**'), Node('*****@*****.**'), Node('*****@*****.**')], on_stopping_preamble=Mock(name='on_stopping_preamble'), on_send_signal=Mock(name='on_send_signal'), on_still_waiting_for=Mock(name='on_still_waiting_for'), on_still_waiting_progress=Mock(name='on_still_waiting_progress'), on_still_waiting_end=Mock(name='on_still_waiting_end'), on_node_start=Mock(name='on_node_start'), on_node_restart=Mock(name='on_node_restart'), on_node_shutdown_ok=Mock(name='on_node_shutdown_ok'), on_node_status=Mock(name='on_node_status'), on_node_signal=Mock(name='on_node_signal'), on_node_signal_dead=Mock(name='on_node_signal_dead'), on_node_down=Mock(name='on_node_down'), on_child_spawn=Mock(name='on_child_spawn'), on_child_signalled=Mock(name='on_child_signalled'), on_child_failure=Mock(name='on_child_failure'), )
def _start_worker_process(app, concurrency=1, pool='solo', loglevel=WORKER_LOGLEVEL, logfile=None, **kwargs): # type (Celery, int, str, Union[int, str], str, **Any) -> Iterable """Start worker in separate process. Yields: celery.app.worker.Worker: worker instance. """ from celery.apps.multi import Cluster, Node app.set_current() cluster = Cluster([Node('testworker1@%h')]) cluster.start() yield cluster.stopwait()
def test_getpids(self): self.gethostname.return_value = 'e.com' self.prepare_pidfile_for_getpids(self.Pidfile) callback = Mock() p = Cluster([ Node('*****@*****.**'), Node('*****@*****.**'), Node('*****@*****.**'), ]) nodes = p.getpids(on_down=callback) node_0, node_1 = nodes self.assertEqual(node_0.name, '*****@*****.**') self.assertEqual( sorted(node_0.argv), sorted([ '', '--executable={0}'.format(node_0.executable), '--logfile=foo%I.log', '--pidfile=foo.pid', '-m celery worker --detach', '-n [email protected]', ]), ) self.assertEqual(node_0.pid, 10) self.assertEqual(node_1.name, '*****@*****.**') self.assertEqual( sorted(node_1.argv), sorted([ '', '--executable={0}'.format(node_1.executable), '--logfile=bar%I.log', '--pidfile=bar.pid', '-m celery worker --detach', '-n [email protected]', ]), ) self.assertEqual(node_1.pid, 11) # without callback, should work nodes = p.getpids('celery worker')
def test_getpids(self): self.gethostname.return_value = 'e.com' self.prepare_pidfile_for_getpids(self.Pidfile) callback = Mock() with patch('celery.apps.multi.os.mkdir'): p = Cluster([ Node('*****@*****.**'), Node('*****@*****.**'), Node('*****@*****.**'), ]) nodes = p.getpids(on_down=callback) node_0, node_1 = nodes assert node_0.name == '*****@*****.**' assert sorted(node_0.argv) == sorted([ '', '--executable={0}'.format(node_0.executable), '--logfile={}'.format( os.path.normpath('/var/log/celery/foo%I.log')), '--pidfile={}'.format(os.path.normpath('/var/run/celery/foo.pid')), '-m celery worker --detach', '-n [email protected]', ]) assert node_0.pid == 10 assert node_1.name == '*****@*****.**' assert sorted(node_1.argv) == sorted([ '', '--executable={0}'.format(node_1.executable), '--logfile={}'.format( os.path.normpath('/var/log/celery/bar%I.log')), '--pidfile={}'.format(os.path.normpath('/var/run/celery/bar.pid')), '-m celery worker --detach', '-n [email protected]', ]) assert node_1.pid == 11 # without callback, should work nodes = p.getpids('celery worker')
def test_getpids(self): self.gethostname.return_value = 'e.com' self.prepare_pidfile_for_getpids(self.Pidfile) callback = Mock() p = Cluster(['foo', 'bar', 'baz']) nodes = p.getpids(on_down=callback) node_0, node_1 = nodes self.assertEqual(node_0.name, '*****@*****.**') self.assertEqual( sorted(node_0.argv), sorted([ '', '--executable={0}'.format(node_0.executable), '--logfile=foo%I.log', '--pidfile=foo.pid', '-m celery worker --detach', '-n [email protected]', ]), ) self.assertEqual(node_0.pid, 10) self.assertEqual(node_1.name, '*****@*****.**') self.assertEqual( sorted(node_1.argv), sorted([ '', '--executable={0}'.format(node_1.executable), '--logfile=bar%I.log', '--pidfile=bar.pid', '-m celery worker --detach', '-n [email protected]', ]), ) self.assertEqual(node_1.pid, 11) # without callback, should work nodes = p.getpids('celery worker')
def __init__(self): extra = "-B" for worker in CELERY_WORKERS: queue = "" if (len(worker.get("queue")) > 1): queue = ",".join(worker.get("queue")) else: queue = worker.get("queue")[0] node = Node(name=worker.get("name"), append="-A celery" + " -Q " + queue + " --concurrency " + worker.get("concurrency") + " -l info" + " -f " + CELERYD_LOG_FILE, extra_args=extra) self.nodelist.append(node) extra = "" cluster = Cluster(self.nodelist) self.cluster = cluster
def Cluster(self, nodes, cmd=None): return Cluster( nodes, cmd=cmd, env=self.env, on_stopping_preamble=self.on_stopping_preamble, on_send_signal=self.on_send_signal, on_still_waiting_for=self.on_still_waiting_for, on_still_waiting_progress=self.on_still_waiting_progress, on_still_waiting_end=self.on_still_waiting_end, on_node_start=self.on_node_start, on_node_restart=self.on_node_restart, on_node_shutdown_ok=self.on_node_shutdown_ok, on_node_status=self.on_node_status, on_node_signal_dead=self.on_node_signal_dead, on_node_signal=self.on_node_signal, on_node_down=self.on_node_down, on_child_spawn=self.on_child_spawn, on_child_signalled=self.on_child_signalled, on_child_failure=self.on_child_failure, )
def _start_worker_process( app, concurrency=1, pool="solo", loglevel=WORKER_LOGLEVEL, logfile=None, **kwargs ): # type (Celery, int, str, Union[int, str], str, **Any) -> Iterable """Start worker in separate process. Yields: celery.app.worker.Worker: worker instance. """ from celery.apps.multi import Cluster, Node app.set_current() cluster = Cluster([Node("testworker1@%h")]) cluster.start() yield cluster.stopwait()
def _start_worker_process(app: Celery, concurrency: int = 1, pool: str = 'solo', loglevel: Union[int, str] = 'info', logfile: str = None, **kwargs: Any) -> Iterable: """Start worker in separate process. Yields: celery.app.worker.Worker: worker instance. """ from celery.apps.multi import Cluster, Node app.set_current() cluster = Cluster([Node('worker@%h')]) cluster.start() yield cluster.stopwait()
class test_Cluster: def setup(self): self.Popen = self.patching('celery.apps.multi.Popen') self.kill = self.patching('os.kill') self.gethostname = self.patching('celery.apps.multi.gethostname') self.gethostname.return_value = 'example.com' self.Pidfile = self.patching('celery.apps.multi.Pidfile') self.cluster = Cluster( [ Node('*****@*****.**'), Node('*****@*****.**'), Node('*****@*****.**') ], on_stopping_preamble=Mock(name='on_stopping_preamble'), on_send_signal=Mock(name='on_send_signal'), on_still_waiting_for=Mock(name='on_still_waiting_for'), on_still_waiting_progress=Mock(name='on_still_waiting_progress'), on_still_waiting_end=Mock(name='on_still_waiting_end'), on_node_start=Mock(name='on_node_start'), on_node_restart=Mock(name='on_node_restart'), on_node_shutdown_ok=Mock(name='on_node_shutdown_ok'), on_node_status=Mock(name='on_node_status'), on_node_signal=Mock(name='on_node_signal'), on_node_signal_dead=Mock(name='on_node_signal_dead'), on_node_down=Mock(name='on_node_down'), on_child_spawn=Mock(name='on_child_spawn'), on_child_signalled=Mock(name='on_child_signalled'), on_child_failure=Mock(name='on_child_failure'), ) def test_len(self): assert len(self.cluster) == 3 def test_getitem(self): assert self.cluster[0].name == '*****@*****.**' def test_start(self): self.cluster.start_node = Mock(name='start_node') self.cluster.start() self.cluster.start_node.assert_has_calls( call(node) for node in self.cluster) def test_start_node(self): self.cluster._start_node = Mock(name='_start_node') node = self.cluster[0] assert (self.cluster.start_node(node) is self.cluster._start_node.return_value) self.cluster.on_node_start.assert_called_with(node) self.cluster._start_node.assert_called_with(node) self.cluster.on_node_status.assert_called_with( node, self.cluster._start_node(), ) def test__start_node(self): node = self.cluster[0] node.start = Mock(name='node.start') assert self.cluster._start_node(node) is node.start.return_value node.start.assert_called_with( self.cluster.env, on_spawn=self.cluster.on_child_spawn, on_signalled=self.cluster.on_child_signalled, on_failure=self.cluster.on_child_failure, ) def test_send_all(self): nodes = [Mock(name='n1'), Mock(name='n2')] self.cluster.getpids = Mock(name='getpids') self.cluster.getpids.return_value = nodes self.cluster.send_all(15) self.cluster.on_node_signal.assert_has_calls( call(node, 'TERM') for node in nodes) for node in nodes: node.send.assert_called_with(15, self.cluster.on_node_signal_dead) @skip.if_win32() def test_kill(self): self.cluster.send_all = Mock(name='.send_all') self.cluster.kill() self.cluster.send_all.assert_called_with(signal.SIGKILL) def test_getpids(self): self.gethostname.return_value = 'e.com' self.prepare_pidfile_for_getpids(self.Pidfile) callback = Mock() p = Cluster([ Node('*****@*****.**'), Node('*****@*****.**'), Node('*****@*****.**'), ]) nodes = p.getpids(on_down=callback) node_0, node_1 = nodes assert node_0.name == '*****@*****.**' assert sorted(node_0.argv) == sorted([ '', '--executable={0}'.format(node_0.executable), '--logfile={}'.format( os.path.normpath('/var/log/celery/foo%I.log')), '--pidfile={}'.format(os.path.normpath('/var/run/celery/foo.pid')), '-m celery worker --detach', '-n [email protected]', ]) assert node_0.pid == 10 assert node_1.name == '*****@*****.**' assert sorted(node_1.argv) == sorted([ '', '--executable={0}'.format(node_1.executable), '--logfile={}'.format( os.path.normpath('/var/log/celery/bar%I.log')), '--pidfile={}'.format(os.path.normpath('/var/run/celery/bar.pid')), '-m celery worker --detach', '-n [email protected]', ]) assert node_1.pid == 11 # without callback, should work nodes = p.getpids('celery worker') def prepare_pidfile_for_getpids(self, Pidfile): class pids(object): def __init__(self, path): self.path = path def read_pid(self): try: return { os.path.normpath('/var/run/celery/foo.pid'): 10, os.path.normpath('/var/run/celery/bar.pid'): 11 }[self.path] except KeyError: raise ValueError() self.Pidfile.side_effect = pids
class test_Cluster: def setup(self): self.Popen = self.patching('celery.apps.multi.Popen') self.kill = self.patching('os.kill') self.gethostname = self.patching('celery.apps.multi.gethostname') self.gethostname.return_value = 'example.com' self.Pidfile = self.patching('celery.apps.multi.Pidfile') self.cluster = Cluster( [Node('*****@*****.**'), Node('*****@*****.**'), Node('*****@*****.**')], on_stopping_preamble=Mock(name='on_stopping_preamble'), on_send_signal=Mock(name='on_send_signal'), on_still_waiting_for=Mock(name='on_still_waiting_for'), on_still_waiting_progress=Mock(name='on_still_waiting_progress'), on_still_waiting_end=Mock(name='on_still_waiting_end'), on_node_start=Mock(name='on_node_start'), on_node_restart=Mock(name='on_node_restart'), on_node_shutdown_ok=Mock(name='on_node_shutdown_ok'), on_node_status=Mock(name='on_node_status'), on_node_signal=Mock(name='on_node_signal'), on_node_signal_dead=Mock(name='on_node_signal_dead'), on_node_down=Mock(name='on_node_down'), on_child_spawn=Mock(name='on_child_spawn'), on_child_signalled=Mock(name='on_child_signalled'), on_child_failure=Mock(name='on_child_failure'), ) def test_len(self): assert len(self.cluster) == 3 def test_getitem(self): assert self.cluster[0].name == '*****@*****.**' def test_start(self): self.cluster.start_node = Mock(name='start_node') self.cluster.start() self.cluster.start_node.assert_has_calls( call(node) for node in self.cluster ) def test_start_node(self): self.cluster._start_node = Mock(name='_start_node') node = self.cluster[0] assert (self.cluster.start_node(node) is self.cluster._start_node.return_value) self.cluster.on_node_start.assert_called_with(node) self.cluster._start_node.assert_called_with(node) self.cluster.on_node_status.assert_called_with( node, self.cluster._start_node(), ) def test__start_node(self): node = self.cluster[0] node.start = Mock(name='node.start') assert self.cluster._start_node(node) is node.start.return_value node.start.assert_called_with( self.cluster.env, on_spawn=self.cluster.on_child_spawn, on_signalled=self.cluster.on_child_signalled, on_failure=self.cluster.on_child_failure, ) def test_send_all(self): nodes = [Mock(name='n1'), Mock(name='n2')] self.cluster.getpids = Mock(name='getpids') self.cluster.getpids.return_value = nodes self.cluster.send_all(15) self.cluster.on_node_signal.assert_has_calls( call(node, 'TERM') for node in nodes ) for node in nodes: node.send.assert_called_with(15, self.cluster.on_node_signal_dead) @skip.if_win32() def test_kill(self): self.cluster.send_all = Mock(name='.send_all') self.cluster.kill() self.cluster.send_all.assert_called_with(signal.SIGKILL) def test_getpids(self): self.gethostname.return_value = 'e.com' self.prepare_pidfile_for_getpids(self.Pidfile) callback = Mock() p = Cluster([ Node('*****@*****.**'), Node('*****@*****.**'), Node('*****@*****.**'), ]) nodes = p.getpids(on_down=callback) node_0, node_1 = nodes assert node_0.name == '*****@*****.**' assert sorted(node_0.argv) == sorted([ '', '--executable={0}'.format(node_0.executable), '--logfile=foo%I.log', '--pidfile=foo.pid', '-m celery worker --detach', '-n [email protected]', ]) assert node_0.pid == 10 assert node_1.name == '*****@*****.**' assert sorted(node_1.argv) == sorted([ '', '--executable={0}'.format(node_1.executable), '--logfile=bar%I.log', '--pidfile=bar.pid', '-m celery worker --detach', '-n [email protected]', ]) assert node_1.pid == 11 # without callback, should work nodes = p.getpids('celery worker') def prepare_pidfile_for_getpids(self, Pidfile): class pids(object): def __init__(self, path): self.path = path def read_pid(self): try: return {'foo.pid': 10, 'bar.pid': 11}[self.path] except KeyError: raise ValueError() self.Pidfile.side_effect = pids
class test_Cluster(AppCase): def setup(self): self.Popen = self.patch('celery.apps.multi.Popen') self.kill = self.patch('os.kill') self.gethostname = self.patch('celery.apps.multi.gethostname') self.gethostname.return_value = 'example.com' self.Pidfile = self.patch('celery.apps.multi.Pidfile') self.cluster = Cluster( ['foo', 'bar', 'baz'], on_stopping_preamble=Mock(name='on_stopping_preamble'), on_send_signal=Mock(name='on_send_signal'), on_still_waiting_for=Mock(name='on_still_waiting_for'), on_still_waiting_progress=Mock(name='on_still_waiting_progress'), on_still_waiting_end=Mock(name='on_still_waiting_end'), on_node_start=Mock(name='on_node_start'), on_node_restart=Mock(name='on_node_restart'), on_node_shutdown_ok=Mock(name='on_node_shutdown_ok'), on_node_status=Mock(name='on_node_status'), on_node_signal=Mock(name='on_node_signal'), on_node_signal_dead=Mock(name='on_node_signal_dead'), on_node_down=Mock(name='on_node_down'), on_child_spawn=Mock(name='on_child_spawn'), on_child_signalled=Mock(name='on_child_signalled'), on_child_failure=Mock(name='on_child_failure'), ) def test_len(self): self.assertEqual(len(self.cluster), 3) def test_getitem(self): self.assertEqual(self.cluster[0].name, '*****@*****.**') def test_start(self): self.cluster.start_node = Mock(name='start_node') self.cluster.start() self.cluster.start_node.assert_has_calls( call(node) for node in self.cluster ) def test_start_node(self): self.cluster._start_node = Mock(name='_start_node') node = self.cluster[0] self.assertIs( self.cluster.start_node(node), self.cluster._start_node.return_value, ) self.cluster.on_node_start.assert_called_with(node) self.cluster._start_node.assert_called_with(node) self.cluster.on_node_status.assert_called_with( node, self.cluster._start_node(), ) def test__start_node(self): node = self.cluster[0] node.start = Mock(name='node.start') self.assertIs( self.cluster._start_node(node), node.start.return_value, ) node.start.assert_called_with( self.cluster.env, on_spawn=self.cluster.on_child_spawn, on_signalled=self.cluster.on_child_signalled, on_failure=self.cluster.on_child_failure, ) def test_send_all(self): nodes = [Mock(name='n1'), Mock(name='n2')] self.cluster.getpids = Mock(name='getpids') self.cluster.getpids.return_value = nodes self.cluster.send_all(15) self.cluster.on_node_signal.assert_has_calls( call(node, 'TERM') for node in nodes ) for node in nodes: node.send.assert_called_with(15, self.cluster.on_node_signal_dead) def test_kill(self): self.cluster.send_all = Mock(name='.send_all') self.cluster.kill() self.cluster.send_all.assert_called_with(signal.SIGKILL) def test_getpids(self): self.gethostname.return_value = 'e.com' self.prepare_pidfile_for_getpids(self.Pidfile) callback = Mock() p = Cluster(['foo', 'bar', 'baz']) nodes = p.getpids(on_down=callback) node_0, node_1 = nodes self.assertEqual(node_0.name, '*****@*****.**') self.assertEqual( sorted(node_0.argv), sorted([ '', '--executable={0}'.format(node_0.executable), '--logfile=foo%I.log', '--pidfile=foo.pid', '-m celery worker --detach', '-n [email protected]', ]), ) self.assertEqual(node_0.pid, 10) self.assertEqual(node_1.name, '*****@*****.**') self.assertEqual( sorted(node_1.argv), sorted([ '', '--executable={0}'.format(node_1.executable), '--logfile=bar%I.log', '--pidfile=bar.pid', '-m celery worker --detach', '-n [email protected]', ]), ) self.assertEqual(node_1.pid, 11) # without callback, should work nodes = p.getpids('celery worker') def prepare_pidfile_for_getpids(self, Pidfile): class pids(object): def __init__(self, path): self.path = path def read_pid(self): try: return {'foo.pid': 10, 'bar.pid': 11}[self.path] except KeyError: raise ValueError() self.Pidfile.side_effect = pids