aboutsummaryrefslogtreecommitdiff
path: root/zen/cmds/up.cpp
blob: 17cba37949945d2bc9b029c26bebf0ceb5019ee5 (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
// Copyright Epic Games, Inc. All Rights Reserved.

#include "up.h"

#include <zencore/filesystem.h>
#include <zencore/logging.h>
#include <zenutil/zenserverprocess.h>

#include <memory>

namespace zen {

UpCommand::UpCommand()
{
}

UpCommand::~UpCommand() = default;

int
UpCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv)
{
	ZEN_UNUSED(GlobalOptions, argc, argv);

	std::filesystem::path ExePath = zen::GetRunningExecutablePath();

	ZenServerEnvironment ServerEnvironment;
	ServerEnvironment.Initialize(ExePath.parent_path());
	ZenServerInstance Server(ServerEnvironment);
	Server.SpawnServer();

	int Timeout = 10000;

	if (!Server.WaitUntilReady(Timeout))
	{
		ZEN_ERROR("zen server launch failed (timed out)");
	}
	else
	{
		ZEN_INFO("zen server up");
	}

	return 0;
}

//////////////////////////////////////////////////////////////////////////

DownCommand::DownCommand()
{
}

DownCommand::~DownCommand() = default;

int
DownCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv)
{
	ZEN_UNUSED(GlobalOptions, argc, argv);

	int ListenPort = 1337;

	// Discover executing instances

	ZenServerState Instance;
	Instance.Initialize();
	ZenServerState::ZenServerEntry* Entry = Instance.Lookup(ListenPort);

	if (!Entry)
	{
		ZEN_WARN("no zen server to bring down");

		return 0;
	}

	try
	{
		std::filesystem::path ExePath = zen::GetRunningExecutablePath();

		ZenServerEnvironment ServerEnvironment;
		ServerEnvironment.Initialize(ExePath.parent_path());
		ZenServerInstance Server(ServerEnvironment);
		Server.AttachToRunningServer(ListenPort);

		ZEN_INFO("attached to server on port {}, requesting shutdown", ListenPort);

		Server.Shutdown();

		ZEN_INFO("shutdown complete");

		return 0;
	}
	catch (std::exception& Ex)
	{
		ZEN_DEBUG("Exception caught when requesting shutdown: {}", Ex.what());
	}

	// Since we cannot obtain a handle to the process we are unable to block on the process
	// handle to determine when the server has shut down. Thus we signal that we would like
	// a shutdown via the shutdown flag and then

	Entry->SignalShutdownRequest();

	return 0;
}

}  // namespace zen