def session(self): """ Context Manager: start a new session and ensure that the session's cursor is: * rollbacked on errors * commited at the end of the ``with`` context when no error occured * always closed at the end of the ``with`` context * it handles the registry signaling """ with openerp.api.Environment.manage(): db = openerp.sql_db.db_connect(self.db_name) session = ConnectorSession(db.cursor(), self.uid, context=self.context) try: with session.env.clear_upon_failure(): RegistryManager.check_registry_signaling(self.db_name) yield session RegistryManager.signal_caches_change(self.db_name) except: session.rollback() raise else: session.commit() finally: session.close()
def _work_database(self, cr): db_name = cr.dbname try: cr.execute( "SELECT 1 FROM ir_module_module " "WHERE name = %s " "AND state = %s", ('connector', 'installed'), log_exceptions=False) except ProgrammingError as err: if unicode(err).startswith( 'relation "ir_module_module" does not exist'): _logger.debug( 'Database %s is not an Odoo database,' ' connector worker not started', db_name) else: raise else: if cr.fetchone(): RegistryManager.check_registry_signaling(db_name) registry = openerp.pooler.get_pool(db_name) if registry: queue_worker = registry['queue.worker'] queue_worker.assign_then_enqueue(cr, openerp.SUPERUSER_ID, max_jobs=MAX_JOBS) RegistryManager.signal_caches_change(db_name)
def session(self): """ Context Manager: start a new session and ensure that the session's cursor is: * rollbacked on errors * commited at the end of the ``with`` context when no error occured * always closed at the end of the ``with`` context * it handles the registry signaling """ with openerp.api.Environment.manage(): db = openerp.sql_db.db_connect(self.db_name) session = ConnectorSession(db.cursor(), self.uid, context=self.context) try: with session.env.clear_upon_failure(): RegistryManager.check_registry_signaling(self.db_name) yield session RegistryManager.signal_caches_change(self.db_name) except: session.rollback() raise else: session.commit() finally: session.close()
def session(self, db_name): """ Context Manager: start a new session and ensure that the session's cursor is: * rollbacked on errors * commited at the end of the ``with`` context when no error occured * always closed at the end of the ``with`` context * it handles the registry signaling """ db = openerp.sql_db.db_connect(db_name) cr = db.cursor() try: RegistryManager.check_registry_signaling(db_name) yield cr RegistryManager.signal_caches_change(db_name) except: cr.rollback() raise else: cr.commit() finally: cr.close()
def _work_database(self, cr): db_name = cr.dbname try: cr.execute("SELECT 1 FROM ir_module_module " "WHERE name = %s " "AND state = %s", ('connector', 'installed'), log_exceptions=False) except ProgrammingError as err: if unicode(err).startswith( 'relation "ir_module_module" does not exist'): _logger.debug('Database %s is not an Odoo database,' ' connector worker not started', db_name) else: raise else: if cr.fetchone(): RegistryManager.check_registry_signaling(db_name) registry = openerp.pooler.get_pool(db_name) if registry: queue_worker = registry['queue.worker'] queue_worker.assign_then_enqueue(cr, openerp.SUPERUSER_ID, max_jobs=MAX_JOBS) RegistryManager.signal_caches_change(db_name)
def run(self): # Add logging messages logging.info('Enter new process...') cr = db_connect(self.dbname).cursor() with openerp.api.Environment.manage(): RegistryManager.check_registry_signaling(self.dbname) # create an Environment env = openerp.api.Environment(cr, self.uid, {}) deferred_obj = env['deferred_processing.task'].\ browse(self._process_id) self._start_time = time.time() deferred_obj.write({'state': 'process'}) logging.info('Executing deferred task %s' % self._process_id) cr.commit() self._state = 'process' if 'context' in self.kwargs: self.kwargs['context'].update({'deferred_process': self}) # Prepare the template to send notification if self.send_email: template_env = env['email.template'] mail_template = template_env.\ search([('name', '=', 'Deferred Processing')], limit=1) m_template_id = mail_template and mail_template.id or False if not m_template_id: self.send_email = False try: # Add a new way to Call workflow function if self.workflow: # Call workflow function self.result = self.method(self.uid, *(self.args + (cr,))) elif self.report: # call report function # Because the core report module still use old API # so, need use cr, uid here self.result = self.method(cr, self.uid, *self.args) else: # Call method New API instance = self.method.__self__ # Because old cursor was closed, replace new env here instance.env = env self.result = self.method(*self.args, **self.kwargs) except Exception as exc: # Handling all exceptions. # Record the exception into field # note. cr.rollback() # update state (interrupt) and note logging.error(exc) deferred_obj.write({'state': 'interrupt', 'note': exc}) # Control the notification if self.send_email: logging.warning('Sending email notification...') mail_template.send_mail(self._process_id, force_send=True) cr.commit() cr.close() return self.refresh_status() self.get_speed() self._state = 'done' self._processed = self._total self._progress = 100.0 to_write = {'state': 'done'} # type(self.result) != bool # Because self._result_parser = "result[0]", # Error if self.result is a # dictionary/number if self.result and type(self.result) in (list, tuple): if self._result_parser: self.result = eval( self._result_parser, {}, {'result': self.result}) to_write['result'] = base64.encodestring(self.result) # Face the problem with cr.rollback() # Nothing change after run deferred task cr.commit() deferred_obj.write(to_write) # Control the notification if self.send_email: logging.info('Sending email notification...') mail_template.send_mail(self._process_id, force_send=True) cr.commit() logging.info('Closing transaction...') cr.close() return return
# Extract and hack part of the Odoo's Root.dispatch() method, # and use it to generate a valid Odoo's Response object. def _dispatch_nodb(): try: func, arguments = root.nodb_routing_map.bind_to_environ( request.httprequest.environ).match() except werkzeug.exceptions.HTTPException, e: return request._handle_exception(e) request.set_handler(func, arguments, "none") result = request.dispatch() return result with request: db = request.session.db if db: RegistryManager.check_registry_signaling(db) try: with openerp.tools.mute_logger('openerp.sql_db'): ir_http = request.registry['ir.http'] except (AttributeError, psycopg2.OperationalError): # psycopg2 error or attribute error while constructing # the registry. That means the database probably does # not exists anymore or the code doesnt match the db. # Log the user out and fall back to nodb request.session.logout() result = _dispatch_nodb() else: result = ir_http._dispatch() RegistryManager.signal_caches_change(db) else: result = _dispatch_nodb()
# Extract and hack part of the Odoo's Root.dispatch() method, # and use it to generate a valid Odoo's Response object. def _dispatch_nodb(): try: func, arguments = root.nodb_routing_map.bind_to_environ( request.httprequest.environ).match() except werkzeug.exceptions.HTTPException, e: return request._handle_exception(e) request.set_handler(func, arguments, "none") result = request.dispatch() return result with request: db = request.session.db if db: RegistryManager.check_registry_signaling(db) try: with openerp.tools.mute_logger('openerp.sql_db'): ir_http = request.registry['ir.http'] except (AttributeError, psycopg2.OperationalError): # psycopg2 error or attribute error while constructing # the registry. That means the database probably does # not exists anymore or the code doesnt match the db. # Log the user out and fall back to nodb request.session.logout() result = _dispatch_nodb() else: result = ir_http._dispatch() RegistryManager.signal_caches_change(db) else: result = _dispatch_nodb()