本文整理汇总了Python中apache.thermos.common.path.TaskPath类的典型用法代码示例。如果您正苦于以下问题:Python TaskPath类的具体用法?Python TaskPath怎么用?Python TaskPath使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了TaskPath类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: write_header
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()
示例2: get
def get(cls, task_id, checkpoint_root):
"""
Get a TaskRunner bound to the task_id in checkpoint_root.
"""
path = TaskPath(root=checkpoint_root, task_id=task_id, state="active")
task_json = path.getpath("task_path")
task_checkpoint = path.getpath("runner_checkpoint")
if not os.path.exists(task_json):
return None
task = ThermosConfigLoader.load_json(task_json)
if task is None:
return None
if len(task.tasks()) == 0:
return None
try:
checkpoint = CheckpointDispatcher.from_file(task_checkpoint)
if checkpoint is None or checkpoint.header is None:
return None
return cls(
task.tasks()[0].task(),
checkpoint_root,
checkpoint.header.sandbox,
log_dir=checkpoint.header.log_dir,
task_id=task_id,
portmap=checkpoint.header.ports,
hostname=checkpoint.header.hostname,
)
except Exception as e:
log.error("Failed to reconstitute checkpoint in TaskRunner.get: %s" % e, exc_info=True)
return None
示例3: run_with_class
def run_with_class(process_class):
with temporary_dir() as td:
taskpath = TaskPath(root=td, task_id='task', process='process', run=0)
sandbox = setup_sandbox(td, taskpath)
with open(os.path.join(sandbox, 'silly_pants'), 'w') as silly_pants:
p = process_class('process', 'echo test >&%s' % silly_pants.fileno(),
0, taskpath, sandbox)
p.start()
return wait_for_rc(taskpath.getpath('process_checkpoint'))
示例4: kill
def kill(cls, task_id, checkpoint_root, force=False,
terminal_status=TaskState.KILLED, clock=time):
"""
An implementation of Task killing that doesn't require a fully hydrated TaskRunner object.
Terminal status must be either KILLED or LOST state.
"""
if terminal_status not in (TaskState.KILLED, TaskState.LOST):
raise cls.Error('terminal_status must be KILLED or LOST (got %s)' %
TaskState._VALUES_TO_NAMES.get(terminal_status) or terminal_status)
pathspec = TaskPath(root=checkpoint_root, task_id=task_id)
checkpoint = pathspec.getpath('runner_checkpoint')
state = CheckpointDispatcher.from_file(checkpoint)
if state is None or state.header is None or state.statuses is None:
if force:
log.error('Task has uninitialized TaskState - forcibly finalizing')
cls.finalize_task(pathspec)
return
else:
log.error('Cannot update states in uninitialized TaskState!')
return
ckpt = cls.open_checkpoint(checkpoint, force=force, state=state)
def write_task_state(state):
update = TaskStatus(state=state, timestamp_ms=int(clock.time() * 1000),
runner_pid=os.getpid(), runner_uid=os.getuid())
ckpt.write(RunnerCkpt(task_status=update))
def write_process_status(status):
ckpt.write(RunnerCkpt(process_status=status))
if cls.is_task_terminal(state.statuses[-1].state):
log.info('Task is already in terminal state! Finalizing.')
cls.finalize_task(pathspec)
return
with closing(ckpt):
write_task_state(TaskState.ACTIVE)
for process, history in state.processes.items():
process_status = history[-1]
if not cls.is_process_terminal(process_status.state):
if cls.kill_process(state, process):
write_process_status(ProcessStatus(process=process,
state=ProcessState.KILLED, seq=process_status.seq + 1, return_code=-9,
stop_time=clock.time()))
else:
if process_status.state is not ProcessState.WAITING:
write_process_status(ProcessStatus(process=process,
state=ProcessState.LOST, seq=process_status.seq + 1))
write_task_state(terminal_status)
cls.finalize_task(pathspec)
示例5: test_simple_process_other_user
def test_simple_process_other_user(*args):
with temporary_dir() as td:
some_user = get_other_nonroot_user()
taskpath = TaskPath(root=td, task_id='task', process='process', run=0)
sandbox = setup_sandbox(td, taskpath)
p = TestProcess('process', 'echo hello world', 0, taskpath, sandbox, user=some_user.pw_name)
p.start()
rc = wait_for_rc(taskpath.getpath('process_checkpoint'))
# since we're not actually root, the best we can do is check the right things were attempted
assert os.setgroups.calledwith([g.gr_gid for g in grp.getgrall() if some_user.pw_name in g])
assert os.setgid.calledwith(some_user.pw_uid)
assert os.setuid.calledwith(some_user.pw_gid)
示例6: test_simple_process
def test_simple_process():
with temporary_dir() as td:
taskpath = TaskPath(root=td, task_id='task', process='process', run=0)
sandbox = setup_sandbox(td, taskpath)
p = TestProcess('process', 'echo hello world', 0, taskpath, sandbox)
p.start()
rc = wait_for_rc(taskpath.getpath('process_checkpoint'))
assert rc == 0
stdout = taskpath.with_filename('stdout').getpath('process_logdir')
assert os.path.exists(stdout)
with open(stdout, 'r') as fp:
assert fp.read() == 'hello world\n'
示例7: test_empty_garbage_collector
def test_empty_garbage_collector():
with temporary_dir() as checkpoint_root:
path = TaskPath(root=checkpoint_root, task_id='does_not_exist')
gc = TaskGarbageCollector(checkpoint_root, 'does_not_exist')
assert gc.get_age() == 0
# assume runner, finished task
assert set(gc.get_metadata(with_size=False)) == set([
path.getpath('runner_checkpoint'),
path.given(state='finished').getpath('task_path'),
])
assert list(gc.get_logs()) == []
assert list(gc.get_data()) == []
示例8: test_log_permissions
def test_log_permissions():
with temporary_dir() as td:
taskpath = TaskPath(root=td, task_id='task', process='process', run=0)
sandbox = setup_sandbox(td, taskpath)
p = TestProcess('process', 'echo hello world', 0, taskpath, sandbox)
p.start()
rc = wait_for_rc(taskpath.getpath('process_checkpoint'))
stdout = taskpath.with_filename('stdout').getpath('process_logdir')
stderr = taskpath.with_filename('stderr').getpath('process_logdir')
assert os.path.exists(stdout)
assert os.path.exists(stderr)
assert os.stat(stdout).st_uid == os.getuid()
assert os.stat(stderr).st_uid == os.getuid()
示例9: __init__
def __init__(self, root, task_id):
"""Construct a TaskMonitor.
:param root: The checkpoint root of the task.
:param task_id: The task id of the task.
"""
pathspec = TaskPath(root=root, task_id=task_id)
self._dispatcher = CheckpointDispatcher()
self._runnerstate = RunnerState(processes={})
self._runner_ckpt = pathspec.getpath("runner_checkpoint")
self._active_file, self._finished_file = (
pathspec.given(state=state).getpath("task_path") for state in ("active", "finished")
)
self._ckpt_head = 0
self._apply_states()
self._lock = threading.Lock()
示例10: test_log_permissions_other_user
def test_log_permissions_other_user(*mocks):
with temporary_dir() as td:
some_user = get_other_nonroot_user()
taskpath = TaskPath(root=td, task_id='task', process='process', run=0)
sandbox = setup_sandbox(td, taskpath)
p = TestProcess('process', 'echo hello world', 0, taskpath, sandbox, user=some_user.pw_name)
p.start()
rc = wait_for_rc(taskpath.getpath('process_checkpoint'))
# since we're not actually root, the best we can do is check the right things were attempted
stdout = taskpath.with_filename('stdout').getpath('process_logdir')
stderr = taskpath.with_filename('stderr').getpath('process_logdir')
assert os.path.exists(stdout)
assert os.path.exists(stderr)
assert os.chown.calledwith(stdout, some_user.pw_uid, some_user.pw_gid)
assert os.chown.calledwith(stderr, some_user.pw_uid, some_user.pw_gid)
示例11: __init__
def __init__(self, root, resource_monitor_class=TaskResourceMonitor):
self._pathspec = TaskPath(root=root)
self._detector = TaskDetector(root)
if not issubclass(resource_monitor_class, ResourceMonitorBase):
raise ValueError("resource monitor class must implement ResourceMonitorBase!")
self._resource_monitor = resource_monitor_class
self._active_tasks = {} # task_id => ActiveObservedTask
self._finished_tasks = {} # task_id => FinishedObservedTask
self._stop_event = threading.Event()
ExceptionalThread.__init__(self)
Lockable.__init__(self)
self.daemon = True
示例12: test_garbage_collector
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)])
示例13: __init__
def __init__(self, task, success_rate=100, random_seed=31337, **extra_task_runner_args):
"""
task = Thermos task
portmap = port map
success_rate = success rate of writing checkpoint to disk
"""
self.task = task
with temporary_file(cleanup=False) as fp:
self.job_filename = fp.name
fp.write(ThermosTaskWrapper(task).to_json())
self.state_filename = tempfile.mktemp()
self.tempdir = tempfile.mkdtemp()
self.task_id = '%s-runner-base' % int(time.time() * 1000000)
self.sandbox = os.path.join(self.tempdir, 'sandbox')
self.extra_task_runner_args = extra_task_runner_args
self.cleaned = False
self.pathspec = TaskPath(root=self.tempdir, task_id=self.task_id)
self.script_filename = None
self.success_rate = success_rate
self.random_seed = random_seed
self._run_count = 0
示例14: test_task_detector
def test_task_detector():
with temporary_dir() as root:
active_log_dir = os.path.join(root, 'active_log')
finished_log_dir = os.path.join(root, 'finished_log')
path = TaskPath(root=root)
detector = TaskDetector(root)
# test empty paths
assert list(detector.get_task_ids(state='active')) == []
assert list(detector.get_task_ids(state='finished')) == []
assert set(detector.get_task_ids()) == set()
assert detector.get_checkpoint(task_id='active_task') == path.given(
task_id='active_task').getpath('runner_checkpoint')
assert detector.get_checkpoint(task_id='finished_task') == path.given(
task_id='finished_task').getpath('runner_checkpoint')
assert set(detector.get_process_checkpoints('active_task')) == set()
assert set(detector.get_process_checkpoints('finished_task')) == set()
assert set(detector.get_process_runs('active_task', active_log_dir)) == set()
assert set(detector.get_process_runs('finished_task', finished_log_dir)) == set()
assert set(detector.get_process_logs('active_task', active_log_dir)) == set()
assert set(detector.get_process_logs('finished_task', finished_log_dir)) == set()
# create paths
paths = [
path.given(state='active', task_id='active_task').getpath('task_path'),
path.given(state='finished', task_id='finished_task').getpath('task_path'),
path.given(task_id='active_task').getpath('runner_checkpoint'),
path.given(task_id='finished_task').getpath('runner_checkpoint'),
path.given(
task_id='active_task',
process='hello_world',
run='0',
log_dir=active_log_dir
).with_filename('stdout').getpath('process_logdir'),
path.given(
task_id='finished_task',
process='goodbye_world',
run='1',
log_dir=finished_log_dir
).with_filename('stderr').getpath('process_logdir'),
path.given(task_id='active_task', process='hello_world').getpath('process_checkpoint'),
path.given(task_id='finished_task', process='goodbye_world').getpath('process_checkpoint'),
]
for p in paths:
touch(p)
detector = TaskDetector(root)
assert list(detector.get_task_ids(state='active')) == list([('active', 'active_task')])
assert list(detector.get_task_ids(state='finished')) == list([('finished', 'finished_task')])
assert set(detector.get_task_ids()) == set(
[('active', 'active_task'), ('finished', 'finished_task')])
assert list(detector.get_process_checkpoints('active_task')) == [
path.given(task_id='active_task', process='hello_world').getpath('process_checkpoint')]
assert list(detector.get_process_checkpoints('finished_task')) == [
path.given(task_id='finished_task', process='goodbye_world').getpath('process_checkpoint')]
assert list(detector.get_process_runs('active_task', active_log_dir)) == [
('hello_world', 0)]
assert list(detector.get_process_runs('finished_task', finished_log_dir)) == [
('goodbye_world', 1)]
assert list(detector.get_process_logs('active_task', active_log_dir)) == [
path.given(
task_id='active_task',
process='hello_world',
run='0',
log_dir=active_log_dir
).with_filename('stdout').getpath('process_logdir')]
assert list(detector.get_process_logs('finished_task', finished_log_dir)) == [
path.given(
task_id='finished_task',
process='goodbye_world',
run='1',
log_dir=finished_log_dir
).with_filename('stderr').getpath('process_logdir')]
示例15: TaskObserver
class TaskObserver(ExceptionalThread, Lockable):
"""
The TaskObserver monitors the thermos checkpoint root for active/finished
tasks. It is used to be the oracle of the state of all thermos tasks on
a machine.
It currently returns JSON, but really should just return objects. We should
then build an object->json translator.
"""
class UnexpectedError(Exception): pass
class UnexpectedState(Exception): pass
POLLING_INTERVAL = Amount(1, Time.SECONDS)
def __init__(self, root, resource_monitor_class=TaskResourceMonitor):
self._pathspec = TaskPath(root=root)
self._detector = TaskDetector(root)
if not issubclass(resource_monitor_class, ResourceMonitorBase):
raise ValueError("resource monitor class must implement ResourceMonitorBase!")
self._resource_monitor = resource_monitor_class
self._active_tasks = {} # task_id => ActiveObservedTask
self._finished_tasks = {} # task_id => FinishedObservedTask
self._stop_event = threading.Event()
ExceptionalThread.__init__(self)
Lockable.__init__(self)
self.daemon = True
@property
def active_tasks(self):
"""Return a dictionary of active Tasks"""
return self._active_tasks
@property
def finished_tasks(self):
"""Return a dictionary of finished Tasks"""
return self._finished_tasks
@property
def all_tasks(self):
"""Return a dictionary of all Tasks known by the TaskObserver"""
return dict(self.active_tasks.items() + self.finished_tasks.items())
def stop(self):
self._stop_event.set()
def start(self):
ExceptionalThread.start(self)
@Lockable.sync
def add_active_task(self, task_id):
if task_id in self.finished_tasks:
log.error('Found an active task (%s) in finished tasks?' % task_id)
return
task_monitor = TaskMonitor(self._pathspec, task_id)
if not task_monitor.get_state().header:
log.info('Unable to load task "%s"' % task_id)
return
sandbox = task_monitor.get_state().header.sandbox
resource_monitor = self._resource_monitor(task_monitor, sandbox)
resource_monitor.start()
self._active_tasks[task_id] = ActiveObservedTask(
task_id=task_id, pathspec=self._pathspec,
task_monitor=task_monitor, resource_monitor=resource_monitor
)
@Lockable.sync
def add_finished_task(self, task_id):
self._finished_tasks[task_id] = FinishedObservedTask(
task_id=task_id, pathspec=self._pathspec
)
@Lockable.sync
def active_to_finished(self, task_id):
self.remove_active_task(task_id)
self.add_finished_task(task_id)
@Lockable.sync
def remove_active_task(self, task_id):
task = self.active_tasks.pop(task_id)
task.resource_monitor.kill()
@Lockable.sync
def remove_finished_task(self, task_id):
self.finished_tasks.pop(task_id)
def run(self):
"""
The internal thread for the observer. This periodically polls the
checkpoint root for new tasks, or transitions of tasks from active to
finished state.
"""
while not self._stop_event.is_set():
time.sleep(self.POLLING_INTERVAL.as_(Time.SECONDS))
active_tasks = [task_id for _, task_id in self._detector.get_task_ids(state='active')]
finished_tasks = [task_id for _, task_id in self._detector.get_task_ids(state='finished')]
with self.lock:
# Ensure all tasks currently detected on the system are observed appropriately
#.........这里部分代码省略.........