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
DCD.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: dc
6 @file DCD.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 time
16 import logging # pylint: disable=unused-import
17 import logging.config
18 import ConfigParser
19 from cement.core import foundation
20 # from transport.ConnectionBuilderLight import ConnectionBuilderLight
21 from app import Utils
22 from app.AdminInterfaceServer import AdminInterfaceServer # pylint: disable=unused-import
23 import app.Consts as APP_CONSTS
24 from dc.ClientInterfaceService import ClientInterfaceService # pylint: disable=unused-import
25 from dc.BatchTasksManager import BatchTasksManager # pylint: disable=unused-import
26 from dc.BatchTasksManagerRealTime import BatchTasksManagerRealTime # pylint: disable=unused-import
27 from dc.BatchTasksManagerProcess import BatchTasksManagerProcess # pylint: disable=unused-import
28 from dc.SitesManager import SitesManager # pylint: disable=unused-import
29 from transport.ConnectionBuilderLight import ConnectionBuilderLight
30 
31 
32 APP_NAME = "dc"
33 START_APP_LOG_MSG = "Start dc daemon."
34 STOP_APP_LOG_MSG = "Stop dc daemon."
35 PID_FILE = "../../../run/dc-daemon.pid"
36 APP_CONFIG_FILE = "../ini/dc-daemon.ini"
37 LOG_CONFIG_FILE = "../ini/dc-daemon_log.ini"
38 LOG_MSG_START_APP = "dc-daemon start"
39 LOG_MSG_CLOSE_APP = "Close dc daemon app"
40 ERR_MSG_CLOSE_APP = "Error close dc daemon app"
41 ERROR_LOAD_LOG_CONFIG_FILE = "Error loading logging config file. Exiting."
42 ERROR_LOAD_CONFIG = "Error loading config file. Exciting."
43 
44 
45 class DCD(foundation.CementApp):
46 
47  CREATE_APP_DELAY = 0
48  START_APP_DELAY = 0
49  JOIN_APP_DELAY = 0
50 
51 
52  class Meta(object):
53  label = APP_NAME
54  def __init__(self):
55  pass
56 
57 
58  # #constructor
59  # initialize default fields
60  def __init__(self):
61  # call base class __init__ method
62  foundation.CementApp.__init__(self)
63  self.logger = None
64  self.threadObjs = None
65 
66 
67  # #setup
68  # setup application
69  def setup(self):
70  # call base class setup method
71  foundation.CementApp.setup(self)
72 
73 
74  # #run
75  # run application
76  def run(self):
77  # call base class run method
78  foundation.CementApp.run(self)
79 
80  # config section
81  self.loadConfig()
82 
83  # load logger config file
84  self.loadLogConfigFile()
85 
86  # load application sequence
88 
89  # create threadObjs
90  # strictly ordered sequence caused connection establishment order
91  try:
92  self.threadObjs = [self.createThreadObj(threadObj) for threadObj in self.threadObjs]
93  except:
94  self.logger.error("Exception during creation thread-based class!\n" + Utils.getTracebackInfo())
95  raise
96 
97  # run apps
98  # strictly ordered
99  try:
100  [self.startThreadObj(threadObj) for threadObj in self.threadObjs]
101  except:
102  self.logger.error("Exception during run thread-based class!\n" + Utils.getTracebackInfo())
103  raise
104 
105  # wait for stop
106  # strictly ordered
107  try:
108  [self.joinThreadObj(threadObj) for threadObj in self.threadObjs]
109  except:
110  self.logger.error("Exception during stop thread-based class!\n" + Utils.getTracebackInfo())
111  raise
112 
113 
114  # #createApp
115  # create application's pool
116  #
117  # @param app_name application name which instance will be created
118  # @return instance of created application
119  def createThreadObj(self, app_name):
120  app = None
121  try:
122  app = (app_name, eval(app_name)(self.config, ConnectionBuilderLight())) # pylint: disable = I0011, eval-used
123  app[1].setName(app[0])
124  self.logger.debug("%s has been created!", app_name)
125  time.sleep(self.CREATE_APP_DELAY)
126  except:
127  self.logger.error("Exception has been thrown during %s creation!", app_name)
128  raise
129  return app
130 
131 
132  # #joinThreadObj
133  # join threadObjs
134  #
135  # @param app_name application name which instance will be stopped
136  # @return None
137  def joinThreadObj(self, app):
138  try:
139  app[1].join()
140  self.logger.debug("%s waiting for stopping!", app[0])
141  time.sleep(self.JOIN_APP_DELAY)
142  except:
143  self.logger.error("Exception has been thrown during %s joining!", app[0])
144  raise
145 
146 
147  # #startApp
148  # start application's pool
149  #
150  # @param app - application instance will be started
151  def startThreadObj(self, app):
152  try:
153  app[1].setName(app[0])
154  app[1].start()
155  self.logger.debug("%s has been started!", app[0])
156  time.sleep(self.START_APP_DELAY)
157  except:
158  self.logger.error("Exception has been thrown during %s start!", app[0])
159  raise
160 
161 
162  # #load config from file
163  # load from cli argument or default config file
164  def loadConfig(self):
165  try:
166  self.config = ConfigParser.ConfigParser()
167  self.config.optionxform = str
168  if self.pargs.config:
169  self.config.read(self.pargs.config)
170  else:
171  raise Exception("Error load config")
172  # self.config.read(APP_CONFIG_FILE)
173  except Exception as err:
174  print ERROR_LOAD_CONFIG + err.message
175  raise
176 
177 
178  # #load application's start sequence
179  # the sequence is the list of class names separated by commas
180  # in "Application" section "instantiateSequence" option
182  self.logger.debug("Load application's start sequence.")
183  self.threadObjs = self.config.get("Application", "instantiateSequence").split(",")
184 
185 
186  # #load logging
187  # load logging configuration (log file, log level, filters)
188  #
189  def loadLogConfigFile(self):
190  try:
191  log_conf_file = self.config.get("Application", "log")
192  logging.config.fileConfig(log_conf_file)
193  #self.logger = logging.getLogger(APP_NAME)
194  # Logger initialization
195  self.logger = logging.getLogger(APP_CONSTS.LOGGER_NAME)
196 
197  except Exception as err:
198  print ERROR_LOAD_LOG_CONFIG_FILE + err.message
199  raise
200 
201 
202  # #close
203  # close application
204  def close(self, code=None):
205  if self.logger is not None:
206  self.logger.debug(LOG_MSG_CLOSE_APP)
207  # call base class run method
208  foundation.CementApp.close(self)
def close(self, code=None)
Definition: DCD.py:204
def loadLogConfigFile(self)
Definition: DCD.py:189
config
Definition: DCD.py:166
def startThreadObj(self, app)
Definition: DCD.py:151
def joinThreadObj(self, app)
Definition: DCD.py:137
def __init__(self)
Definition: DCD.py:60
Class hides routines of bulding connection objects.
def loadStartAppsSequence(self)
Definition: DCD.py:181
int START_APP_DELAY
Definition: DCD.py:48
int JOIN_APP_DELAY
Definition: DCD.py:49
logger
Definition: DCD.py:63
def __init__(self)
Definition: DCD.py:54
int CREATE_APP_DELAY
Definition: DCD.py:47
def loadConfig(self)
Definition: DCD.py:164
-mask-info
def setup(self)
Definition: DCD.py:69
threadObjs
Definition: DCD.py:64
def run(self)
Definition: DCD.py:76
Definition: join.py:1
def createThreadObj(self, app_name)
Definition: DCD.py:119