How to use the pgctl.flock.flock function in pgctl

To help you get started, we’ve selected a few pgctl examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Yelp / pgctl / tests / unit / flock.py View on Github external
def it_releases_lock_on_exit(self, tmpfile):
        with flock(tmpfile):
            print('oh, hi!')

        with flock(tmpfile):
            print('oh, hi!')
github Yelp / pgctl / tests / unit / flock.py View on Github external
def it_disallows_subsequent_callers(self, tmpfile):
        with flock(tmpfile):
            print('oh, hi!')

            assert_locked(tmpfile)
github Yelp / pgctl / tests / unit / flock.py View on Github external
def it_works_fine_with_a_directory(self, tmpfile):
        import os.path

        assert not os.path.isdir(tmpfile)
        os.mkdir(tmpfile)

        with flock(tmpfile):
            print('oh, hi!')

        assert os.path.isdir(tmpfile)
github Yelp / pgctl / tests / unit / flock.py View on Github external
def assert_locked(tmpfile):
    with ShouldRaise(flock.Locked(tmpfile)):
        with flock(tmpfile):
            raise AssertionError('this should not work')
github Yelp / pgctl / tests / unit / flock.py View on Github external
def it_creates_a_file_if_it_didnt_exist(self, tmpfile):
        from os.path import exists
        assert not exists(tmpfile)
        with flock(tmpfile):
            print('oh, hi!')
        assert exists(tmpfile)
github Yelp / pgctl / pgctl / service.py View on Github external
def flock(path):
    """attempt to show the user a better message on failure, and handle the race condition"""
    def handle_race(path):
        show_runaway_processes(path)
        if handle_race.limit > 0:
            handle_race.limit -= 1
        else:
            reraise(Impossible('lock is held, but not by any process, ten times'))
    handle_race.limit = 10

    from .flock import flock
    return flock(path, on_fail=handle_race)
github Yelp / pgctl / pgctl / cli.py View on Github external
def playground_locked(self):
        """Lock the entire playground."""
        def on_lock_held(path):
            reraise(LockHeld(
                'another pgctl command is currently managing this service: (%s)\n%s' %
                (bestrelpath(path), ps(fuser(path)))
            ))

        with contextlib2.ExitStack() as context:
            for service in self.services:
                service.ensure_exists()

                # This lock represents a pgctl cli interacting with the service.
                from .flock import flock
                lock = context.enter_context(flock(
                    service.path.join('.pgctl.lock').strpath,
                    on_fail=on_lock_held,
                ))
                from .flock import set_fd_inheritable
                set_fd_inheritable(lock, False)

            yield