Exemple #1
0
 def open_checkpoint(cls, filename, force=False, state=None):
     """
   Acquire a locked checkpoint stream.
 """
     safe_mkdir(os.path.dirname(filename))
     fp = lock_file(filename, "a+")
     if fp in (None, False):
         if force:
             log.info('Found existing runner, forcing leadership forfeit.')
             state = state or CheckpointDispatcher.from_file(filename)
             if cls.kill_runner(state):
                 log.info('Successfully killed leader.')
                 # TODO(wickman)  Blocking may not be the best idea here.  Perhaps block up to
                 # a maximum timeout.  But blocking is necessary because os.kill does not immediately
                 # release the lock if we're in force mode.
                 fp = lock_file(filename, "a+", blocking=True)
         else:
             log.error('Found existing runner, cannot take control.')
     if fp in (None, False):
         raise cls.PermissionError(
             'Could not open locked checkpoint: %s, lock_file = %s' %
             (filename, fp))
     ckpt = ThriftRecordWriter(fp)
     ckpt.set_sync(True)
     return ckpt
Exemple #2
0
def daemonize(pidfile=None, stdout='/dev/null', stderr='/dev/null'):
  global _PIDFILE

  def daemon_fork():
    try:
      if os.fork() > 0:
        os._exit(0)
    except OSError as e:
      sys.stderr.write('Failed to fork: %s\n' % e)
      sys.exit(1)

  daemon_fork()
  os.setsid()
  daemon_fork()

  if pidfile:
    _PIDFILE = lock_file(pidfile, 'w+')
    if _PIDFILE:
      pid = os.getpid()
      sys.stderr.write('Writing pid %s into %s\n' % (pid, pidfile))
      _PIDFILE.write(str(pid))
      _PIDFILE.flush()
    else:
      sys.stderr.write('Could not acquire pidfile %s, another process running!\n' % pidfile)
      sys.exit(1)

    def shutdown():
      os.unlink(pidfile)
      _PIDFILE.close()
    atexit.register(shutdown)

  sys.stdin = open('/dev/null', 'r')
  sys.stdout = open(stdout, 'a+')
  sys.stderr = open(stderr, 'a+', 1)
Exemple #3
0
 def _setup_ckpt(self):
   """Set up the checkpoint: must be run on the parent."""
   self._log('initializing checkpoint file: %s' % self.ckpt_file())
   ckpt_fp = lock_file(self.ckpt_file(), "a+")
   if ckpt_fp in (None, False):
     raise self.CheckpointError('Could not acquire checkpoint permission or lock for %s!' %
       self.ckpt_file())
   self._ckpt_head = os.path.getsize(self.ckpt_file())
   ckpt_fp.seek(self._ckpt_head)
   self._ckpt = ThriftRecordWriter(ckpt_fp)
   self._ckpt.set_sync(True)
Exemple #4
0
 def _setup_ckpt(self):
   """Set up the checkpoint: must be run on the parent."""
   self._log('initializing checkpoint file: %s' % self.ckpt_file())
   ckpt_fp = lock_file(self.ckpt_file(), "a+")
   if ckpt_fp in (None, False):
     raise self.CheckpointError('Could not acquire checkpoint permission or lock for %s!' %
       self.ckpt_file())
   self._ckpt_head = os.path.getsize(self.ckpt_file())
   ckpt_fp.seek(self._ckpt_head)
   self._ckpt = ThriftRecordWriter(ckpt_fp)
   self._ckpt.set_sync(True)
Exemple #5
0
 def open_checkpoint(cls, filename, force=False, state=None):
     """
   Acquire a locked checkpoint stream.
 """
     safe_mkdir(os.path.dirname(filename))
     fp = lock_file(filename, "a+")
     if fp in (None, False):
         if force:
             log.info("Found existing runner, forcing leadership forfeit.")
             state = state or CheckpointDispatcher.from_file(filename)
             if cls.kill_runner(state):
                 log.info("Successfully killed leader.")
                 # TODO(wickman)  Blocking may not be the best idea here.  Perhaps block up to
                 # a maximum timeout.  But blocking is necessary because os.kill does not immediately
                 # release the lock if we're in force mode.
                 fp = lock_file(filename, "a+", blocking=True)
         else:
             log.error("Found existing runner, cannot take control.")
     if fp in (None, False):
         raise cls.PermissionError("Could not open locked checkpoint: %s, lock_file = %s" % (filename, fp))
     ckpt = ThriftRecordWriter(fp)
     ckpt.set_sync(True)
     return ckpt
Exemple #6
0
def _daemonize(pidfile, stdout, stderr, quiet, exit_parent):
    global _PIDFILE

    def daemon_fork(exit_parent=True):
        try:
            if os.fork() > 0:
                if exit_parent:
                    os._exit(0)
                return True
            return False
        except OSError as e:
            sys.stderr.write('Failed to fork: %s\n' % e)
            sys.exit(1)

    parent = daemon_fork(exit_parent)
    if parent:
        return True
    os.setsid()
    daemon_fork()

    if pidfile:
        _PIDFILE = lock_file(pidfile, 'w+')
        if _PIDFILE:
            pid = os.getpid()
            if not quiet:
                sys.stderr.write('Writing pid %s into %s\n' % (pid, pidfile))
            _PIDFILE.write(str(pid))
            _PIDFILE.flush()
        else:
            if not quiet:
                sys.stderr.write(
                    'Could not acquire pidfile %s, another process running!\n'
                    % pidfile)
            sys.exit(1)

        def shutdown():
            os.unlink(pidfile)
            _PIDFILE.close()

        atexit.register(shutdown)

    sys.stdin = open('/dev/null', 'r')
    sys.stdout = open(stdout, 'a+')
    sys.stderr = open(stderr, 'a+', 1)