aboutsummaryrefslogtreecommitdiff
path: root/src/zen/cmds/exec_cmd.h
blob: a0bf201a1bf36e9221f78a713a6ee87bf4f388bf (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright Epic Games, Inc. All Rights Reserved.

#pragma once

#include "../zen.h"

#include <zencompute/recordingreader.h>
#include <zencore/compactbinarypackage.h>
#include <zencore/guid.h>
#include <zencore/iohash.h>

#include <filesystem>
#include <functional>
#include <memory>
#include <unordered_map>

namespace zen {
class CbPackage;
class CbObject;
struct IoHash;
class ChunkResolver;
}  // namespace zen

#if ZEN_WITH_COMPUTE_SERVICES

namespace zen::compute {
class ComputeServiceSession;
}

namespace zen {

class ExecCommand;

struct ExecFunctionDefinition
{
	std::string FunctionName;
	zen::Guid	FunctionVersion;
	zen::Guid	BuildSystemVersion;
	zen::IoHash WorkerId;
};

//////////////////////////////////////////////////////////////////////////
// Subcommands

class ExecHttpSubCmd : public ZenSubCmdBase
{
public:
	explicit ExecHttpSubCmd(ExecCommand& Parent);
	void Run(const ZenCliOptions& GlobalOptions) override;

private:
	ExecCommand& m_Parent;
	std::string	 m_HostName;
};

class ExecInprocSubCmd : public ZenSubCmdBase
{
public:
	explicit ExecInprocSubCmd(ExecCommand& Parent);
	void Run(const ZenCliOptions& GlobalOptions) override;

private:
	ExecCommand& m_Parent;
	bool		 m_Managed = false;
};

class ExecBeaconSubCmd : public ZenSubCmdBase
{
public:
	explicit ExecBeaconSubCmd(ExecCommand& Parent);
	void Run(const ZenCliOptions& GlobalOptions) override;

private:
	ExecCommand&		  m_Parent;
	std::string			  m_OrchestratorUrl;
	std::filesystem::path m_BeaconPath;
};

class ExecDumpSubCmd : public ZenSubCmdBase
{
public:
	explicit ExecDumpSubCmd(ExecCommand& Parent);
	void Run(const ZenCliOptions& GlobalOptions) override;

private:
	ExecCommand& m_Parent;
};

class ExecBuildlogSubCmd : public ZenSubCmdBase
{
public:
	explicit ExecBuildlogSubCmd(ExecCommand& Parent);
	void Run(const ZenCliOptions& GlobalOptions) override;

private:
	ExecCommand& m_Parent;
};

/**
 * Zen CLI command for executing functions from a recording
 *
 * Mostly for testing and debugging purposes
 */

class ExecCommand : public ZenCmdWithSubCommands
{
public:
	ExecCommand();
	~ExecCommand();

	static constexpr char Name[]		= "exec";
	static constexpr char Description[] = "Execute functions from a recording";

	cxxopts::Options& Options() override { return m_Options; }

	// Shared state & helpers (public for subcommand access)

	using FunctionDefinition = ExecFunctionDefinition;

	static void EmitFunctionList(const std::vector<FunctionDefinition>& FunctionList);
	void		RegisterWorkerFunctionsFromDescription(const zen::CbObject& WorkerDesc, const zen::IoHash& WorkerId);

	int RunSession(zen::compute::ComputeServiceSession& ComputeSession, std::string_view OrchestratorUrl = {});

	std::unordered_map<zen::IoHash, zen::CbPackage> m_WorkerMap;
	std::vector<FunctionDefinition>					m_FunctionList;
	zen::ChunkResolver*								m_ChunkResolver	  = nullptr;
	zen::compute::RecordingReaderBase*				m_RecordingReader = nullptr;
	bool											m_VerboseLogging  = false;
	bool											m_QuietLogging	  = false;
	bool											m_DumpActions	  = false;
	std::filesystem::path							m_OutputPath;
	bool											m_Binary = false;
	std::filesystem::path							m_RecordingLogPath;

private:
	bool OnParentOptionsParsed(const ZenCliOptions& GlobalOptions) override;

	cxxopts::Options	  m_Options{Name, Description};
	std::string			  m_SubCommand;
	std::filesystem::path m_RecordingPath;
	int					  m_Offset = 0;
	int					  m_Stride = 1;
	int					  m_Limit  = 0;
	bool				  m_Quiet  = false;

	std::unique_ptr<zen::compute::RecordingReader>	 m_Reader;
	std::unique_ptr<zen::compute::UeRecordingReader> m_UeReader;

	ExecHttpSubCmd	   m_HttpSubCmd{*this};
	ExecInprocSubCmd   m_InprocSubCmd{*this};
	ExecBeaconSubCmd   m_BeaconSubCmd{*this};
	ExecDumpSubCmd	   m_DumpSubCmd{*this};
	ExecBuildlogSubCmd m_BuildlogSubCmd{*this};
};

}  // namespace zen

#endif	// ZEN_WITH_COMPUTE_SERVICES