예제 #1
0
    async def _start_session(self):
        self.logger.info(f"Starting Tribler session with config: {self.config}")
        self.session = Session(self.config, self.components)
        self.session.set_as_default()
        await self.session.start()

        self.logger.info("Tribler session started")
예제 #2
0
async def start_crawler(tribler_config):

    # We use our own community loader
    loader = IPv8CommunityLoader()
    loader.set_launcher(BandwidthCommunityCrawlerLauncher())
    session = Session(tribler_config, community_loader=loader)

    await session.start()
예제 #3
0
async def crawler_session(session_config: TriblerConfig):
    session = Session(session_config,
                      [KeyComponent(), Ipv8Component(), BandwidthAccountingComponent(crawler_mode=True)])
    signal.signal(signal.SIGTERM, lambda signum, stack: session.shutdown_event.set)
    async with session.start():
        await session.shutdown_event.wait()
예제 #4
0
class TinyTriblerService:
    """Lightweight tribler service, that used for experiments.

    All overlays are disabled by default.
    """
    def __init__(self,
                 config,
                 components: List[Component],
                 timeout_in_sec=None,
                 working_dir=Path('/tmp/tribler')):
        self.logger = logging.getLogger(self.__class__.__name__)

        self.session = None
        self.process_checker = None
        self.working_dir = working_dir
        self.config = config
        self.timeout_in_sec = timeout_in_sec
        self.components = components

    async def on_tribler_started(self):
        """Function will calls after the Tribler session is started

        It is good place to add a custom code.
        """

    async def start_tribler(self):
        self.logger.info(
            f'Starting tribler instance in directory: {self.working_dir}')

        self._check_already_running()
        await self._start_session()

        if self.timeout_in_sec:
            asyncio.create_task(self._terminate_by_timeout())

        self._enable_graceful_shutdown()
        await self.on_tribler_started()

    async def _start_session(self):
        self.logger.info(
            f"Starting Tribler session with config: {self.config}")
        self.session = Session(self.config, self.components)
        self.session.set_as_default()
        await self.session.start_components()

        self.logger.info("Tribler session started")

    def _check_already_running(self):
        self.logger.info(
            f'Check if we are already running a Tribler instance in: {self.working_dir}'
        )

        root_state_dir = get_root_state_directory()
        self.process_checker = ProcessChecker(root_state_dir)
        if self.process_checker.already_running:
            self.logger.error(
                f"Another Tribler instance is already using directory: {self.working_dir}"
            )
            asyncio.get_running_loop().stop()

    def _enable_graceful_shutdown(self):
        self.logger.info("Enabling graceful shutdown")

        def signal_handler(signum, frame):
            self.logger.info(
                f"Received shut down signal {signum} in frame {frame}")
            self._graceful_shutdown()

        signal.signal(signal.SIGINT, signal_handler)
        signal.signal(signal.SIGTERM, signal_handler)

    def _graceful_shutdown(self):
        self.logger.info("Shutdown gracefully")

        task = asyncio.create_task(self.session.shutdown())
        task.add_done_callback(
            lambda result: asyncio.get_running_loop().stop())

    async def _terminate_by_timeout(self):
        self.logger.info(
            f"Scheduling terminating by timeout {self.timeout_in_sec}s from now"
        )
        await asyncio.sleep(self.timeout_in_sec)

        self.logger.info("Terminating by timeout")
        self._graceful_shutdown()
예제 #5
0
    async def start_tribler(self, options):
        """
        Main method to startup Tribler.
        """
        async def signal_handler(sig):
            print(f"Received shut down signal {sig}")
            if not self._stopping:
                self._stopping = True
                await self.session.shutdown()
                print("Tribler shut down")
                get_event_loop().stop()
                self.process_checker.remove_lock_file()

        signal.signal(signal.SIGINT,
                      lambda sig, _: ensure_future(signal_handler(sig)))
        signal.signal(signal.SIGTERM,
                      lambda sig, _: ensure_future(signal_handler(sig)))

        statedir = Path(options.statedir
                        or Path(get_appstate_dir(), '.Tribler'))
        config = TriblerConfig.load(file=statedir / 'triblerd.conf',
                                    state_dir=statedir)

        # Check if we are already running a Tribler instance
        root_state_dir = get_root_state_directory()
        self.process_checker = ProcessChecker(root_state_dir)
        if self.process_checker.already_running:
            print(
                f"Another Tribler instance is already using statedir {config.state_dir}"
            )
            get_event_loop().stop()
            return

        print("Starting Tribler")

        if options.restapi > 0:
            config.api.http_enabled = True
            config.api.http_port = options.restapi

        if options.ipv8 > 0:
            config.ipv8.port = options.ipv8
        elif options.ipv8 == 0:
            config.ipv8.enabled = False

        if options.libtorrent != -1 and options.libtorrent > 0:
            config.libtorrent.port = options.libtorrent

        if options.ipv8_bootstrap_override is not None:
            config.ipv8.bootstrap_override = options.ipv8_bootstrap_override

        if options.testnet:
            config.tunnel_community.testnet = True
            config.chant.testnet = True
            config.bandwidth_accounting.testnet = True

        self.session = Session(config)
        try:
            await self.session.start_components()
        except Exception as e:
            print(str(e))
            get_event_loop().stop()
        else:
            print("Tribler started")