aboutsummaryrefslogtreecommitdiff
path: root/src/zencompute/functionrunner.cpp
blob: 8e7c12b2b93bd9235d0905ccc0cf8fae05303093 (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
// Copyright Epic Games, Inc. All Rights Reserved.

#include "functionrunner.h"

#if ZEN_WITH_COMPUTE_SERVICES

#	include <zencore/compactbinary.h>
#	include <zencore/filesystem.h>

#	include <fmt/format.h>
#	include <vector>

namespace zen::compute {

FunctionRunner::FunctionRunner(std::filesystem::path BasePath) : m_ActionsPath(BasePath / "actions")
{
}

FunctionRunner::~FunctionRunner() = default;

size_t
FunctionRunner::QueryCapacity()
{
	return 1;
}

std::vector<SubmitResult>
FunctionRunner::SubmitActions(const std::vector<Ref<RunnerAction>>& Actions)
{
	std::vector<SubmitResult> Results;
	Results.reserve(Actions.size());

	for (const Ref<RunnerAction>& Action : Actions)
	{
		Results.push_back(SubmitAction(Action));
	}

	return Results;
}

void
FunctionRunner::MaybeDumpAction(int ActionLsn, const CbObject& ActionObject)
{
	if (m_DumpActions)
	{
		std::string			  UniqueId = fmt::format("{}.ddb", ActionLsn);
		std::filesystem::path Path	   = m_ActionsPath / UniqueId;

		zen::WriteFile(Path, IoBuffer(ActionObject.GetBuffer().AsIoBuffer()));
	}
}

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

RunnerAction::RunnerAction(FunctionServiceSession* OwnerSession) : m_OwnerSession(OwnerSession)
{
	this->Timestamps[static_cast<int>(State::New)] = DateTime::Now().GetTicks();
}

RunnerAction::~RunnerAction()
{
}

void
RunnerAction::SetActionState(State NewState)
{
	ZEN_ASSERT(NewState < State::_Count);
	this->Timestamps[static_cast<int>(NewState)] = DateTime::Now().GetTicks();

	do
	{
		if (State CurrentState = m_ActionState.load(); CurrentState == NewState)
		{
			// No state change
			return;
		}
		else
		{
			if (NewState <= CurrentState)
			{
				// Cannot transition to an earlier or same state
				return;
			}

			if (m_ActionState.compare_exchange_strong(CurrentState, NewState))
			{
				// Successful state change

				m_OwnerSession->PostUpdate(this);

				return;
			}
		}
	} while (true);
}

void
RunnerAction::SetResult(CbPackage&& Result)
{
	m_Result = std::move(Result);
}

CbPackage&
RunnerAction::GetResult()
{
	ZEN_ASSERT(IsCompleted());
	return m_Result;
}

}  // namespace zen::compute

#endif	// ZEN_WITH_COMPUTE_SERVICES