<commit_before># -*- coding: utf-8 -*-
##
##
## This file is part of CDS Indico.
## Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 CERN.
##
## CDS Indico is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License as
## published by the Free Software Foundation; either version 2 of the
## License, or (at your option) any later version.
##
## CDS Indico is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with CDS Indico; if not, write to the Free Software Foundation, Inc.,
## 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.

import time, signal, copy, sys, traceback
from threading import Thread
import smtplib
from datetime import datetime, timedelta

import ZODB
from persistent import Persistent
from BTrees import OOBTree

import MaKaC.common.db as db
import os
from Configuration import Config
from MaKaC.common.info import HelperMaKaCInfo
from Counter import Counter
from MaKaC.user import Avatar
from MaKaC.common.timezoneUtils import nowutc
from MaKaC.common.contextManager import ContextManager
from MaKaC.statistics import CategoryStatistics
from MaKaC.webinterface.mail import GenericMailer, GenericNotification
from MaKaC.trashCan import TrashCanManager
from MaKaC.i18n import _
from pytz import timezone
from pytz import all_timezones

class toExec:
    """
    Get the list of task and for each one, check if its time to run it
    """
    def __init__(self, logFilename=None):
        self._file=None
        self._logFilename=logFilename

    def _initLog(self):
        if self._logFilename:
            i = 0
            while i < 10:
                try:
                    self._file=open(self._logFilename,"a")
                    i = 11
                except IOError, e:
                    if i < 10:
                        i += 1
                        time.sleep(i)
                    else:
                        self._printOutput(e)
                        raise e

    def _closeLog(self):
        if self._file:
            self._file.close()

    def _printOutput(self, text):
        text = "[%s] %s"%(id(self),text)
        if self._logFilename:
            if self._file:
                print >> self._file, text
                self._file.flush()
                return
        print text

    def _runObj(self, obj):
        # create the context
        ContextManager.create()

        try:
            obj.run()
        except Exception, e:
            self._printOutput(e)
            self._sendErrorEmail(e)

        # destroy the context
        ContextManager.destroy()


    def execute(self):
        try:
            self._initLog()
            self._printOutput("***Starting execute***\n")
            ### Get the connection in order to avoid that to threads share the same one
            conn = db.DBMgr().getInstance()
            conn.startRequest()
            ###
            taskList = HelperTaskList().getTaskListInstance()
            tasks = taskList.getTasks()
            if len(tasks) == 0:
                self._printOutput("--->No tasks!")
            else:
                self._printOutput("Execute at %s\n"%datetime.now())
            taskids = [i.getId() for i in tasks]
            for id in taskids:
                ### check if another thread have already removed the task
                conn.sync()
                if not id in taskList.listTask.keys():
                    continue
                task = taskList.getTaskById(id)
                ###
                if task.isRunning():
                    self._printOutput("\t--->Task <%s> already running"%task.getId())
                else:
                    try:
                        self._printOutput("\t--->Running task <%s>"%task.getId())

                        if task.getStartDate() == None or task.getStartDate() < nowutc():
                            self._printOutput("\t (SINGLE RUN)")
                            # the task is started
                            if task.getInterval() == None or task.getLastDate() == None:
                                # the task is run once or never runned
                                task.setRunning(True)
                                conn.commit()
                                abort=task.prerun()
                                if abort:
                                    self._printOutput("\t<---Task <%s> was aborted."%task.getId())
                                    task.setRunning(False)
                                    conn.commit()
                                    continue
                                for obj in task.getObjList():
                                    self._printOutput("\t\tRunning object <%s> (start date: %s)"%(obj.getId(),task.getStartDate()))
                                    #task.setLastDate(datetime.now())

                                    self._runObj(obj)

                                    self._printOutput("\t\tEnd object")
                                task.setLastDate(nowutc())
                                task.setRunning(False)
                                if task.getInterval() == None:
                                    # if run once, delete the task from list
                                    taskList.removeTask(task)
                                    self._printOutput("\t\tTask was removed")
                                else:
                                    self._printOutput("\t\tTask is periodic (getInterval: %s)" % task.getInterval())
                            else:
                                self._printOutput("\t (PERIODIC TASK)")
                                # the task is to be run multiple times
                                if task.getLastDate() + task.getInterval() < nowutc():
                                    task.setRunning(True)
                                    conn.commit()
                                    abort=task.prerun()
                                    if abort:
                                        self._printOutput("\t<---Task <%s> was aborted."%task.getId())
                                        task.setRunning(False)
                                        conn.commit()
                                        continue
                                    # it's time to launch the task!!!
                                    for obj in task.getObjList():
                                        self._printOutput("\tRunning object %s (last date: %s)"%(obj.getId(),task.getLastDate()))
                                        #task.setLastDate(datetime.now())

                                        self._runObj(obj)

                                        self._printOutput("\t\tEnd object")
                                    if task.getLastDate() and task.getEndDate():
                                        if task.getLastDate() + task.getInterval() > task.getEndDate():
                                            # the task is finish, delete it from list
                                            taskList.removeTask(task)
                                            self._printOutput("\t\tTask was removed")
                                    task.setLastDate(nowutc())
                                    task.setRunning(False)
                        self._printOutput("\t<---end task <%s>\n"%task.getId())
                        conn.commit()
                    except Exception, e:
                        conn.sync()
                        task.setRunning(False)
                        conn.commit()
                        ty, ex, tb = sys.exc_info()
                        tracebackList = traceback.format_list( traceback.extract_tb( tb ) )
                        self._printOutput("*****---->[ERROR]:%s\nTraceback:%s"%(e,"\n".join(tracebackList)))
                        self._sendErrorEmail(e)
            conn.endRequest()
            self._printOutput("***end execute***\n\n")
            self._closeLog()
        except Exception, e:
            ty, ex, tb = sys.exc_info()
            tracebackList = traceback.format_list( traceback.extract_tb( tb ) )
            self._printOutput("*****---->[ERROR]:%s\nTraceback:%s"%(e,"\n".join(tracebackList)))
            self._sendErrorEmail(e)

    def _sendErrorEmail(self, e):
        ty, ex, tb = sys.exc_info()
        tracebackList = traceback.format_list( traceback.extract_tb( tb ) )
        sm = sendMail()
        sm.setFromAddr(HelperMaKaCInfo.getMaKaCInfoInstance().getSupportEmail())
        sm.addToAddr(HelperMaKaCInfo.getMaKaCInfoInstance().getSupportEmail())<commit_msg>[FIX] RB DB open (in case it's active) for tasks<commit_after># -*- coding: utf-8 -*-
##
##
## This file is part of CDS Indico.
## Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 CERN.
##
## CDS Indico is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License as
## published by the Free Software Foundation; either version 2 of the
## License, or (at your option) any later version.
##
## CDS Indico is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with CDS Indico; if not, write to the Free Software Foundation, Inc.,
## 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.

import time, signal, copy, sys, traceback
from threading import Thread
import smtplib
from datetime import datetime, timedelta

import ZODB
from persistent import Persistent
from BTrees import OOBTree

import MaKaC.common.db as db
import os
from Configuration import Config
from MaKaC.common.info import HelperMaKaCInfo
from Counter import Counter
from MaKaC.user import Avatar
from MaKaC.common.timezoneUtils import nowutc
from MaKaC.common.contextManager import ContextManager
from MaKaC.statistics import CategoryStatistics
from MaKaC.webinterface.mail import GenericMailer, GenericNotification
from MaKaC.trashCan import TrashCanManager
from MaKaC.i18n import _
from pytz import timezone
from pytz import all_timezones

from MaKaC.rb_location import CrossLocationDB

class toExec:
    """
    Get the list of task and for each one, check if its time to run it
    """
    def __init__(self, logFilename=None):
        self._file=None
        self._logFilename=logFilename

    def _initLog(self):
        if self._logFilename:
            i = 0
            while i < 10:
                try:
                    self._file=open(self._logFilename,"a")
                    i = 11
                except IOError, e:
                    if i < 10:
                        i += 1
                        time.sleep(i)
                    else:
                        self._printOutput(e)
                        raise e

    def _closeLog(self):
        if self._file:
            self._file.close()

    def _printOutput(self, text):
        text = "[%s] %s"%(id(self),text)
        if self._logFilename:
            if self._file:
                print >> self._file, text
                self._file.flush()
                return
        print text

    def _runObj(self, obj):
        # create the context
        ContextManager.create()

        try:
            obj.run()
        except Exception, e:
            self._printOutput(e)
            self._sendErrorEmail(e)

        # destroy the context
        ContextManager.destroy()


    def execute(self):
        try:
            self._initLog()
            self._printOutput("***Starting execute***\n")
            ### Get the connection in order to avoid that to threads share the same one
            conn = db.DBMgr().getInstance()
            conn.startRequest()

            minfo = HelperMaKaCInfo.getMaKaCInfoInstance()

            if minfo.getRoomBookingModuleActive():
                CrossLocationDB.connect()
            ###
            taskList = HelperTaskList().getTaskListInstance()
            tasks = taskList.getTasks()
            if len(tasks) == 0:
                self._printOutput("--->No tasks!")
            else:
                self._printOutput("Execute at %s\n"%datetime.now())
            taskids = [i.getId() for i in tasks]
            for id in taskids:
                ### check if another thread have already removed the task
                conn.sync()
                if not id in taskList.listTask.keys():
                    continue
                task = taskList.getTaskById(id)
                ###
                if task.isRunning():
                    self._printOutput("\t--->Task <%s> already running"%task.getId())
                else:
                    try:
                        self._printOutput("\t--->Running task <%s>"%task.getId())

                        if task.getStartDate() == None or task.getStartDate() < nowutc():
                            self._printOutput("\t (SINGLE RUN)")
                            # the task is started
                            if task.getInterval() == None or task.getLastDate() == None:
                                # the task is run once or never runned
                                task.setRunning(True)
                                conn.commit()
                                abort=task.prerun()
                                if abort:
                                    self._printOutput("\t<---Task <%s> was aborted."%task.getId())
                                    task.setRunning(False)
                                    conn.commit()
                                    continue
                                for obj in task.getObjList():
                                    self._printOutput("\t\tRunning object <%s> (start date: %s)"%(obj.getId(),task.getStartDate()))
                                    #task.setLastDate(datetime.now())

                                    self._runObj(obj)

                                    self._printOutput("\t\tEnd object")
                                task.setLastDate(nowutc())
                                task.setRunning(False)
                                if task.getInterval() == None:
                                    # if run once, delete the task from list
                                    taskList.removeTask(task)
                                    self._printOutput("\t\tTask was removed")
                                else:
                                    self._printOutput("\t\tTask is periodic (getInterval: %s)" % task.getInterval())
                            else:
                                self._printOutput("\t (PERIODIC TASK)")
                                # the task is to be run multiple times
                                if task.getLastDate() + task.getInterval() < nowutc():
                                    task.setRunning(True)
                                    conn.commit()
                                    abort=task.prerun()
                                    if abort:
                                        self._printOutput("\t<---Task <%s> was aborted."%task.getId())
                                        task.setRunning(False)
                                        conn.commit()
                                        continue
                                    # it's time to launch the task!!!
                                    for obj in task.getObjList():
                                        self._printOutput("\tRunning object %s (last date: %s)"%(obj.getId(),task.getLastDate()))
                                        #task.setLastDate(datetime.now())

                                        self._runObj(obj)

                                        self._printOutput("\t\tEnd object")
                                    if task.getLastDate() and task.getEndDate():
                                        if task.getLastDate() + task.getInterval() > task.getEndDate():
                                            # the task is finish, delete it from list
                                            taskList.removeTask(task)
                                            self._printOutput("\t\tTask was removed")
                                    task.setLastDate(nowutc())
                                    task.setRunning(False)
                        self._printOutput("\t<---end task <%s>\n"%task.getId())
                        conn.commit()
                    except Exception, e:
                        conn.sync()
                        task.setRunning(False)
                        conn.commit()
                        ty, ex, tb = sys.exc_info()
                        tracebackList = traceback.format_list( traceback.extract_tb( tb ) )
                        self._printOutput("*****---->[ERROR]:%s\nTraceback:%s"%(e,"\n".join(tracebackList)))
                        self._sendErrorEmail(e)

            if minfo.getRoomBookingModuleActive():
                CrossLocationDB.commit()
                CrossLocationDB.disconnect()

            conn.endRequest()

            self._printOutput("***end execute***\n\n")
            self._closeLog()
        except Exception, e:
            ty, ex, tb = sys.exc_info()

            tracebackList = traceback.format_list( traceback.extract_tb( tb ) )
            self._printOutput("*****---->[ERROR]:%s\nTraceback:%s"%(e,"\n".join(tracebackList)))
            self._sendErrorEmail(e)

    def _sendErrorEmail(self, e):
        ty, ex, tb = sys.exc_info()
        tracebackList = traceback.format_list( traceback.extract_tb( tb ) )
        sm = sendMail()
        sm.setFromAddr(HelperMaKaCInfo.getMaKaCInfoInstance().getSupportEmail())
        sm.addToAddr(HelperMaKaCInfo.getMaKaCInfoInstance().getSupportEmail())
{'fraction_non_alphanumeric': 0.09337466098411469, 'fraction_numerical': 0.005645652294238114, 'mean_word_length': 1.2772876228888328, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 92, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5878088', 'n_tokens_mistral': 4507, 'n_tokens_neox': 4205, 'n_words': 1173}