MySQL 8.0.40
Source Code Documentation
process_state_component.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2022, 2024, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef MYSQL_HARNESS_PROCESS_STATE_COMPONENT_INCLUDED
27#define MYSQL_HARNESS_PROCESS_STATE_COMPONENT_INCLUDED
28
29#include <functional>
30#include <string>
31#include <vector>
32
33#include "harness_export.h"
35
36namespace mysql_harness {
37// set when the Router receives a signal to shut down or some fatal error
38// condition occurred
39class HARNESS_EXPORT ShutdownPending {
40 public:
41 /**
42 * Reason for shutdown.
43 */
44 enum class Reason { NONE, REQUESTED, FATAL_ERROR };
45
46 [[nodiscard]] std::string message() const { return message_; }
47 void message(const std::string &msg) { message_ = msg; }
48
49 [[nodiscard]] Reason reason() const { return reason_; }
50 void reason(Reason r) { reason_ = r; }
51
52 private:
54 std::string message_;
55};
56
57std::string HARNESS_EXPORT to_string(const ShutdownPending::Reason &reason);
58
59/**
60 * manages the state of the process.
61 *
62 * allows to shutdown the current process in a
63 *
64 * - thread safe
65 * - waitable
66 *
67 * way.
68 *
69 * To be used
70 *
71 * - by the signal-handler to mark the process for shutdown,
72 * - and by the Loader to wait for a shutdown to happen.
73 */
74class HARNESS_EXPORT ProcessStateComponent {
75 public:
76 static ProcessStateComponent &get_instance();
77
78 using key_type = std::string;
79
80 // disable copy, as we are a single-instance
82 void operator=(ProcessStateComponent const &) = delete;
83
84 // no move either
87
88 void clear();
89
90 /**
91 * request application shutdown.
92 *
93 * @param reason reason for the shutdown
94 * @param msg human readable reason of the shutdown
95 * @throws std::system_error same as std::unique_lock::lock does
96 */
97 void request_application_shutdown(
99 const std::string &msg = {});
100
101 using on_shutdown_request_callback = std::function<void(
102 ShutdownPending::Reason reason, const std::string &msg)>;
103 /**
104 * register a callback that will get exectued whenever the component gets the
105 * shutdown request
106 *
107 * @param callback callback to execute when the shutdown is requested
108 */
111 on_shutdown_request_callbacks_.push_back(callback);
112 }
113
114 /**
115 * pending shutdown state.
116 *
117 * - synchronized
118 * - waitable
119 */
121 return shutdown_pending_;
122 }
123
124 private:
126
127 WaitableMonitor<ShutdownPending> shutdown_pending_{{}};
128
129 std::vector<on_shutdown_request_callback> on_shutdown_request_callbacks_;
130};
131
132} // namespace mysql_harness
133
134#endif
Monitor can be waited for.
Definition: monitor.h:62
manages the state of the process.
Definition: process_state_component.h:74
void register_on_shutdown_request_callback(on_shutdown_request_callback callback)
register a callback that will get exectued whenever the component gets the shutdown request
Definition: process_state_component.h:109
std::vector< on_shutdown_request_callback > on_shutdown_request_callbacks_
Definition: process_state_component.h:129
void operator=(ProcessStateComponent &&)=delete
WaitableMonitor< ShutdownPending > & shutdown_pending()
pending shutdown state.
Definition: process_state_component.h:120
ProcessStateComponent(ProcessStateComponent const &)=delete
std::function< void(ShutdownPending::Reason reason, const std::string &msg)> on_shutdown_request_callback
Definition: process_state_component.h:102
ProcessStateComponent(ProcessStateComponent &&)=delete
void operator=(ProcessStateComponent const &)=delete
std::string key_type
Definition: process_state_component.h:78
Definition: process_state_component.h:39
void message(const std::string &msg)
Definition: process_state_component.h:47
Reason
Reason for shutdown.
Definition: process_state_component.h:44
std::string message_
Definition: process_state_component.h:54
std::string message() const
Definition: process_state_component.h:46
void reason(Reason r)
Definition: process_state_component.h:50
Reason reason() const
Definition: process_state_component.h:49
Definition: common.h:42
std::string HARNESS_EXPORT to_string(const ShutdownPending::Reason &reason)
Definition: process_state_component.cc:60
const mysql_service_registry_t * r
Definition: pfs_example_plugin_employee.cc:86