예제 #1
0
def test_dead_pid_in_lock_file(tmpdir):
    """Testing whether the process checker returns false when there is a dead pid in the lock file."""
    dead_pid = 134824733
    create_lock_file_with_pid(tmpdir, dead_pid)
    process_checker = ProcessChecker(state_directory=Path(tmpdir))
    assert not process_checker.is_pid_running(dead_pid)
    assert not process_checker.already_running
예제 #2
0
def test_invalid_pid_in_lock_file(tmpdir):
    """
    Testing pid should be -1 if the lock file is invalid
    """
    with open(tmpdir / LOCK_FILE_NAME, 'wb') as lock_file:
        lock_file.write(b"Hello world")

    process_checker = ProcessChecker(state_directory=Path(tmpdir))
    assert process_checker.get_pid_from_lock_file() == -1
예제 #3
0
def test_own_pid_in_lock_file(tmpdir):
    """
    Testing whether the process checker returns True when it finds its own pid in the lock file
    """
    create_lock_file_with_pid(tmpdir, os.getpid())
    process_checker = ProcessChecker(state_directory=Path(tmpdir))
    assert process_checker.already_running
예제 #4
0
    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()
예제 #5
0
def run_core(api_port, api_key, root_state_dir, parsed_args):
    should_kill_other_tribler_instances(root_state_dir)
    logger.info('Running Core' +
                ' in gui_test_mode' if parsed_args.gui_test_mode else '')
    load_logger_config('tribler-core', root_state_dir)

    # Check if we are already running a Tribler instance
    process_checker = ProcessChecker(root_state_dir)
    if process_checker.already_running:
        logger.info('Core is already running, exiting')
        sys.exit(1)
    process_checker.create_lock_file()
    version_history = VersionHistory(root_state_dir)
    state_dir = version_history.code_version.directory
    try:
        run_tribler_core_session(api_port,
                                 api_key,
                                 state_dir,
                                 gui_test_mode=parsed_args.gui_test_mode)
    finally:
        logger.info('Remove lock file')
        process_checker.remove_lock_file()
예제 #6
0
def test_other_instance_running(tmpdir, background_process):
    """Testing whether the process checker returns true when another process is running."""
    create_lock_file_with_pid(tmpdir, background_process.pid)
    process_checker = ProcessChecker(state_directory=Path(tmpdir))
    assert process_checker.is_pid_running(background_process.pid)
    assert process_checker.already_running
예제 #7
0
def process_checker(tmpdir):
    return ProcessChecker(state_directory=Path(tmpdir))
예제 #8
0
파일: check_os.py 프로젝트: xoriole/tribler
def get_existing_tribler_pid(root_state_dir):
    """ Get PID of existing instance if present from the lock file (if any)"""
    process_checker = ProcessChecker(root_state_dir)
    if process_checker.already_running:
        return process_checker.get_pid_from_lock_file()
    return -1
예제 #9
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")