Passed
Push — master ( d3d957...12fe94 )
by George
01:20
created

LoaferManager.dispatcher()   B

Complexity

Conditions 6

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
cc 6
c 4
b 0
f 1
dl 0
loc 9
rs 8
1
import asyncio
2
import logging
3
4
from cached_property import cached_property
5
6
from .dispatchers import LoaferDispatcher
0 ignored issues
show
Configuration introduced by
Unable to import 'dispatchers' (invalid syntax (<string>, line 17))

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
7
from .exceptions import ConfigurationError
8
from .routes import Route
0 ignored issues
show
Configuration introduced by
Unable to import 'routes' (invalid syntax (<string>, line 56))

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
9
from .runners import LoaferRunner
10
11
logger = logging.getLogger(__name__)
12
13
14
class LoaferManager:
15
16
    def __init__(self, routes, runner=None, _concurrency_limit=None, _max_threads=None):
17
        self._concurrency_limit = _concurrency_limit
18
        if runner is None:
19
            self.runner = LoaferRunner(on_stop_callback=self.on_loop__stop, max_workers=_max_threads)
20
        else:
21
            self.runner = runner
22
23
        self.routes = routes
24
25
    @cached_property
26
    def dispatcher(self):
27
        if not (self.routes and all(isinstance(r, Route) for r in self.routes)):
28
            raise ConfigurationError('invalid routes to dispatch, routes={}'.format(self.routes))
29
30
        if not any(r.enabled for r in self.routes):
31
            raise ConfigurationError('all routes are disabled, routes={}'.format(self.routes))
32
33
        return LoaferDispatcher(self.routes, max_jobs=self._concurrency_limit)
34
35
    def run(self, forever=True):
36
        loop = self.runner.loop
37
        self._future = asyncio.ensure_future(
0 ignored issues
show
Bug introduced by
The Module asyncio does not seem to have a member named ensure_future.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
Coding Style introduced by
The attribute _future was defined outside __init__.

It is generally a good practice to initialize all attributes to default values in the __init__ method:

class Foo:
    def __init__(self, x=None):
        self.x = x
Loading history...
38
            self.dispatcher.dispatch_providers(loop, forever=forever),
39
            loop=loop,
40
        )
41
        self._future.add_done_callback(self.on_future__errors)
42
        self.runner.start(self._future, run_forever=forever)
43
44
    #
45
    # Callbacks
46
    #
47
48
    def on_future__errors(self, future):
49
        exc = future.exception()
50
        # Unhandled errors crashes the event loop execution
51
        if isinstance(exc, BaseException):
52
            logger.critical('fatal error caught: {!r}'.format(exc))
53
            self.runner.stop()
54
55
    def on_loop__stop(self, *args, **kwargs):
0 ignored issues
show
Unused Code introduced by
The argument args seems to be unused.
Loading history...
Unused Code introduced by
The argument kwargs seems to be unused.
Loading history...
56
        logger.info('cancel schedulled operations ...')
57
        for task in asyncio.Task.all_tasks(self.runner.loop):
58
            logger.debug('cancelling {}'.format(task))
59
            task.cancel()
60
61
        if hasattr(self, '_future'):
62
            self._future.cancel()
63
64
        self.dispatcher.stop()
65