Example #1
0
    def test_execute(self):
        x = CeleryCommand(app=self.app)
        Help = x.commands['help'] = Mock()
        help = Help.return_value = Mock()
        x.execute('fooox', ['a'])
        help.run_from_argv.assert_called_with(x.prog_name, [], command='help')
        help.reset()
        x.execute('help', ['help'])
        help.run_from_argv.assert_called_with(x.prog_name, [], command='help')

        Dummy = x.commands['dummy'] = Mock()
        dummy = Dummy.return_value = Mock()
        exc = dummy.run_from_argv.side_effect = Error(
            'foo', status='EX_FAILURE',
        )
        x.on_error = Mock(name='on_error')
        help.reset()
        x.execute('dummy', ['dummy'])
        x.on_error.assert_called_with(exc)
        dummy.run_from_argv.assert_called_with(
            x.prog_name, [], command='dummy',
        )
        help.run_from_argv.assert_called_with(
            x.prog_name, [], command='help',
        )

        exc = dummy.run_from_argv.side_effect = x.UsageError('foo')
        x.on_usage_error = Mock()
        x.execute('dummy', ['dummy'])
        x.on_usage_error.assert_called_with(exc)
Example #2
0
 def test_prepare_prog_name(self):
     x = CeleryCommand(app=self.app)
     main = Mock(name='__main__')
     main.__file__ = '/opt/foo.py'
     with patch.dict(sys.modules, __main__=main):
         self.assertEqual(x.prepare_prog_name('__main__.py'), '/opt/foo.py')
         self.assertEqual(x.prepare_prog_name('celery'), 'celery')
Example #3
0
 def test_relocate_args_from_start(self):
     x = CeleryCommand(app=self.app)
     assert x._relocate_args_from_start(None) == []
     relargs1 = x._relocate_args_from_start([
         '-l',
         'debug',
         'worker',
         '-c',
         '3',
         '--foo',
     ])
     assert relargs1 == ['worker', '-c', '3', '--foo', '-l', 'debug']
     relargs2 = x._relocate_args_from_start([
         '--pool=gevent',
         '-l',
         'debug',
         'worker',
         '--foo',
         '-c',
         '3',
     ])
     assert relargs2 == [
         'worker',
         '--foo',
         '-c',
         '3',
         '--pool=gevent',
         '-l',
         'debug',
     ]
     assert x._relocate_args_from_start(['foo', '--foo=1']) == [
         'foo',
         '--foo=1',
     ]
Example #4
0
def celery_cmd(args):
    # remove the celery shell command
    next(funcs for group, funcs, _ in command_classes if group == 'Main').remove('shell')
    del CeleryCommand.commands['shell']

    if args and args[0] == 'flower':
        # Somehow flower hangs when executing it using CeleryCommand() so we simply exec it directly.
        # It doesn't really need the celery config anyway (besides the broker url)

        try:
            import flower
        except ImportError:
            print(cformat('%{red!}Flower is not installed'))
            sys.exit(1)

        auth_args = ['[email protected]', '--auth_provider=flower.views.auth.GithubLoginHandler']
        auth_env = {'FLOWER_OAUTH2_KEY': '',
                    'FLOWER_OAUTH2_SECRET': '',
                    'FLOWER_OAUTH2_REDIRECT_URI': 'http://localhost:5555/login'
                    }

        args = ['celery', '-b', config.CELERY_BROKER] + args
        env = dict(os.environ, **auth_env)
        os.execvpe('celery', args, env)
    elif args and args[0] == 'shell':
        print(cformat('%{red!}Please use `snms shell`.'))
        sys.exit(1)
    else:
        CeleryCommand(celery).execute_from_commandline(['snms celery'] + args)
Example #5
0
 def test_remove_options_at_beginning(self):
     x = CeleryCommand(app=self.app)
     self.assertEqual(x.remove_options_at_beginning(None), [])
     self.assertEqual(x.remove_options_at_beginning(["-c 3", "--foo"]), [])
     self.assertEqual(x.remove_options_at_beginning(["--foo", "-c 3"]), [])
     self.assertEqual(x.remove_options_at_beginning(["foo", "--foo=1"]),
                      ["foo", "--foo=1"])
Example #6
0
 def test_remove_options_at_beginning(self):
     x = CeleryCommand(app=self.app)
     self.assertEqual(x.remove_options_at_beginning(None), [])
     self.assertEqual(x.remove_options_at_beginning(['-c 3', '--foo']), [])
     self.assertEqual(x.remove_options_at_beginning(['--foo', '-c 3']), [])
     self.assertEqual(x.remove_options_at_beginning(
         ['foo', '--foo=1']), ['foo', '--foo=1'])
Example #7
0
    def test_handle_argv(self):
        x = CeleryCommand(app=self.app)
        x.execute = Mock()
        x.handle_argv('celery', [])
        x.execute.assert_called_with('help', ['help'])

        x.handle_argv('celery', ['start', 'foo'])
        x.execute.assert_called_with('start', ['start', 'foo'])
Example #8
0
    def test_handle_argv(self):
        x = CeleryCommand(app=self.app)
        x.execute = Mock()
        x.handle_argv("celery", [])
        x.execute.assert_called_with("help", ["help"])

        x.handle_argv("celery", ["start", "foo"])
        x.execute.assert_called_with("start", ["start", "foo"])
Example #9
0
def celery_cmd(args):
    # remove the celery shell command
    next(funcs for group, funcs, _ in command_classes
         if group == 'Main').remove('shell')
    del CeleryCommand.commands['shell']

    if args and args[0] == 'flower':
        # Somehow flower hangs when executing it using CeleryCommand() so we simply exec it directly.
        # It doesn't really need the celery config anyway (besides the broker url)

        try:
            import flower  # noqa: F401
        except ImportError:
            print(cformat('%{red!}Flower is not installed'))
            sys.exit(1)

        app = OAuthApplication.find_one(system_app_type=SystemAppType.flower)
        if not app.redirect_uris:
            print(
                cformat(
                    '%{yellow!}Authentication will fail unless you configure the redirect url for the {} OAuth '
                    'application in the administration area.').format(
                        app.name))

        print(
            cformat('%{green!}Only Indico admins will have access to flower.'))
        print(
            cformat(
                '%{yellow}Note that revoking admin privileges will not revoke Flower access.'
            ))
        print(cformat('%{yellow}To force re-authentication, restart Flower.'))
        auth_args = [
            '--auth=^Indico Admin$',
            '--auth_provider=indico.core.celery.flower.FlowerAuthHandler'
        ]
        auth_env = {
            'INDICO_FLOWER_CLIENT_ID':
            app.client_id,
            'INDICO_FLOWER_CLIENT_SECRET':
            app.client_secret,
            'INDICO_FLOWER_AUTHORIZE_URL':
            url_for('oauth.oauth_authorize', _external=True),
            'INDICO_FLOWER_TOKEN_URL':
            url_for('oauth.oauth_token', _external=True),
            'INDICO_FLOWER_USER_URL':
            url_for('users.authenticated_user', _external=True)
        }
        if config.FLOWER_URL:
            auth_env['INDICO_FLOWER_URL'] = config.FLOWER_URL
        args = ['celery', '-b', config.CELERY_BROKER] + args + auth_args
        env = dict(os.environ, **auth_env)
        os.execvpe('celery', args, env)
    elif args and args[0] == 'shell':
        print(cformat('%{red!}Please use `indico shell`.'))
        sys.exit(1)
    else:
        CeleryCommand(celery).execute_from_commandline(['indico celery'] +
                                                       args)
Example #10
0
def celery(ctx):
    """Manage the Celery task daemon."""
    from .tasks import celery
    # remove the celery shell command
    next(funcs for group, funcs, _ in command_classes
         if group == 'Main').remove('shell')
    del CeleryCommand.commands['shell']
    CeleryCommand(celery).execute_from_commandline(['cephalopod celery'] +
                                                   ctx.args)
Example #11
0
 def test_short_and_long_arguments_be_the_same(self):
     for arg in "--app", "-A":
         appstr = '.'.join([__name__, 'APP'])
         x = CeleryCommand(app=self.app)
         x.execute = Mock()
         with pytest.raises(SystemExit):
             x.execute_from_commandline(['celery', arg, appstr, 'worker'])
         assert x.execute.called
         assert x.execute.call_args[0]
         assert x.execute.call_args[0][0] == "worker"
Example #12
0
 def test_load_extensions_commands(self):
     with patch('celery.bin.celery.Extensions') as Ext:
         prev, mod.command_classes = list(mod.command_classes), Mock()
         try:
             ext = Ext.return_value = Mock(name='Extension')
             ext.load.return_value = ['foo', 'bar']
             x = CeleryCommand(app=self.app)
             x.load_extension_commands()
             mod.command_classes.append.assert_called_with(
                 ('Extensions', ['foo', 'bar'], 'magenta'), )
         finally:
             mod.command_classes = prev
Example #13
0
    def test_execute_from_commandline(self):
        x = CeleryCommand(app=self.app)
        x.handle_argv = Mock()
        x.handle_argv.return_value = 1
        with self.assertRaises(SystemExit):
            x.execute_from_commandline()

        x.handle_argv.return_value = True
        with self.assertRaises(SystemExit):
            x.execute_from_commandline()

        x.handle_argv.side_effect = KeyboardInterrupt()
        with self.assertRaises(SystemExit):
            x.execute_from_commandline()
Example #14
0
    def run(self, args):
        # disable the zodb commit hook
        update_session_options(db)
        # remove the celery shell command
        next(funcs for group, funcs, _ in command_classes if group == 'Main').remove('shell')
        del CeleryCommand.commands['shell']

        if args and args[0] == 'flower':
            # Somehow flower hangs when executing it using CeleryCommand() so we simply exec it directly.
            # It doesn't really need the celery config anyway (besides the broker url)

            try:
                import flower
            except ImportError:
                print cformat('%{red!}Flower is not installed')
                sys.exit(1)

            client_id = Config.getInstance().getFlowerClientId()
            if client_id:
                app = OAuthApplication.find_first(client_id=client_id)
                if app is None:
                    print cformat('%{red!}There is no OAuth application with the client id {}.').format(client_id)
                    sys.exit(1)
                elif 'read:user' not in app.default_scopes:
                    print cformat('%{red!}The {} application needs the read:user scope.').format(app.name)
                    sys.exit(1)
                print cformat('%{green!}Only Indico admins will have access to flower.')
                print cformat('%{yellow}Note that revoking admin privileges will not revoke Flower access.')
                print cformat('%{yellow}To force re-authentication, restart Flower.')
                auth_args = ['--auth=^Indico Admin$', '--auth_provider=indico.core.celery.flower.FlowerAuthHandler']
                auth_env = {'INDICO_FLOWER_CLIENT_ID': app.client_id,
                            'INDICO_FLOWER_CLIENT_SECRET': app.client_secret,
                            'INDICO_FLOWER_AUTHORIZE_URL': url_for('oauth.oauth_authorize', _external=True),
                            'INDICO_FLOWER_TOKEN_URL': url_for('oauth.oauth_token', _external=True),
                            'INDICO_FLOWER_USER_URL': url_for('users.authenticated_user', _external=True)}
            else:
                print cformat('%{red!}WARNING: %{yellow!}Flower authentication is disabled.')
                print cformat('%{yellow!}Having access to Flower allows one to shutdown Celery workers.')
                print
                auth_args = []
                auth_env = {}
            args = ['celery', '-b', Config.getInstance().getCeleryBroker()] + args + auth_args
            env = dict(os.environ, **auth_env)
            os.execvpe('celery', args, env)
        elif args and args[0] == 'shell':
            print cformat('%{red!}Please use `indico shell`.')
            sys.exit(1)
        else:
            CeleryCommand(celery).execute_from_commandline(['indico celery'] + args)
Example #15
0
    def test_execute(self):
        x = CeleryCommand(app=self.app)
        Help = x.commands['help'] = Mock()
        help = Help.return_value = Mock()
        x.execute('fooox', ['a'])
        help.run_from_argv.assert_called_with(x.prog_name, ['help'])
        help.reset()
        x.execute('help', ['help'])
        help.run_from_argv.assert_called_with(x.prog_name, ['help'])

        Dummy = x.commands['dummy'] = Mock()
        dummy = Dummy.return_value = Mock()
        dummy.run_from_argv.side_effect = Error('foo', status='EX_FAILURE')
        help.reset()
        x.execute('dummy', ['dummy'])
        dummy.run_from_argv.assert_called_with(x.prog_name, ['dummy'])
        help.run_from_argv.assert_called_with(x.prog_name, ['dummy'])
Example #16
0
    def test_execute(self):
        x = CeleryCommand(app=self.app)
        Help = x.commands["help"] = Mock()
        help = Help.return_value = Mock()
        x.execute("fooox", ["a"])
        help.run_from_argv.assert_called_with(x.prog_name, ["help"])
        help.reset()
        x.execute("help", ["help"])
        help.run_from_argv.assert_called_with(x.prog_name, ["help"])

        Dummy = x.commands["dummy"] = Mock()
        dummy = Dummy.return_value = Mock()
        dummy.run_from_argv.side_effect = Error("foo", status="EX_FAILURE")
        help.reset()
        x.execute("dummy", ["dummy"])
        dummy.run_from_argv.assert_called_with(x.prog_name, ["dummy"])
        help.run_from_argv.assert_called_with(x.prog_name, ["dummy"])
Example #17
0
 def test_relocate_args_from_start(self):
     x = CeleryCommand(app=self.app)
     self.assertEqual(x._relocate_args_from_start(None), [])
     self.assertEqual(
         x._relocate_args_from_start(
             ['-l', 'debug', 'worker', '-c', '3', '--foo'], ),
         ['worker', '-c', '3', '--foo', '-l', 'debug'],
     )
     self.assertEqual(
         x._relocate_args_from_start(
             ['--pool=gevent', '-l', 'debug', 'worker', '--foo', '-c', '3'
              ], ),
         ['worker', '--foo', '-c', '3', '--pool=gevent', '-l', 'debug'],
     )
     self.assertEqual(
         x._relocate_args_from_start(['foo', '--foo=1']),
         ['foo', '--foo=1'],
     )
Example #18
0
    def test_execute_from_commandline(self):
        x = CeleryCommand(app=self.app)
        x.handle_argv = Mock()
        x.handle_argv.return_value = 1
        with self.assertRaises(SystemExit):
            x.execute_from_commandline()

        x.handle_argv.return_value = True
        with self.assertRaises(SystemExit):
            x.execute_from_commandline()

        x.handle_argv.side_effect = KeyboardInterrupt()
        with self.assertRaises(SystemExit):
            x.execute_from_commandline()

        x.respects_app_option = True
        with self.assertRaises(SystemExit):
            x.execute_from_commandline(['celery', 'multi'])
        self.assertFalse(x.respects_app_option)
        x.respects_app_option = True
        with self.assertRaises(SystemExit):
            x.execute_from_commandline(['manage.py', 'celery', 'multi'])
        self.assertFalse(x.respects_app_option)
Example #19
0
 def test_load_extensions_no_commands(self):
     with patch('celery.bin.celery.Extensions') as Ext:
         ext = Ext.return_value = Mock(name='Extension')
         ext.load.return_value = None
         x = CeleryCommand(app=self.app)
         x.load_extension_commands()
Example #20
0
 def test_with_pool_option(self):
     x = CeleryCommand(app=self.app)
     self.assertIsNone(x.with_pool_option(['celery', 'events']))
     self.assertTrue(x.with_pool_option(['celery', 'worker']))
     self.assertTrue(x.with_pool_option(['manage.py', 'celery', 'worker']))
Example #21
0
def start_celery(ctx):
    """Preconfigured wrapper around the 'celery' command."""
    CeleryCommand(celery).execute_from_commandline(["flaskbb celery"] +
                                                   ctx.args)
Example #22
0
 def test_on_usage_error(self):
     x = CeleryCommand(app=self.app)
     x.error = Mock()
     x.on_usage_error(x.UsageError('foo'), command=None)
     x.error.assert_called()
     x.on_usage_error(x.UsageError('foo'), command='dummy')
Example #23
0
 def test_with_pool_option(self):
     x = CeleryCommand(app=self.app)
     assert x.with_pool_option(['celery', 'events']) is None
     assert x.with_pool_option(['celery', 'worker'])
     assert x.with_pool_option(['manage.py', 'celery', 'worker'])
def start_celery(argv):
    cmd = CeleryCommand()
    cmd.maybe_patch_concurrency()
    cmd.execute_from_commandline(argv)