aboutsummaryrefslogtreecommitdiff
path: root/src/zencompute/runners/managedrunner.h
blob: 21a44d43cd556212da960c8afd7a0e848ff66084 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright Epic Games, Inc. All Rights Reserved.

#pragma once

#include "localrunner.h"

#if ZEN_WITH_COMPUTE_SERVICES

#	include <zenutil/process/subprocessmanager.h>

#	include <memory>

namespace asio {
class io_context;
}

namespace zen::compute {

/** Cross-platform process runner backed by SubprocessManager.

	Subclasses LocalProcessRunner, reusing sandbox management, worker manifesting,
	input/output handling, and shared action preparation. Replaces the polling-based
	monitor thread with async exit callbacks driven by SubprocessManager, and
	delegates CPU/memory metrics sampling to the manager's built-in round-robin
	sampler.

	A ProcessGroup (backed by a JobObject on Windows, process group on POSIX) is
	used for bulk cancellation on shutdown.

	This runner does not perform any platform-specific sandboxing (AppContainer,
	namespaces, Seatbelt). It is intended as a simpler, cross-platform alternative
	to the platform-specific runners for non-sandboxed workloads.
 */
class ManagedProcessRunner : public LocalProcessRunner
{
public:
	ManagedProcessRunner(ChunkResolver&				  Resolver,
						 const std::filesystem::path& BaseDir,
						 DeferredDirectoryDeleter&	  Deleter,
						 WorkerThreadPool&			  WorkerPool,
						 int32_t					  MaxConcurrentActions = 0);
	~ManagedProcessRunner();

	void					   Shutdown() override;
	[[nodiscard]] SubmitResult SubmitAction(Ref<RunnerAction> Action) override;
	void					   CancelRunningActions() override;
	bool					   CancelAction(int ActionLsn) override;
	[[nodiscard]] bool		   IsHealthy() override { return true; }

private:
	static constexpr int kIoThreadCount = 4;

	// Exit callback posted on an io_context thread.
	void OnProcessExit(int ActionLsn, int ExitCode);

	std::unique_ptr<asio::io_context>  m_IoContext;
	std::unique_ptr<SubprocessManager> m_SubprocessManager;
	ProcessGroup*					   m_ProcessGroup = nullptr;
	std::vector<std::thread>		   m_IoThreads;
};

}  // namespace zen::compute

#endif