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