def write_header(root, sandbox, task_id):
    log_dir = os.path.join(sandbox, '.logs')
    path = TaskPath(root=root, task_id=task_id, log_dir=log_dir)
    header = RunnerHeader(task_id=task_id, sandbox=sandbox, log_dir=log_dir)
    ckpt = TaskRunnerHelper.open_checkpoint(path.getpath('runner_checkpoint'))
    ckpt.write(RunnerCkpt(runner_header=header))
    ckpt.close()
Example #2
0
  def _initialize_ckpt_header(self):
    """
      Initializes the RunnerHeader for this checkpoint stream if it has not already
      been constructed.
    """
    if self._state.header is None:
      try:
        uid = pwd.getpwnam(self._user).pw_uid
      except KeyError:
        # This will cause failures downstream, but they will at least be correctly
        # reflected in the process state.
        log.error('Unknown user %s.' % self._user)
        uid = None

      header = RunnerHeader(
          task_id=self._task_id,
          launch_time_ms=int(self._launch_time * 1000),
          sandbox=self._sandbox,
          log_dir=self._log_dir,
          hostname=self._hostname,
          user=self._user,
          uid=uid,
          ports=self._portmap)
      runner_ckpt = RunnerCkpt(runner_header=header)
      self._dispatcher.dispatch(self._state, runner_ckpt)
Example #3
0
def test_garbage_collector(safe_rmtree, safe_delete):
    with temporary_dir() as sandbox, temporary_dir(
    ) as checkpoint_root, temporary_dir() as log_dir:

        path = TaskPath(root=checkpoint_root, task_id='test', log_dir=log_dir)

        touch(os.path.join(sandbox, 'test_file1'))
        touch(os.path.join(sandbox, 'test_file2'))
        safe_mkdir(
            os.path.dirname(path.given(state='finished').getpath('task_path')))
        safe_mkdir(os.path.dirname(path.getpath('runner_checkpoint')))
        touch(path.given(state='finished').getpath('task_path'))

        header = RunnerHeader(task_id='test', sandbox=sandbox, log_dir=log_dir)
        ckpt = TaskRunnerHelper.open_checkpoint(
            path.getpath('runner_checkpoint'))
        ckpt.write(RunnerCkpt(runner_header=header))
        ckpt.close()

        gc = TaskGarbageCollector(checkpoint_root, task_id='test')
        assert gc._state.header.log_dir == log_dir
        assert gc._state.header.sandbox == sandbox

        # erase metadata
        gc.erase_metadata()
        safe_delete.assert_has_calls([
            call(path.given(state='finished').getpath('task_path')),
            call(path.getpath('runner_checkpoint'))
        ],
                                     any_order=True)
        safe_rmtree.assert_has_calls([call(path.getpath('checkpoint_path'))])

        safe_delete.reset_mock()
        safe_rmtree.reset_mock()

        # erase logs
        gc.erase_logs()
        safe_rmtree.assert_has_calls([call(log_dir)])

        safe_delete.reset_mock()
        safe_rmtree.reset_mock()

        # erase sandbox
        gc.erase_data()

        safe_delete.assert_has_calls([
            call(os.path.join(sandbox, 'test_file1')),
            call(os.path.join(sandbox, 'test_file2'))
        ],
                                     any_order=True)
        safe_rmtree.assert_has_calls([call(sandbox)])
Example #4
0
def make_runner_state(cpid=COORDINATOR_PID,
                      pid=PID,
                      user=USER1,
                      pname=PROCESS_NAME):
    return RunnerState(header=RunnerHeader(user=user),
                       processes={
                           pname: [
                               ProcessStatus(fork_time=CREATE_TIME,
                                             start_time=CREATE_TIME,
                                             pid=pid,
                                             coordinator_pid=cpid,
                                             process=pname)
                           ]
                       })
Example #5
0
 def _initialize_ckpt_header(self):
     """
   Initializes the RunnerHeader for this checkpoint stream if it has not already
   been constructed.
 """
     if self._state.header is None:
         header = RunnerHeader(task_id=self._task_id,
                               launch_time_ms=int(self._launch_time * 1000),
                               sandbox=self._sandbox,
                               log_dir=self._log_dir,
                               hostname=socket.gethostname(),
                               user=self._user,
                               ports=self._portmap)
         runner_ckpt = RunnerCkpt(runner_header=header)
         self._dispatcher.dispatch(self._state, runner_ckpt)