def runengine(request, *kw, **kwargs): if request.method == 'GET': #needed to find out right arguments: # 1. python_executable_path. Problem in virtualenv. Use setting in bots.ini if there # 2. botsengine_path. Problem in apache. Use setting in bots.ini if there # 3. environment (config). OK # 4. commandstorun (eg --new) and routes. OK python_executable_path = botsglobal.ini.get('settings', 'python_executable_path', sys.executable) botsengine_path = botsglobal.ini.get( 'settings', 'botsengine_path', os.path.join(os.path.dirname(os.path.abspath(sys.argv[0])), 'bots-engine.py')) environment = '-c' + botsglobal.ini.get('directories', 'config_org') lijst = [python_executable_path, botsengine_path, environment] # get 4. commandstorun (eg --new) and routes via request if 'clparameter' in request.GET: lijst.append(request.GET['clparameter']) #either bots-engine is run directly or via jobqueue-server: if botsglobal.ini.getboolean( 'jobqueue', 'enabled', False ): #run bots-engine via jobqueue-server; reports back if job is queued import job2queue terug = job2queue.send_job_to_jobqueue(lijst) messages.add_message(request, messages.INFO, job2queue.JOBQUEUEMESSAGE2TXT[terug]) botsglobal.logger.info(job2queue.JOBQUEUEMESSAGE2TXT[terug]) else: #run bots-engine direct.; reports back if bots-engien is started succesful. **not reported: problems with running. botsglobal.logger.info( _(u'Run bots-engine with parameters: "%(parameters)s"'), {'parameters': unicode(lijst)}) #first check if another instance of bots-engine is running/if port is free try: engine_socket = botslib.check_if_other_engine_is_running() except socket.error: notification = _( u'Trying to run "bots-engine", but another instance of "bots-engine" is running. Please try again later.' ) messages.add_message(request, messages.INFO, notification) botsglobal.logger.info(notification) return django.shortcuts.redirect('/home') else: engine_socket.close() #and close the socket #run engine try: terug = subprocess.Popen(lijst).pid except Exception as msg: notification = _( u'Errors while trying to run bots-engine: "%s".') % msg messages.add_message(request, messages.INFO, notification) botsglobal.logger.info(notification) else: messages.add_message(request, messages.INFO, _(u'Bots-engine is started.')) return django.shortcuts.redirect('/home')
def runengine(request, *kw, **kwargs): if request.method == 'GET': #needed to find out right arguments: # 1. python_executable_path. Problem in virtualenv. Use setting in bots.ini if there # 2. botsengine_path. Problem in apache. Use setting in bots.ini if there # 3. environment (config). OK # 4. commandstorun (eg --new) and routes. OK python_executable_path = botsglobal.ini.get('settings', 'python_executable_path', sys.executable) botsengine_path = botsglobal.ini.get('settings', 'botsengine_path', os.path.join( os.path.dirname(os.path.abspath(sys.argv[0])), 'bots-engine.py')) environment = '-c' + botsglobal.ini.get('directories', 'config_org') lijst = [python_executable_path, botsengine_path, environment] # get 4. commandstorun (eg --new) and routes via request if 'clparameter' in request.GET: lijst.append(request.GET['clparameter']) #either bots-engine is run directly or via jobqueue-server: # run bots-engine via jobqueue-server; reports back if job is queued if botsglobal.ini.getboolean('jobqueue', 'enabled', False): import job2queue terug = job2queue.send_job_to_jobqueue(lijst) messages.add_message(request, messages.INFO, job2queue.JOBQUEUEMESSAGE2TXT[terug]) botsglobal.logger.info(job2queue.JOBQUEUEMESSAGE2TXT[terug]) else: # run bots-engine direct.; reports back if bots-engien is started succesful. **not reported: problems with running. botsglobal.logger.info(_('Run bots-engine with parameters: "%(parameters)s"'), {'parameters': unicode(lijst)}) #first check if another instance of bots-engine is running/if port is free try: engine_socket = botslib.check_if_other_engine_is_running() except socket.error: notification = _( 'Trying to run "bots-engine", but another instance of "bots-engine" is running. Please try again later.') messages.add_message(request, messages.INFO, notification) botsglobal.logger.info(notification) return django.shortcuts.redirect('/home') else: engine_socket.close() # and close the socket #run engine try: terug = subprocess.Popen(lijst).pid except Exception as msg: notification = _('Errors while trying to run bots-engine: "%s".') % msg messages.add_message(request, messages.INFO, notification) botsglobal.logger.info(notification) else: messages.add_message(request, messages.INFO, _('Bots-engine is started.')) return django.shortcuts.redirect('/home')
def start(): #NOTE: bots directory should always be on PYTHONPATH - otherwise it will not start. #***command line arguments************************** usage = ''' This is "%(name)s" version %(version)s, part of Bots open source edi translator (http://bots.sourceforge.net). A utility to generate the index file of a plugin; this can be seen as a database dump of the configuration. This is eg useful for version control. Usage: %(name)s -c<directory> Options: -c<directory> directory for configuration files (default: config). ''' % { 'name': os.path.basename(sys.argv[0]), 'version': botsglobal.version } configdir = 'config' for arg in sys.argv[1:]: if arg.startswith('-c'): configdir = arg[2:] if not configdir: print 'Error: configuration directory indicated, but no directory name.' sys.exit(1) else: print usage sys.exit(0) #***end handling command line arguments************************** botsinit.generalinit( configdir) #find locating of bots, configfiles, init paths etc. if not botsglobal.ini.getboolean('jobqueue', 'enabled', False): print 'Error: bots jobqueue cannot start; not enabled in %s/bots.ini' % configdir sys.exit(1) process_name = 'dirmonitor' logger = botsinit.initserverlogging(process_name) logger.log(25, u'Bots %(process_name)s started.', {'process_name': process_name}) logger.log( 25, u'Bots %(process_name)s configdir: "%(configdir)s".', { 'process_name': process_name, 'configdir': botsglobal.ini.get('directories', 'config') }) botsenginepath = os.path.join(os.path.dirname(os.path.abspath( sys.argv[0])), 'bots-engine.py') #get path to bots-engine cond = threading.Condition() tasks = set() dir_watch_data = [] for section in botsglobal.ini.sections(): if section.startswith('dirmonitor') and section[len('dirmonitor'):]: dir_watch_data.append({}) dir_watch_data[-1]['path'] = botsglobal.ini.get(section, 'path') dir_watch_data[-1]['rec'] = botsglobal.ini.getboolean( section, 'recursive', False) dir_watch_data[-1]['filemask'] = botsglobal.ini.get( section, 'filemask', '*') dir_watch_data[-1]['route'] = botsglobal.ini.get( section, 'route', '') if not dir_watch_data: logger.error(u'Nothing to watch!') sys.exit(0) if os.name == 'nt': #for windows: start a thread per directory watcher for dir_watch in dir_watch_data: dir_watch_thread = threading.Thread(target=windows_event_handler, args=(logger, dir_watch, cond, tasks)) dir_watch_thread.daemon = True #do not wait for thread when exiting dir_watch_thread.start() else: #for linux: one watch-thread, but multiple watches. dir_watch_thread = threading.Thread(target=linux_event_handler, args=(logger, dir_watch_data, cond, tasks)) dir_watch_thread.daemon = True #do not wait for thread when exiting dir_watch_thread.start() # this main thread get the results from the watch-thread(s). logger.info(u'Bots %(process_name)s started.', {'process_name': process_name}) active_receiving = False timeout = 2.0 cond.acquire() while True: #this functions as a buffer: all events go into set tasks. #the tasks are fired to jobqueue after TIMOUT sec. #this is to avoid firing to many tasks to jobqueue; events typically come in bursts. #is value of timeout is larger, reaction times are slower...but less tasks are fired to jobqueue. #in itself this is not a problem, as jobqueue will alos discard duplicate jobs. #2 sec seems to e a good value: reasonable quick, not to nervous. cond.wait( timeout=timeout) #get back when results, or after timeout sec if tasks: if not active_receiving: #first request (after tasks have been fired, or startup of dirmonitor) active_receiving = True last_time = time.time() else: #active receiving events current_time = time.time() if current_time - last_time >= timeout: #cond.wait returned probably because of a timeout try: for task in tasks: logger.info( u'Send to queue "%(path)s %(config)s %(task)s".', { 'path': botsenginepath, 'config': '-c' + configdir, 'task': task }) job2queue.send_job_to_jobqueue([ sys.executable, botsenginepath, '-c' + configdir, task ]) except Exception as msg: logger.info(u'Error in running task: "%(msg)s".', {'msg': msg}) tasks.clear() active_receiving = False else: #cond.wait returned probably because of a timeout logger.debug(u'time difference to small.') last_time = current_time cond.release() sys.exit(0)
def start(): # NOTE: bots directory should always be on PYTHONPATH - otherwise it will not start. # ***command line arguments************************** usage = """ This is "%(name)s" version %(version)s, part of Bots open source edi translator (http://bots.sourceforge.net). A utility to generate the index file of a plugin; this can be seen as a database dump of the configuration. This is eg useful for version control. Usage: %(name)s -c<directory> Options: -c<directory> directory for configuration files (default: config). """ % { "name": os.path.basename(sys.argv[0]), "version": botsglobal.version, } configdir = "config" for arg in sys.argv[1:]: if arg.startswith("-c"): configdir = arg[2:] if not configdir: print "Error: configuration directory indicated, but no directory name." sys.exit(1) else: print usage sys.exit(0) # ***end handling command line arguments************************** botsinit.generalinit(configdir) # find locating of bots, configfiles, init paths etc. if not botsglobal.ini.getboolean("jobqueue", "enabled", False): print "Error: bots jobqueue cannot start; not enabled in %s/bots.ini" % configdir sys.exit(1) process_name = "dirmonitor" logger = botsinit.initserverlogging(process_name) logger.log(25, u"Bots %(process_name)s started.", {"process_name": process_name}) logger.log( 25, u'Bots %(process_name)s configdir: "%(configdir)s".', {"process_name": process_name, "configdir": botsglobal.ini.get("directories", "config")}, ) botsenginepath = os.path.join( os.path.dirname(os.path.abspath(sys.argv[0])), "bots-engine.py" ) # get path to bots-engine cond = threading.Condition() tasks = set() dir_watch_data = [] for section in botsglobal.ini.sections(): if section.startswith("dirmonitor") and section[len("dirmonitor") :]: dir_watch_data.append({}) dir_watch_data[-1]["path"] = botsglobal.ini.get(section, "path") dir_watch_data[-1]["rec"] = botsglobal.ini.getboolean(section, "recursive", False) dir_watch_data[-1]["filemask"] = botsglobal.ini.get(section, "filemask", "*") dir_watch_data[-1]["route"] = botsglobal.ini.get(section, "route", "") if not dir_watch_data: logger.error(u"Nothing to watch!") sys.exit(0) if os.name == "nt": # for windows: start a thread per directory watcher for dir_watch in dir_watch_data: dir_watch_thread = threading.Thread(target=windows_event_handler, args=(logger, dir_watch, cond, tasks)) dir_watch_thread.daemon = True # do not wait for thread when exiting dir_watch_thread.start() else: # for linux: one watch-thread, but multiple watches. dir_watch_thread = threading.Thread(target=linux_event_handler, args=(logger, dir_watch_data, cond, tasks)) dir_watch_thread.daemon = True # do not wait for thread when exiting dir_watch_thread.start() # this main thread get the results from the watch-thread(s). logger.info(u"Bots %(process_name)s started.", {"process_name": process_name}) active_receiving = False timeout = 2.0 cond.acquire() while True: # this functions as a buffer: all events go into set tasks. # the tasks are fired to jobqueue after TIMOUT sec. # this is to avoid firing to many tasks to jobqueue; events typically come in bursts. # is value of timeout is larger, reaction times are slower...but less tasks are fired to jobqueue. # in itself this is not a problem, as jobqueue will alos discard duplicate jobs. # 2 sec seems to e a good value: reasonable quick, not to nervous. cond.wait(timeout=timeout) # get back when results, or after timeout sec if tasks: if not active_receiving: # first request (after tasks have been fired, or startup of dirmonitor) active_receiving = True last_time = time.time() else: # active receiving events current_time = time.time() if current_time - last_time >= timeout: # cond.wait returned probably because of a timeout try: for task in tasks: logger.info( u'Send to queue "%(path)s %(config)s %(task)s".', {"path": botsenginepath, "config": "-c" + configdir, "task": task}, ) job2queue.send_job_to_jobqueue([sys.executable, botsenginepath, "-c" + configdir, task]) except Exception as msg: logger.info(u'Error in running task: "%(msg)s".', {"msg": msg}) tasks.clear() active_receiving = False else: # cond.wait returned probably because of a timeout logger.debug(u"time difference to small.") last_time = current_time cond.release() sys.exit(0)