Completed
Push — master ( dc3db7...c94c8e )
by George
02:13
created

LoaferManager.run()   A

Complexity

Conditions 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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

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 .runners import LoaferRunner
8
9
logger = logging.getLogger(__name__)
10
11
12
class LoaferManager:
13
14
    def __init__(self, routes, consumers, runner=None):
15
        self.runner = runner or LoaferRunner(on_stop_callback=self.on_loop__stop)
16
        self.routes = routes
17
        self.consumers = consumers
18
19
    @cached_property
20
    def dispatcher(self):
21
        return LoaferDispatcher(self.routes, self.consumers)
22
23
    def run(self, forever=True):
24
        self._future = asyncio.gather(self.dispatcher.dispatch_providers())
0 ignored issues
show
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...
25
        self._future.add_done_callback(self.on_future__errors)
26
        self.runner.start(self._future, run_forever=forever)
27
28
    #
29
    # Callbacks
30
    #
31
32
    def on_future__errors(self, future):
33
        exc = future.exception()
34
        # Unhandled errors crashes the event loop execution
35
        if isinstance(exc, BaseException):
36
            logger.critical('Fatal error caught: {!r}'.format(exc))
37
            self.runner.stop()
38
39
    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...
40
        logger.debug('Stopping consumers ...')
41
        self.dispatcher.stop_providers()
42
43
        if hasattr(self, '_future'):
44
            logger.debug('Cancel schedulled operations ...')
45
            self._future.cancel()
46