Skip to content

Commit f97c1b8

Browse files
Merge pull request #58 from salesforce/catch-connection-errors-early
2 parents 3e1e41e + c948746 commit f97c1b8

File tree

5 files changed

+70
-4
lines changed

5 files changed

+70
-4
lines changed

p4-fusion/commands/test_result.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright (c) 2022 Salesforce, Inc.
3+
* All rights reserved.
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
* For full license text, see the LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
6+
*/
7+
#include "test_result.h"
8+
9+
void TestResult::OutputStat(StrDict* varList)
10+
{
11+
}
12+
13+
void TestResult::OutputText(const char* data, int length)
14+
{
15+
}
16+
17+
void TestResult::OutputBinary(const char* data, int length)
18+
{
19+
}

p4-fusion/commands/test_result.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright (c) 2022 Salesforce, Inc.
3+
* All rights reserved.
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
* For full license text, see the LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
6+
*/
7+
#pragma once
8+
9+
#include "common.h"
10+
#include "result.h"
11+
12+
class TestResult : public Result
13+
{
14+
public:
15+
void OutputStat(StrDict* varList) override;
16+
void OutputText(const char* data, int length) override;
17+
void OutputBinary(const char* data, int length) override;
18+
};

p4-fusion/main.cc

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,18 @@ int Main(int argc, char** argv)
8282

8383
P4API::P4PORT = Arguments::GetSingleton()->GetPort();
8484
P4API::P4USER = Arguments::GetSingleton()->GetUsername();
85+
86+
const Error& serviceConnectionResult = P4API().TestConnection(5).GetError();
87+
bool serverAvailable = serviceConnectionResult.IsError() == 0;
88+
if (serverAvailable)
89+
{
90+
SUCCESS("Perforce server is available");
91+
}
92+
else
93+
{
94+
ERR("Error occurred while connecting to " << P4API::P4PORT);
95+
return 1;
96+
}
8597
P4API::P4CLIENT = Arguments::GetSingleton()->GetClient();
8698
P4API::ClientSpec = P4API().Client().GetClientSpec();
8799

@@ -358,15 +370,17 @@ void SignalHandler(sig_atomic_t s)
358370

359371
int main(int argc, char** argv)
360372
{
373+
int exitCode = 0;
374+
361375
try
362376
{
363-
Main(argc, argv);
377+
exitCode = Main(argc, argv);
364378
}
365379
catch (const std::exception& e)
366380
{
367381
ERR("Exception occurred: " << typeid(e).name() << ": " << e.what());
368382
return 1;
369383
}
370384

371-
return 0;
385+
return exitCode;
372386
}

p4-fusion/p4_api.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,11 @@ ClientResult P4API::Client()
227227
return Run<ClientResult>("client", { "-o" });
228228
}
229229

230+
TestResult P4API::TestConnection(const int retries)
231+
{
232+
return RunEx<TestResult>("changes", { "-m", "1", "//..." }, retries);
233+
}
234+
230235
ChangesResult P4API::ShortChanges(const std::string& path)
231236
{
232237
return Run<ChangesResult>("changes", {

p4-fusion/p4_api.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "commands/users_result.h"
2222
#include "commands/info_result.h"
2323
#include "commands/client_result.h"
24+
#include "commands/test_result.h"
2425

2526
class P4API
2627
{
@@ -35,6 +36,8 @@ class P4API
3536

3637
template <class T>
3738
T Run(const char* command, const std::vector<std::string>& stringArguments);
39+
template <class T>
40+
T RunEx(const char* command, const std::vector<std::string>& stringArguments, const int commandRetries);
3841

3942
public:
4043
static std::string P4PORT;
@@ -63,6 +66,7 @@ class P4API
6366

6467
void AddClientSpecView(const std::vector<std::string>& viewStrings);
6568

69+
TestResult TestConnection(const int retries);
6670
ChangesResult ShortChanges(const std::string& path);
6771
ChangesResult Changes(const std::string& path);
6872
ChangesResult Changes(const std::string& path, const std::string& from, int32_t maxCount);
@@ -84,7 +88,7 @@ class P4API
8488
};
8589

8690
template <class T>
87-
inline T P4API::Run(const char* command, const std::vector<std::string>& stringArguments)
91+
inline T P4API::RunEx(const char* command, const std::vector<std::string>& stringArguments, const int commandRetries)
8892
{
8993
std::string argsString;
9094
for (const std::string& stringArg : stringArguments)
@@ -103,7 +107,7 @@ inline T P4API::Run(const char* command, const std::vector<std::string>& stringA
103107
m_ClientAPI.SetArgv(argsCharArray.size(), argsCharArray.data());
104108
m_ClientAPI.Run(command, &clientUser);
105109

106-
int retries = CommandRetries;
110+
int retries = commandRetries;
107111
while (m_ClientAPI.Dropped() || clientUser.GetError().IsError())
108112
{
109113
if (retries == 0)
@@ -167,3 +171,9 @@ inline T P4API::Run(const char* command, const std::vector<std::string>& stringA
167171

168172
return clientUser;
169173
}
174+
175+
template <class T>
176+
inline T P4API::Run(const char* command, const std::vector<std::string>& stringArguments)
177+
{
178+
return RunEx<T>(command, stringArguments, CommandRetries);
179+
}

0 commit comments

Comments
 (0)