HCE Project Python language Distributed Tasks Manager Application, Distributed Crawler Application and client API bindings.  2.0.0-chaika
Hierarchical Cluster Engine Python language binding
DTMD.py
Go to the documentation of this file.
1 """
2 HCE project, Python bindings, Distributed Tasks Manager application.
3 Event objects definitions.
4 
5 @package: dtm
6 @file DTMD.py
7 @author Oleksii <developers.hce@gmail.com>
8 @link: http://hierarchical-cluster-engine.com/
9 @copyright: Copyright &copy; 2013-2014 IOIX Ukraine
10 @license: http://hierarchical-cluster-engine.com/license/
11 @since: 0.1
12 """
13 
14 
15 import logging
16 import time
17 # import sys
18 import logging.config
19 import ConfigParser
20 from cement.core import foundation
21 from transport.ConnectionBuilderLight import ConnectionBuilderLight
22 
23 from dtm.TasksExecutor import TasksExecutor # pylint: disable=W0611
24 from dtm.ClientInterfaceService import ClientInterfaceService # pylint: disable=W0611
25 from dtm.ExecutionEnvironmentManager import ExecutionEnvironmentManager # pylint: disable=W0611
26 from app.AdminInterfaceServer import AdminInterfaceServer # pylint: disable=W0611
27 from dtm.ResourcesManager import ResourcesManager # pylint: disable=W0611
28 from dtm.TasksManager import TasksManager # pylint: disable=W0611
29 from dtm.TasksDataManager import TasksDataManager # pylint: disable=W0611
30 from dtm.ResourcesStateMonitor import ResourcesStateMonitor # pylint: disable=W0611
31 from dtm.Scheduler import Scheduler # pylint: disable=W0611
32 from dtm.TasksStateUpdateService import TasksStateUpdateService # pylint: disable=W0611
33 import dtm.Constants as DTM_CONSTS
34 import app.Utils as Utils # pylint: disable=F0401
35 from app.Utils import ExceptionLog
36 
37 
38 APP_NAME = "dtmd"
39 START_APP_LOG_MSG = "Start dtm daemon."
40 STOP_APP_LOG_MSG = "Stop dtm daemon."
41 PID_FILE = "../../../run/dtm-daemon.pid"
42 APP_CONFIG_FILE = "../ini/dtm-daemon.ini"
43 LOG_CONFIG_FILE = "../ini/dtm-daemon_log.ini"
44 LOG_MSG_START_APP = "dtm-daemon start"
45 ERROR_LOAD_LOG_CONFIG_FILE = "Error loading logging config file. Exiting."
46 ERROR_LOAD_CONFIG = "Error loading config file. Exciting."
47 
48 
49 class DTMD(foundation.CementApp):
50 
51  CREATE_APP_DELAY = 0
52  START_APP_DELAY = 0
53  JOIN_APP_DELAY = 0
54 
55 
56  class Meta(object):
57  label = APP_NAME
58  def __init__(self):
59  pass
60 
61 
62  # #constructor
63  # initialize default fields
64  def __init__(self):
65  # call base class __init__ method
66  foundation.CementApp.__init__(self)
67  self.logger = None
68  self.threadObjs = None
69 
70 
71  # #setup
72  # setup application
73  def setup(self):
74  # call base class setup method
75  foundation.CementApp.setup(self)
76 
77 
78  # #run
79  # run application
80  def run(self):
81  # call base class run method
82  foundation.CementApp.run(self)
83 
84  # config section
85  self.loadConfig()
86 
87  # load self.logger config file
88  self.loadLogConfigFile()
89 
90  # load application sequence
92 
93  # create threadObjs
94  # strictly ordered sequence caused connection establishment order
95  try:
96  self.threadObjs = [self.createThreadObj(app_name) for app_name in self.threadObjs]
97  except:
98  raise
99 
100  # run apps
101  # strictly ordered
102  try:
103  [self.startThreadObj(app) for app in self.threadObjs]
104  except:
105  raise
106 
107  # wait for stop
108  # strictly ordered
109  # try:
110  # [self.joinThreadObj(app) for app in self.threadObjs]
111  # except:
112  # raise
113 
114 
115  # #createApp
116  # create application's pool
117  #
118  # @param app_name application name which instance will be created
119  # @return instance of created application
120  def createThreadObj(self, app_name):
121  app = None
122  try:
123  app = (app_name, eval(app_name)(self.config, ConnectionBuilderLight())) # pylint: disable=W0123
124  app[1].setName(app[0])
125  self.logger.debug("%s has been created!" % app_name)
126  time.sleep(self.CREATE_APP_DELAY)
127  except Exception as err:
128  ExceptionLog.handler(self.logger, err, "Exception has been thrown during %s creation. %s" % (app_name, err.message), \
129  (err))
130  raise
131  return app
132 
133 
134  # #joinThreadObj
135  # join threadObjs
136  #
137  # @param app_name application name which instance will be stopped
138  # @return None
139  def joinThreadObj(self, app):
140  try:
141  app[1].join()
142  self.logger.debug("%s waiting for stopping!" % app[0])
143  time.sleep(self.JOIN_APP_DELAY)
144  except Exception as err:
145  ExceptionLog.handler(self.logger, err, "Exception has been thrown during %s joining. %s" % (app[0], err.message), \
146  (err))
147  raise
148 
149 
150  # #startApp
151  # start application's pool
152  #
153  # @param app - application instance will be started
154  def startThreadObj(self, app):
155  try:
156  app[1].setName(app[0])
157  app[1].start()
158  self.logger.debug("%s has been started!" % app[0])
159  time.sleep(self.START_APP_DELAY)
160  except Exception as err:
161  ExceptionLog.handler(self.logger, err, "Exception has been thrown during %s start. %s" % (app[0], err.message), \
162  (err))
163  raise
164 
165 
166  # #load config from file
167  # load from cli argument or default config file
168  def loadConfig(self):
169  try:
170  self.config = ConfigParser.ConfigParser()
171  self.config.optionxform = str
172  if self.pargs.config:
173  self.config.read(self.pargs.config)
174  # self.logger.debug("Load config from cli argument: " + self.pargs.config)
175  else:
176  self.config.read(APP_CONFIG_FILE)
177  # self.logger.debug("Load config from default config file: " + APP_CONFIG_FILE)
178  except:
179  print ERROR_LOAD_CONFIG
180  raise
181 
182 
183  # #load application's start sequence
184  # the sequence is the list of class names separated by commas
185  # in "Application" section "instantiateSequence" option
187  self.logger.debug("Load application's start sequence.")
188  self.threadObjs = self.config.get("Application", "instantiateSequence").split(",")
189 
190 
191  # #load logging
192  # load logging configuration (log file, log level, filters)
193  #
194  def loadLogConfigFile(self):
195  try:
196  log_conf_file = self.config.get("Application", "log")
197  logging.config.fileConfig(log_conf_file)
198  self.logger = logging.getLogger(DTM_CONSTS.LOGGER_NAME)
199  except:
200  print ERROR_LOAD_LOG_CONFIG_FILE
201  raise
202 
203 
204  # #close
205  # close application
206  def close(self):
207  if self.logger is not None:
208  self.logger.debug("dtm daemon is closed")
209  # call base class run method
210  foundation.CementApp.close(self)
def loadConfig(self)
Definition: DTMD.py:168
def setup(self)
Definition: DTMD.py:73
int START_APP_DELAY
Definition: DTMD.py:52
def joinThreadObj(self, app)
Definition: DTMD.py:139
def startThreadObj(self, app)
Definition: DTMD.py:154
def loadLogConfigFile(self)
Definition: DTMD.py:194
def run(self)
Definition: DTMD.py:80
int JOIN_APP_DELAY
Definition: DTMD.py:53
def loadStartAppsSequence(self)
Definition: DTMD.py:186
int CREATE_APP_DELAY
Definition: DTMD.py:51
Class hides routines of bulding connection objects.
def createThreadObj(self, app_name)
Definition: DTMD.py:120
def close(self)
Definition: DTMD.py:206
Definition: join.py:1
def __init__(self)
Definition: DTMD.py:64
def __init__(self)
Definition: DTMD.py:58