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
DBTask.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 
3 
4 """
5 HCE project, Python bindings, Distributed Tasks Manager application.
6 Event objects definitions.
7 
8 @package: dc
9 @file db-task.py
10 @author igor
11 @link: http://hierarchical-cluster-engine.com/
12 @copyright: Copyright © 2013-2014 IOIX Ukraine
13 @license: http://hierarchical-cluster-engine.com/license/
14 @since: 0.1
15 """
16 
17 try:
18  import cPickle as pickle
19 except ImportError:
20  import pickle
21 
22 import os
23 import sys
24 import ConfigParser
25 import logging.config
26 import MySQLdb
27 from cement.core import foundation
28 #import dc.EventObjects as dc_event
29 #from dc.Constants import DRCESyncTasksCover
30 import dc_db.Constants as Constants
31 from dc_db.TasksManager import TasksManager
32 #from dc_db.Constants import LOGGER_NAME # pylint: disable=F0401
33 
34 import app.Utils as Utils # pylint: disable=F0401
35 import app.Consts as APP_CONSTS
36 from app.Utils import ExceptionLog
37 from app.Exceptions import DatabaseException
38 
39 
40 #logger = logging.getLogger(LOGGER_NAME)
41 
42 
43 def exceptionLogging(abortStr):
44  sys.stderr.write(abortStr)
45 
46 
47 class DBTask(foundation.CementApp):
48 
49 
50  class Meta(object):
51  label = Constants.APP_NAME
52  def __init__(self):
53  pass
54 
55 
56  # #constructor
57  # initialize default fields
58  def __init__(self):
59  # call base class __init__ method
60  foundation.CementApp.__init__(self)
61  self.errorCode = Constants.EXIT_CODE_OK
62  self.errorStr = ""
63 
64 
65  # #setup
66  # setup application
67  def setup(self):
68  # call base class setup method
69  foundation.CementApp.setup(self)
70 
71 
72  # #run
73  # run application
74  def run(self):
75  # call base class run method
76  foundation.CementApp.run(self)
77 
78  if self.pargs.config is not None:
79  try:
80  drceSyncTasksCoverPickled = sys.stdin.read()
81 
82  if not os.path.isfile(self.pargs.config):
83  raise Exception('Config file %s not found' % self.pargs.config)
84  cfgParser = ConfigParser.ConfigParser()
85  cfgParser.read(self.pargs.config)
86 
87  # init loggers
88  logging.config.fileConfig(cfgParser.get("TasksManager", "log_cfg"))
89 
90  # Logger initialization
91  logger = Utils.MPLogger().getLogger()
92  #logger.debug("Receved object:\n" + str(drceSyncTasksCoverPickled) + "\n")
93 
94  dbTask = TasksManager(cfgParser)
95 
96 
97  if len(drceSyncTasksCoverPickled) == 0 or drceSyncTasksCoverPickled[-1] != '\x2E':
98  drceSyncTasksCoverPickled += '\x2E'
99 
100  drceSyncTasksCover = pickle.loads(drceSyncTasksCoverPickled)
101  responseDRCESyncTasksCover = dbTask.process(drceSyncTasksCover)
102  sys.stdout.write(pickle.dumps(responseDRCESyncTasksCover))
103  sys.stdout.flush()
104 
105  if dbTask.SQLErrorCode != Constants.EXIT_CODE_OK:
106  logger.error("Exited with dbTask.SQLErrorCode=" + str(dbTask.SQLErrorCode))
107  #sys.stderr.write("Exited with dbTask.SQLErrorCode=" + str(dbTask.SQLErrorCode) + "\n" +
108  # "dbTask.SQLErrorMessage=\"" + dbTask.SQLErrorString + "\"\n")
109 
110 
111  # log message about profiler
112  logger.info(APP_CONSTS.LOGGER_DELIMITER_LINE)
113  # stop profiling
114  self.errorCode = dbTask.SQLErrorCode
115  raise DatabaseException(dbTask.SQLErrorString)
116 
117  else:
118  # log message about profiler
119  logger.info(APP_CONSTS.LOGGER_DELIMITER_LINE)
120  except MySQLdb.OperationalError as err: # pylint: disable=E1101
121  ExceptionLog.handler(logger, err, "Aborted cause exception! MySQLdb.OperationalError:")
122  exceptionLogging("Aborted cause exception! MySQLdb.OperationalError {%s}\n" % str(err))
123  self.errorCode = Constants.EXIT_CODE_GLOBAL_ERROR
124  except MySQLdb.Error as err: # pylint: disable=E1101
125  ExceptionLog.handler(logger, err, "Aborted cause exception! Error %d: %s" % (err.args[0], err.args[1]))
126  exceptionLogging("Aborted cause exception! Error %d: %s" % (err.args[0], err.args[1]))
127  self.errorCode = Constants.EXIT_CODE_GLOBAL_ERROR
128  except DatabaseException, err:
129  ExceptionLog.handler(logger, err, "Aborted cause exception! ")
130  exceptionLogging("Aborted cause exception! {%s}\n" % str(err))
131  self.errorCode = Constants.EXIT_CODE_MYSQL_ERROR
132  except Exception as err:
133  ExceptionLog.handler(logger, err, "Aborted cause exception! ")
134  exceptionLogging("Aborted cause exception! {%s}\n" % str(err))
135  self.errorCode = Constants.EXIT_CODE_GLOBAL_ERROR
136  except: # pylint: disable=W0702
137  ExceptionLog.handler(logger, None, "Unknown exception")
138  # log message about profiler
139  logger.info(APP_CONSTS.LOGGER_DELIMITER_LINE)
140  # stop profiling
141  sys.stdout.flush()
142  self.errorCode = APP_CONSTS.EXIT_FAILURE
143  return
144  else:
145  logger.error("Invalid command line arguments. To help - python ./db_task.py -h")
146  sys.stderr.write("Invalid command line arguments. To help - python ./db_task.py -h")
147 
148  # log message about profiler
149  logger.info(APP_CONSTS.LOGGER_DELIMITER_LINE)
150  # stop profiling
151  sys.stdout.flush()
152  self.errorCode = Constants.EXIT_CODE_CONFIG_ERROR
153 
154 
155  # #close
156  # close application
157  def close(self, code=None): # pylint: disable=W0221,W0613
158  sys.stdout.flush()
159  self.errorCode = Constants.EXIT_CODE_OK
160  foundation.CementApp.close(self)
def __init__(self)
Definition: DBTask.py:52
def exceptionLogging(abortStr)
Definition: DBTask.py:43
def run(self)
Definition: DBTask.py:74
def setup(self)
Definition: DBTask.py:67
errorCode
drceSyncTasksCoverPickled = sys.stdin.read()
Definition: DBTask.py:61
def close(self, code=None)
Definition: DBTask.py:157
def __init__(self)
Definition: DBTask.py:58