-
Notifications
You must be signed in to change notification settings - Fork 801
Expand file tree
/
Copy pathwait_ready.cpp
More file actions
132 lines (108 loc) · 3.76 KB
/
Copy pathwait_ready.cpp
File metadata and controls
132 lines (108 loc) · 3.76 KB
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
/*
* Copyright (C) Canonical, Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "wait_ready.h"
#include "animated_spinner.h"
#include "common_cli.h"
#include <multipass/cli/argparser.h>
#include <multipass/exceptions/cmd_exceptions.h>
#include <multipass/timer.h>
#include <chrono>
namespace mp = multipass;
namespace cmd = multipass::cmd;
mp::ReturnCodeVariant cmd::WaitReady::run(mp::ArgParser* parser)
{
auto ret = parse_args(parser);
if (ret != ParseCode::Ok)
{
return parser->returnCodeFrom(ret);
}
mp::AnimatedSpinner spinner{cout};
spinner.start("Waiting for the Multipass daemon to be ready");
std::unique_ptr<mp::utils::Timer> timer;
// If the user has specified a timeout, we will create a timer
if (parser->isSet("timeout"))
{
timer = cmd::make_timer(parser->value("timeout").toInt(),
&spinner,
cerr,
"Timed out waiting for the Multipass daemon to be ready.");
timer->start();
}
auto on_success = [&spinner, &timer](WaitReadyReply&) -> ReturnCodeVariant {
if (timer)
timer->stop();
spinner.stop();
return ReturnCode::Ok;
};
auto on_failure = [this, &spinner, &timer](grpc::Status& status) -> ReturnCodeVariant {
if (status.error_code() == grpc::StatusCode::NOT_FOUND)
{
// This is the expected state for when the daemon is not yet ready
// Sleep for a short duration and signal to retry
MP_UTILS.sleep_for(std::chrono::milliseconds(500));
return ReturnCode::Retry;
}
if (timer)
timer->stop();
spinner.stop();
// For any other error, we will handle it as a standard failure
return standard_failure_handler_for(name(), cerr, status);
};
request.set_verbosity_level(parser->verbosityLevel());
ReturnCodeVariant return_code;
while ((return_code = dispatch(&RpcMethod::wait_ready, request, on_success, on_failure)) ==
ReturnCode::Retry)
;
return return_code;
}
std::string cmd::WaitReady::name() const
{
return "wait-ready";
}
QString cmd::WaitReady::short_help() const
{
return QStringLiteral("Wait for the Multipass daemon to be ready");
}
QString cmd::WaitReady::description() const
{
return QStringLiteral(
"Wait for the Multipass daemon to be ready. This command will block until the\n"
"daemon has initialized, fetched up-to-date image information, and is ready to\n"
"accept requests. Its main use is to prevent failures caused by incomplete\n"
"initialization in batch operations. An optional timeout aborts the command if\n"
"reached.");
}
mp::ParseCode cmd::WaitReady::parse_args(mp::ArgParser* parser)
{
mp::cmd::add_timeout(parser);
auto status = parser->commandParse(this);
// Check if the command was parsed successfully
if (status != ParseCode::Ok)
{
return status;
}
try
{
mp::cmd::parse_timeout(parser);
}
catch (const mp::ValidationException& e)
{
cerr << "error: " << e.what() << "\n";
return ParseCode::CommandLineError;
}
return status;
}