source: ThirdParty/JobMarket/src/JobMarket/server_main.cpp@ 74d798

Candidate_v1.7.0 stable
Last change on this file since 74d798 was 74d798, checked in by Frederik Heber <frederik.heber@…>, 3 months ago

JobMarket: Scheduler shutdown in extra thread.

  • The shutdown needs to happen in an extra thread. We need to wait for busy workers to finish and at the same time schedule (and execute) operations to remove idle workers. As the io_service runs in the main thread and this on a signal gets interrupted, we need these shutdown functions to run independently such that the io_service can continue handling the networking connections.
  • added flag to stop accepting new workers when shutting down
  • added command-line option such that the pool guard may be active from start already (and not only when the first worker becomes busy).
  • Property mode set to 100644
File size: 3.4 KB
Line 
1/*
2 * Project: JobMarket
3 * Description: asynchronous Server/Controller/Client-approach to parallel computing, based on boost::asio
4 * Copyright (C) 2011-2012 Frederik Heber. All rights reserved.
5 *
6 */
7
8/*
9 * \file server_main.cpp
10 *
11 * This file strongly follows the Serialization example from the boost::asio
12 * library (see server.cpp)
13 *
14 * Created on: Oct 21, 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// program_options must have some strange static stuff, causes double free or
26// corruption if included after MemDebug
27#include <boost/program_options.hpp>
28
29//#include "CodePatterns/MemDebug.hpp"
30
31#include <cstdlib>
32#include <iostream>
33#include <vector>
34
35#include "CodePatterns/Info.hpp"
36#include "CodePatterns/Log.hpp"
37#include "JobMarket/atexit.hpp"
38#include "JobMarket/FragmentScheduler.hpp"
39#include "JobMarket/Jobs/SystemCommandJob.hpp"
40#include "JobMarket/ServerOptions.hpp"
41#include "JobMarket/SignalHandler.hpp"
42
43int server_main(int argc, char* argv[])
44{
45 // from this moment on, we need to be sure to deinitialize in the correct order
46 // this is handled by the cleanup function
47 atexit(cleanUp);
48
49 ServerOptions ServerOpts;
50
51 // Declare the supported options.
52 boost::program_options::options_description desc("Allowed options");
53 desc.add_options()
54 ("help,h", "produce help message")
55 ("verbosity,v", boost::program_options::value<size_t>(), "set verbosity level")
56 ("signal", boost::program_options::value< std::vector<size_t> >(), "set signal to catch (can be given multiple times)")
57 ("workerport", boost::program_options::value< unsigned short >(), "listen on this port for connecting workers")
58 ("controllerport", boost::program_options::value< unsigned short >(), "listen on this port for connecting controller")
59 ("timeout", boost::program_options::value< size_t >(), "timeout in seconds for alive status of workers")
60 ("guard-from-start", boost::program_options::value< bool >(), "activate pool guard from server start directly")
61 ;
62
63 boost::program_options::variables_map vm;
64 boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
65 boost::program_options::notify(vm);
66
67 int status = 0;
68 status = ServerOpts.parseHelp(vm, desc);
69 if (status) return status;
70 status = ServerOpts.parseVerbosity(vm);
71 if (status) return status;
72 status = ServerOpts.parseWorkerPort(vm);
73 if (status) return status;
74 status = ServerOpts.parseControllerPort(vm);
75 if (status) return status;
76 status = ServerOpts.parseTimeout(vm);
77 if (status) return status;
78 status = ServerOpts.parseSignals(vm);
79 if (status) return status;
80 status = ServerOpts.parseGuardFromStart(vm);
81 if (status) return status;
82
83 size_t Exitflag = 0;
84 try
85 {
86 boost::asio::io_service io_service;
87 FragmentScheduler Server(io_service, ServerOpts.workerport, ServerOpts.controllerport, ServerOpts.timeout, ServerOpts.guardFromStart);
88
89 // catch ctrl-c and shutdown worker properly
90 boost::function<void (int)> shutdownfunction = boost::bind(&FragmentScheduler::shutdown, boost::ref(Server), _1);
91 SignalHandler signalhandler(shutdownfunction, ServerOpts.signals);
92
93 {
94 Info info("io_service");
95 io_service.run();
96 }
97 Exitflag = Server.getExitflag();
98 }
99 catch (std::exception& e)
100 {
101 std::cerr << e.what() << std::endl;
102 }
103
104 return Exitflag;
105}
Note: See TracBrowser for help on using the repository browser.