@@ -95,46 +95,6 @@ std::shared_ptr<wpublish::ChunkInventory> makeChunkInventory(string const& worke
9595 return inventory;
9696}
9797
98- /* *
99- * This function will keep periodically updating worker's info in the Replication
100- * System's Registry.
101- * @param id The unique identifier of a worker to be registered.
102- * @note The thread will terminate the process if the registraton request to the Registry
103- * was explicitly denied by the service. This means the application may be misconfigured.
104- * Transient communication errors when attempting to connect or send requests to
105- * the Registry will be posted into the log stream and ignored.
106- */
107- void registryUpdateLoop (string const & id) {
108- auto const workerConfig = wconfig::WorkerConfig::instance ();
109- auto const method = http::Method::POST;
110- string const url = " http://" + workerConfig->replicationRegistryHost () + " :" +
111- to_string (workerConfig->replicationRegistryPort ()) + " /qserv-worker" ;
112- vector<string> const headers = {" Content-Type: application/json" };
113- json const request = json::object ({{" version" , http::MetaModule::version},
114- {" instance_id" , workerConfig->replicationInstanceId ()},
115- {" auth_key" , workerConfig->replicationAuthKey ()},
116- {" worker" ,
117- {{" name" , id},
118- {" management-port" , workerConfig->replicationHttpPort ()},
119- {" management-host-name" , util::get_current_host_fqdn ()}}}});
120- string const requestContext =
121- " WorkerMain: '" + http::method2string (method) + " ' request to '" + url + " '" ;
122- http::Client client (method, url, request.dump (), headers);
123- while (true ) {
124- try {
125- json const response = client.readAsJson ();
126- if (0 == response.at (" success" ).get <int >()) {
127- string const error = response.at (" error" ).get <string>();
128- LOGS (_log, LOG_LVL_ERROR, requestContext + " was denied, error: '" + error + " '." );
129- abort ();
130- }
131- } catch (exception const & ex) {
132- LOGS (_log, LOG_LVL_WARN, requestContext + " failed, ex: " + ex.what ());
133- }
134- this_thread::sleep_for (chrono::seconds (max (1U , workerConfig->replicationRegistryHearbeatIvalSec ())));
135- }
136- }
137-
13898} // namespace
13999
140100namespace lsst ::qserv::wmain {
@@ -265,24 +225,70 @@ WorkerMain::WorkerMain() {
265225
266226 // Begin periodically updating worker's status in the Replication System's registry
267227 // in the detached thread. This will continue before the application gets terminated.
268- thread registryUpdateThread (::registryUpdateLoop, _foreman-> chunkInventory ()-> id () );
269- registryUpdateThread. detach ( );
228+ thread registryUpdateThread (&WorkerMain::_registryUpdateLoop, this );
229+ _registryUpdateThread = move (registryUpdateThread );
270230}
271231
272232void WorkerMain::waitForTerminate () {
273233 unique_lock uniq (_terminateMtx);
274- _terminateCv.wait (uniq, [this ]() { return _terminate; });
234+ _terminateCv.wait (uniq, [this ]() -> bool { return _terminate; });
275235}
276236
277237void WorkerMain::terminate () {
278- lock_guard lck (_terminateMtx);
279- _terminate = true ;
238+ {
239+ lock_guard lck (_terminateMtx);
240+ if (_terminate.exchange (true )) return ;
241+ ;
242+ }
280243 _terminateCv.notify_all ();
244+ _controlHttpSvc->stop ();
281245}
282246
283247WorkerMain::~WorkerMain () {
284248 LOGS (_log, LOG_LVL_INFO, " WorkerMain shutdown." );
285- _controlHttpSvc->stop ();
249+ terminate ();
250+ _registryUpdateThread.join ();
251+ }
252+
253+ /* *
254+ * This function will keep periodically updating worker's info in the Replication
255+ * System's Registry.
256+ * @param id The unique identifier of a worker to be registered.
257+ * @note The thread will terminate the process if the registraton request to the Registry
258+ * was explicitly denied by the service. This means the application may be misconfigured.
259+ * Transient communication errors when attempting to connect or send requests to
260+ * the Registry will be posted into the log stream and ignored.
261+ */
262+ void WorkerMain::_registryUpdateLoop () {
263+ string const id = _foreman->chunkInventory ()->id ();
264+ auto const workerConfig = wconfig::WorkerConfig::instance ();
265+ auto const method = http::Method::POST;
266+ string const url = " http://" + workerConfig->replicationRegistryHost () + " :" +
267+ to_string (workerConfig->replicationRegistryPort ()) + " /qserv-worker" ;
268+ vector<string> const headers = {" Content-Type: application/json" };
269+ json const request = json::object ({{" version" , http::MetaModule::version},
270+ {" instance_id" , workerConfig->replicationInstanceId ()},
271+ {" auth_key" , workerConfig->replicationAuthKey ()},
272+ {" worker" ,
273+ {{" name" , id},
274+ {" management-port" , workerConfig->replicationHttpPort ()},
275+ {" management-host-name" , _foreman->getFqdn ()}}}});
276+ string const requestContext =
277+ " WorkerMain: '" + http::method2string (method) + " ' request to '" + url + " '" ;
278+ http::Client client (method, url, request.dump (), headers);
279+ while (!_terminate) {
280+ try {
281+ json const response = client.readAsJson ();
282+ if (0 == response.at (" success" ).get <int >()) {
283+ string const error = response.at (" error" ).get <string>();
284+ LOGS (_log, LOG_LVL_ERROR, requestContext + " was denied, error: '" + error + " '." );
285+ abort ();
286+ }
287+ } catch (exception const & ex) {
288+ LOGS (_log, LOG_LVL_WARN, requestContext + " failed, ex: " + ex.what ());
289+ }
290+ this_thread::sleep_for (chrono::seconds (max (1U , workerConfig->replicationRegistryHearbeatIvalSec ())));
291+ }
286292}
287293
288294} // namespace lsst::qserv::wmain
0 commit comments