source: src/Fragmentation/Automation/FragmentScheduler.cpp@ 267b8d

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 267b8d was 267b8d, checked in by Frederik Heber <heber@…>, 12 years ago

Server now also intercepts sigint and shuts down gracefully.

  • new FragmentScheduler::shutdown() function is bound and given as signal handler.
  • on signal we first remove all workers and only shutdown and close sockets when there are no workers.
  • note that we do not release the signalhandler after first received signal but after a given number as two ctrl-c are required to shut down server having poolworkers.
  • Property mode set to 100644
File size: 21.2 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2011 University of Bonn. All rights reserved.
5 * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
6 */
7
8/*
9 * \file FragmentScheduler.cpp
10 *
11 * This file strongly follows the Serialization example from the boost::asio
12 * library (see server.cpp)
13 *
14 * Created on: Oct 19, 2011
15 * Author: heber
16 */
17
18// include config.h
19#ifdef HAVE_CONFIG_H
20#include <config.h>
21#endif
22
23// boost asio needs specific operator new
24#include <boost/asio.hpp>
25
26#include "CodePatterns/MemDebug.hpp"
27
28#include <algorithm>
29#include <boost/bind.hpp>
30#include <boost/lambda/lambda.hpp>
31#include <boost/lexical_cast.hpp>
32#include <iostream>
33#include <vector>
34#include "Connection.hpp" // Must come before boost/serialization headers.
35#include <boost/serialization/vector.hpp>
36#include "CodePatterns/Info.hpp"
37#include "CodePatterns/Log.hpp"
38#include "CodePatterns/Observer/Notification.hpp"
39#include "ControllerChoices.hpp"
40#include "Operations/Servers/SendJobToWorkerOperation.hpp"
41#include "Operations/Workers/EnrollInPoolOperation.hpp"
42#include "Jobs/MPQCCommandJob.hpp"
43#include "Jobs/SystemCommandJob.hpp"
44#include "JobId.hpp"
45
46#include "FragmentScheduler.hpp"
47
48/** Helper function to enforce binding of FragmentWorker to possible derived
49 * FragmentJob classes.
50 */
51void dummyInit() {
52 SystemCommandJob("/bin/false", "something", JobId::IllegalJob);
53 MPQCCommandJob("nofile", JobId::IllegalJob);
54}
55
56/** Constructor of class FragmentScheduler.
57 *
58 * We setup both acceptors to accept connections from workers and Controller.
59 *
60 * \param io_service io_service of the asynchronous communications
61 * \param workerport port to listen for worker connections
62 * \param controllerport port to listen for controller connections.
63 */
64FragmentScheduler::FragmentScheduler(boost::asio::io_service& _io_service, unsigned short workerport, unsigned short controllerport) :
65 Observer("FragmentScheduler"),
66 io_service(_io_service),
67 WorkerListener(_io_service, workerport, JobsQueue, pool,
68 boost::bind(&FragmentScheduler::sendJobToWorker, boost::ref(*this), _1, _2)),
69 ControllerListener(_io_service, controllerport, JobsQueue,
70 boost::bind(&FragmentScheduler::removeAllWorkers, boost::ref(*this)),
71 boost::bind(&FragmentScheduler::shutdown, boost::ref(*this))),
72 connection(_io_service)
73{
74 Info info(__FUNCTION__);
75
76 // sign on to idle workers and present jobs
77 pool.signOn(this, WorkerPool::WorkerIdle);
78 JobsQueue.signOn(this, FragmentQueue::JobAdded);
79
80 // listen for controller
81 ControllerListener.initiateSocket();
82
83 // listen for workers
84 WorkerListener.initiateSocket();
85}
86
87FragmentScheduler::~FragmentScheduler()
88{
89 // sign off
90 pool.signOff(this, WorkerPool::WorkerIdle);
91 JobsQueue.signOff(this, FragmentQueue::JobAdded);
92}
93
94/** Handle a new worker connection.
95 *
96 * We store the given address in the pool.
97 *
98 * \param e error code if something went wrong
99 * \param conn reference with the connection
100 */
101void FragmentScheduler::WorkerListener_t::handle_Accept(const boost::system::error_code& e, connection_ptr conn)
102{
103 Info info(__FUNCTION__);
104 if (!e)
105 {
106 // Successfully accepted a new connection.
107 // read address
108 conn->async_read(address,
109 boost::bind(&FragmentScheduler::WorkerListener_t::handle_ReadAddress, this,
110 boost::asio::placeholders::error, conn));
111 }
112 else
113 {
114 // An error occurred. Log it and return. Since we are not starting a new
115 // accept operation the io_service will run out of work to do and the
116 // server will exit.
117 Exitflag = ErrorFlag;
118 ELOG(0, e.message());
119 }
120}
121
122/** Handle having received Worker's address
123 *
124 * \param e error code if something went wrong
125 * \param conn reference with the connection
126 */
127void FragmentScheduler::WorkerListener_t::handle_ReadAddress(const boost::system::error_code& e, connection_ptr conn)
128{
129 Info info(__FUNCTION__);
130 if (!e)
131 {
132 // Successfully accepted a new connection.
133 // read address
134 conn->async_read(choice,
135 boost::bind(&FragmentScheduler::WorkerListener_t::handle_ReadChoice, this,
136 boost::asio::placeholders::error, conn));
137 }
138 else
139 {
140 // An error occurred. Log it and return. Since we are not starting a new
141 // accept operation the io_service will run out of work to do and the
142 // server will exit.
143 Exitflag = ErrorFlag;
144 ELOG(0, e.message());
145 }
146}
147
148/** Controller callback function to read the choice for next operation.
149 *
150 * \param e error code if something went wrong
151 * \param conn reference with the connection
152 */
153void FragmentScheduler::WorkerListener_t::handle_ReadChoice(const boost::system::error_code& e, connection_ptr conn)
154{
155 Info info(__FUNCTION__);
156 if (!e)
157 {
158 LOG(1, "INFO: Received request for operation " << choice << ".");
159 // switch over the desired choice read previously
160 switch(choice) {
161 case NoWorkerOperation:
162 {
163 ELOG(1, "WorkerListener_t::handle_ReadChoice() - called with NoOperation.");
164 break;
165 }
166 case EnrollInPool:
167 {
168 if (pool.presentInPool(address)) {
169 ELOG(1, "INFO: worker "+toString(address)+" is already contained in pool.");
170 enum EnrollInPoolOperation::EnrollFlag flag = EnrollInPoolOperation::Fail;
171 conn->async_write(flag,
172 boost::bind(&FragmentScheduler::WorkerListener_t::handle_enrolled, this,
173 boost::asio::placeholders::error, conn));
174 } else {
175 // insert as its new worker
176 LOG(1, "INFO: Adding " << address << " to pool ...");
177 pool.addWorker(address);
178 enum EnrollInPoolOperation::EnrollFlag flag = EnrollInPoolOperation::Success;
179 conn->async_write(flag,
180 boost::bind(&FragmentScheduler::WorkerListener_t::handle_enrolled, this,
181 boost::asio::placeholders::error, conn));
182 break;
183 }
184 case SendResult:
185 {
186 if (pool.presentInPool(address)) {
187 // check whether its priority is busy_priority
188 if (pool.isWorkerBusy(address)) {
189 conn->async_read(result,
190 boost::bind(&FragmentScheduler::WorkerListener_t::handle_ReceiveResultFromWorker, this,
191 boost::asio::placeholders::error, conn));
192 } else {
193 ELOG(1, "Worker " << address << " trying to send result who is not marked as busy.");
194 conn->async_read(result,
195 boost::bind(&FragmentScheduler::WorkerListener_t::handle_RejectResultFromWorker, this,
196 boost::asio::placeholders::error, conn));
197 }
198 } else {
199 ELOG(1, "Worker " << address << " trying to send result who is not in pool.");
200 conn->async_read(result,
201 boost::bind(&FragmentScheduler::WorkerListener_t::handle_RejectResultFromWorker, this,
202 boost::asio::placeholders::error, conn));
203 }
204 break;
205 }
206 case RemoveFromPool:
207 {
208 if (pool.presentInPool(address)) {
209 // removing present worker
210 pool.removeWorker(address);
211 } else {
212 ELOG(1, "Shutting down Worker " << address << " not contained in pool.");
213 }
214 break;
215 }
216 default:
217 Exitflag = ErrorFlag;
218 ELOG(1, "WorkerListener_t::handle_ReadChoice() - called with no valid choice.");
219 break;
220 }
221 }
222 // restore NoOperation choice such that choice is not read twice
223 choice = NoWorkerOperation;
224
225 initiateSocket();
226 }
227 else
228 {
229 // An error occurred. Log it and return. Since we are not starting a new
230 // accept operation the io_service will run out of work to do and the
231 // server will exit.
232 Exitflag = ErrorFlag;
233 ELOG(0, e.message());
234 }
235}
236
237
238/** Callback function when new worker has enrolled.
239 *
240 * \param e error code if something went wrong
241 * \param conn reference with the connection
242 */
243void FragmentScheduler::WorkerListener_t::handle_enrolled(const boost::system::error_code& e, connection_ptr conn)
244{
245 Info info(__FUNCTION__);
246 if (!e) {
247 LOG(2, "DEBUG: Successfully enrolled.");
248 LOG(1, "INFO: There are " << pool.getNoTotalWorkers() << " workers in the queue, "
249 << pool.getNoIdleWorkers() << " of which are idle.");
250 } else {
251 // An error occurred. Log it and return. Since we are not starting a new
252 // accept operation the io_service will run out of work to do and the
253 // server will exit.
254 Exitflag = ErrorFlag;
255 ELOG(0, e.message());
256 }
257}
258
259/** Callback function when result has been received.
260 *
261 * \param e error code if something went wrong
262 * \param conn reference with the connection
263 */
264void FragmentScheduler::WorkerListener_t::handle_ReceiveResultFromWorker(const boost::system::error_code& e, connection_ptr conn)
265{
266 Info info(__FUNCTION__);
267 LOG(1, "INFO: Received result for job #" << result->getId() << " ...");
268
269 // and push into queue
270 ASSERT(result->getId() != (JobId_t)JobId::NoJob,
271 "WorkerListener_t::handle_ReceiveResultFromWorker() - result received has NoJob id.");
272 ASSERT(result->getId() != (JobId_t)JobId::IllegalJob,
273 "WorkerListener_t::handle_ReceiveResultFromWorker() - result received has IllegalJob id.");
274 // place id into expected
275 if ((result->getId() != (JobId_t)JobId::NoJob) && (result->getId() != (JobId_t)JobId::IllegalJob))
276 JobsQueue.pushResult(result);
277
278 // mark as idle
279 pool.unmarkWorkerBusy(address);
280 LOG(1, "INFO: There are " << pool.getNoTotalWorkers() << " workers in the queue, "
281 << pool.getNoIdleWorkers() << " of which are idle.");
282
283 // erase result
284 result.reset();
285 LOG(1, "INFO: JobsQueue has " << JobsQueue.getDoneJobs() << " results.");
286}
287
288/** Callback function when result has been received.
289 *
290 * \param e error code if something went wrong
291 * \param conn reference with the connection
292 */
293void FragmentScheduler::WorkerListener_t::handle_RejectResultFromWorker(const boost::system::error_code& e, connection_ptr conn)
294{
295 Info info(__FUNCTION__);
296 // nothing to do
297 LOG(1, "INFO: Rejecting result for job #" << result->getId() << ", placing back into queue.");
298
299 JobsQueue.resubmitJob(result->getId());
300
301 LOG(1, "INFO: JobsQueue has " << JobsQueue.getDoneJobs() << " results.");
302}
303
304
305/** Handle a new controller connection.
306 *
307 * \sa handle_ReceiveJobs()
308 * \sa handle_CheckResultState()
309 * \sa handle_SendResults()
310 *
311 * \param e error code if something went wrong
312 * \param conn reference with the connection
313 */
314void FragmentScheduler::ControllerListener_t::handle_Accept(const boost::system::error_code& e, connection_ptr conn)
315{
316 Info info(__FUNCTION__);
317 if (!e)
318 {
319 conn->async_read(choice,
320 boost::bind(&FragmentScheduler::ControllerListener_t::handle_ReadChoice, this,
321 boost::asio::placeholders::error, conn));
322 }
323 else
324 {
325 // An error occurred. Log it and return. Since we are not starting a new
326 // accept operation the io_service will run out of work to do and the
327 // server will exit.
328 Exitflag = ErrorFlag;
329 ELOG(0, e.message());
330 }
331}
332
333/** Controller callback function to read the choice for next operation.
334 *
335 * \param e error code if something went wrong
336 * \param conn reference with the connection
337 */
338void FragmentScheduler::ControllerListener_t::handle_ReadChoice(const boost::system::error_code& e, connection_ptr conn)
339{
340 Info info(__FUNCTION__);
341 if (!e)
342 {
343 bool LaunchNewAcceptor = true;
344 LOG(1, "INFO: Received request for operation " << choice << ".");
345 // switch over the desired choice read previously
346 switch(choice) {
347 case NoControllerOperation:
348 {
349 ELOG(1, "ControllerListener_t::handle_ReadChoice() - called with NoOperation.");
350 break;
351 }
352 case GetNextJobId:
353 {
354 LOG(1, "INFO: Receiving number of desired job ids from controller ...");
355 conn->async_read(NumberIds,
356 boost::bind(&FragmentScheduler::ControllerListener_t::handle_GetNextJobIdState, this,
357 boost::asio::placeholders::error, conn));
358 break;
359 }
360 case SendJobs:
361 {
362 // The connection::async_write() function will automatically
363 // serialize the data structure for us.
364 LOG(1, "INFO: Receiving bunch of jobs from a controller ...");
365 conn->async_read(jobs,
366 boost::bind(&FragmentScheduler::ControllerListener_t::handle_ReceiveJobs, this,
367 boost::asio::placeholders::error, conn));
368 break;
369 }
370 case CheckState:
371 {
372 // first update number
373 jobInfo[0] = JobsQueue.getPresentJobs();
374 jobInfo[1] = JobsQueue.getDoneJobs();
375 // now we accept connections to check for state of calculations
376 LOG(1, "INFO: Sending state that "+toString(jobInfo[0])+" jobs are present and "+toString(jobInfo[1])+" jobs are done to controller ...");
377 conn->async_write(jobInfo,
378 boost::bind(&FragmentScheduler::ControllerListener_t::handle_CheckResultState, this,
379 boost::asio::placeholders::error, conn));
380 break;
381 }
382 case RemoveAll:
383 {
384 removeallWorkers();
385 break;
386 }
387 case ReceiveResults:
388 {
389 const std::vector<FragmentResult::ptr> results = JobsQueue.getAllResults();
390 // ... or we give the results
391 LOG(1, "INFO: Sending "+toString(results.size())+" results to controller ...");
392 conn->async_write(results,
393 boost::bind(&FragmentScheduler::ControllerListener_t::handle_SendResults, this,
394 boost::asio::placeholders::error, conn));
395 break;
396 }
397 case ShutdownControllerSocket:
398 {
399 LOG(1, "INFO: Received shutdown from controller ...");
400 // only allow for shutdown when there are no more jobs in the queue
401 if (!JobsQueue.isJobPresent()) {
402 // we shutdown? Hence, also shutdown controller
403 LaunchNewAcceptor = !shutdownAllSockets();
404 } else {
405 ELOG(2, "There are still jobs waiting in the queue.");
406 }
407 break;
408 }
409 default:
410 Exitflag = ErrorFlag;
411 ELOG(1, "ControllerListener_t::handle_ReadChoice() - called with no valid choice.");
412 break;
413 }
414 // restore NoControllerOperation choice such that choice is not read twice
415 choice = NoControllerOperation;
416
417 if (LaunchNewAcceptor) {
418 LOG(1, "Launching new acceptor on socket.");
419 // Start an accept operation for a new Connection.
420 initiateSocket();
421 }
422 }
423 else
424 {
425 // An error occurred. Log it and return. Since we are not starting a new
426 // accept operation the io_service will run out of work to do and the
427 // server will exit.
428 Exitflag = ErrorFlag;
429 ELOG(0, e.message());
430 }
431}
432
433/** Controller callback function when job has been sent.
434 *
435 * We check here whether the worker socket is accepting, if there
436 * have been no jobs we re-activate it, as it is shut down after
437 * last job.
438 *
439 * \param e error code if something went wrong
440 * \param conn reference with the connection
441 */
442void FragmentScheduler::ControllerListener_t::handle_ReceiveJobs(const boost::system::error_code& e, connection_ptr conn)
443{
444 Info info(__FUNCTION__);
445 // jobs are received, hence place in JobsQueue
446 if (!jobs.empty()) {
447 LOG(1, "INFO: Pushing " << jobs.size() << " jobs into queue.");
448 JobsQueue.pushJobs(jobs);
449 }
450 jobs.clear();
451}
452
453/** Controller callback function when checking on state of results.
454 *
455 * \param e error code if something went wrong
456 * \param conn reference with the connection
457 */
458void FragmentScheduler::ControllerListener_t::handle_CheckResultState(const boost::system::error_code& e, connection_ptr conn)
459{
460 Info info(__FUNCTION__);
461 // do nothing
462 LOG(1, "INFO: Sent that " << jobInfo << " jobs are (scheduled, done).");
463}
464
465/** Controller callback function when checking on state of results.
466 *
467 * \param e error code if something went wrong
468 * \param conn reference with the connection
469 */
470void FragmentScheduler::ControllerListener_t::handle_GetNextJobIdState(const boost::system::error_code& e, connection_ptr conn)
471{
472 Info info(__FUNCTION__);
473
474 std::vector<JobId_t> nextids( NumberIds, JobId::IllegalJob);
475 std::generate(nextids.begin(), nextids.end(),
476 boost::bind(&GlobalJobId::getNextId, boost::ref(globalId)));
477 LOG(1, "INFO: Sending next available job ids " << nextids << " to controller ...");
478 conn->async_write(nextids,
479 boost::bind(&FragmentScheduler::ControllerListener_t::handle_SendIds, this,
480 boost::asio::placeholders::error, conn));
481}
482
483/** Controller callback function when free job ids have been sent.
484 *
485 * \param e error code if something went wrong
486 * \param conn reference with the connection
487 */
488void FragmentScheduler::ControllerListener_t::handle_SendIds(const boost::system::error_code& e, connection_ptr conn)
489{
490 Info info(__FUNCTION__);
491 // do nothing
492 LOG(1, "INFO: Ids have been sent.");
493}
494
495/** Controller callback function when result has been received.
496 *
497 * \param e error code if something went wrong
498 * \param conn reference with the connection
499 */
500void FragmentScheduler::ControllerListener_t::handle_SendResults(const boost::system::error_code& e, connection_ptr conn)
501{
502 Info info(__FUNCTION__);
503 // do nothing
504 LOG(1, "INFO: Results have been sent.");
505}
506
507
508/** Helper function to send a job to worker.
509 *
510 * Note that we do not set the worker as busy. We simply send it the job.
511 *
512 * @param address address of worker
513 * @param job job to send
514 */
515void FragmentScheduler::sendJobToWorker(const WorkerAddress &address, FragmentJob::ptr &job)
516{
517 ASSERT( pool.isWorkerBusy(address),
518 "FragmentScheduler::sendJobToWorker() - Worker "+toString(address)+" is not marked as busy.");
519 LOG(1, "INFO: Sending job " << job->getId() << " to worker " << address << ".");
520
521 // create op, sign on, and hand over to queue
522 AsyncOperation *sendJobOp = new SendJobToWorkerOperation(connection,job);
523 OpQueue.push_back(sendJobOp, address);
524}
525
526/** Helper function to shutdown a single worker.
527 *
528 * We send NoJob to indicate shutdown
529 *
530 * @param address of worker to shutdown
531 */
532void FragmentScheduler::shutdownWorker(const WorkerAddress &address)
533{
534 ASSERT( !pool.isWorkerBusy(address),
535 "FragmentScheduler::sendJobToWorker() - Worker "+toString(address)+" is already busy.");
536 LOG(2, "INFO: Shutting down worker " << address << "...");
537 AsyncOperation *shutdownWorkerOp = new ShutdownWorkerOperation(connection);
538 OpQueue.push_back(shutdownWorkerOp, address);
539}
540
541/** Sends shutdown to all current workers in the pool.
542 *
543 */
544void FragmentScheduler::removeAllWorkers()
545{
546 // first, sign off such that no new jobs are given to workers
547 pool.signOff(this, WorkerPool::WorkerIdle);
548
549 LOG(2, "DEBUG: Waiting for busy workers to finish ...");
550 while (pool.hasBusyWorkers())
551 ;
552
553 LOG(2, "INFO: Shutting down workers ...");
554 // iterate until there are no more idle workers
555 // get list of all idle workers
556 typedef std::vector<std::pair<std::string, std::string> > WorkerList_t;
557 WorkerList_t WorkerList = pool.getListOfIdleWorkers();
558
559 // give all workers shutdown signal
560 for (WorkerList_t::const_iterator iter = WorkerList.begin(); iter != WorkerList.end(); ++iter)
561 shutdownWorker(WorkerAddress(iter->first, iter->second));
562}
563
564/** Function to shutdown server properly, e.g. for use as signal handler.
565 *
566 * @param sig signal number
567 */
568void FragmentScheduler::shutdown(int sig)
569{
570 LOG(0, "STATUS: Shutting down due to signal " << sig << ".");
571
572 if (!pool.presentIdleWorkers() && !pool.hasBusyWorkers()) {
573 shutdown();
574 } else {
575 removeAllWorkers();
576 }
577}
578
579/** Helper function to shutdown the server properly.
580 *
581 * \todo one should idle here until all workers have returned from
582 * calculating stuff (or workers need to still listen while they are
583 * calculating which is probably better).
584 *
585 * \note We only shutdown when there are no workers left
586 *
587 * @return true - doing shutdown, false - precondition not met, not shutting down
588 */
589bool FragmentScheduler::shutdown()
590{
591 if (!pool.presentIdleWorkers() && !pool.hasBusyWorkers()) {
592 LOG(1, "INFO: Shutting all down ...");
593
594 /// close the worker listener's socket
595 WorkerListener.closeSocket();
596
597 /// close the controller listener's socket
598 ControllerListener.closeSocket();
599
600 /// finally, stop the io_service
601 io_service.stop();
602 return true;
603 } else {
604 ELOG(2, "There are still idle or busy workers present.");
605 return false;
606 }
607}
608
609/** Internal helper to send the next available job to the next idle worker.
610 *
611 */
612void FragmentScheduler::sendAvailableJobToNextIdleWorker()
613{
614 const WorkerAddress address = pool.getNextIdleWorker();
615 FragmentJob::ptr job = JobsQueue.popJob();
616 sendJobToWorker(address, job);
617}
618
619void FragmentScheduler::update(Observable *publisher)
620{
621 ASSERT(0, "FragmentScheduler::update() - we are not signed on for global updates.");
622}
623
624void FragmentScheduler::recieveNotification(Observable *publisher, Notification_ptr notification)
625{
626 if ((publisher == &pool) && (notification->getChannelNo() == WorkerPool::WorkerIdle)) {
627 // we have an idle worker
628 LOG(1, "INFO: We are notified of an idle worker.");
629 // are jobs available?
630 if (JobsQueue.isJobPresent()) {
631 sendAvailableJobToNextIdleWorker();
632 }
633 } else if ((publisher == &JobsQueue) && (notification->getChannelNo() == FragmentQueue::JobAdded)) {
634 // we have new jobs
635 LOG(1, "INFO: We are notified of a new job.");
636 // check for idle workers
637 if (pool.presentIdleWorkers()) {
638 sendAvailableJobToNextIdleWorker();
639 }
640 } else {
641 ASSERT(0, "FragmentScheduler::recieveNotification() - we are not signed on for updates in channel "
642 +toString(notification->getChannelNo())+".");
643 }
644}
645
646void FragmentScheduler::subjectKilled(Observable *publisher)
647{}
Note: See TracBrowser for help on using the repository browser.