/usr/lib/python2.7/dist-packages/gozerbot/runner.py is in gozerbot 0.99.1-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | # jsb/runner.py
#
#
""" threads management to run jobs. """
## jsb imports
from gozerbot.threads.thr import getname, start_new_thread, start_bot_command
from gozerbot.utils.generic import handle_exception
from gozerbot.utils.trace import callstack
from gozerbot.threads.threadloop import RunnerLoop
from gozerbot.utils.log import rlog
## basic imports
import Queue
import time
import thread
import random
import logging
import sys
## Runner class
class Runner(RunnerLoop):
"""
a runner is a thread with a queue on which jobs can be pushed.
jobs scheduled should not take too long since only one job can
be executed in a Runner at the same time.
"""
def __init__(self, name="runner", doready=True):
RunnerLoop.__init__(self, name)
self.working = False
self.starttime = time.time()
self.elapsed = self.starttime
self.finished = time.time()
self.doready = doready
def handle(self, descr, func, *args, **kwargs):
""" schedule a job. """
self.working = True
name = getname(str(func))
try:
#rlockmanager.acquire(getname(str(func)))
name = getname(str(func))
self.name = name
rlog(10, "runner", 'running %s: %s' % (descr, name))
self.starttime = time.time()
func(*args, **kwargs)
self.finished = time.time()
self.elapsed = self.finished - self.starttime
#if self.elapsed > 3:
#logging.debug('ALERT %s %s job taking too long: %s seconds' % (descr, str(func), self.elapsed))
except Exception, ex: handle_exception()
#finally: rlockmanager.release()
self.working = False
## BotEventRunner class
class BotEventRunner(Runner):
def handle(self, descr, func, bot, ievent, *args, **kwargs):
""" schedule a bot command. """
try:
self.starttime = time.time()
#lockmanager.acquire(getname(str(func)))
name = getname(str(func))
self.name = name
self.working = True
rlog(10, "runner", "now running %s" % name)
func(bot, ievent, *args, **kwargs)
for queue in ievent.queues:
queue.put_nowait(None)
self.finished = time.time()
self.elapsed = self.finished - self.starttime
#if self.elapsed > 3:
# logging.info('ALERT %s %s job taking too long: %s seconds' % (descr, str(func), self.elapsed))
#if ievent.iscommand: ievent.ready()
#if not ievent.type == "OUTPUT" and not ievent.dontclose: ievent.ready()
#time.sleep(0.001)
except Exception, ex:
handle_exception(ievent)
#finally: lockmanager.release(getname(str(func)))
self.working = False
self.name = "finished"
## Runners class
class Runners(object):
""" runners is a collection of runner objects. """
def __init__(self, max=100, runnertype=Runner, doready=True):
self.max = max
self.runners = []
self.runnertype = runnertype
self.doready = doready
def runnersizes(self):
""" return sizes of runner objects. """
result = []
for runner in self.runners: result.append("%s - %s" % (runner.queue.qsize(), runner.name))
return result
def stop(self):
""" stop runners. """
for runner in self.runners: runner.stop()
def start(self):
""" overload this if needed. """
pass
def put(self, *data):
""" put a job on a free runner. """
#logging.debug("size is %s" % len(self.runners))
for runner in self.runners:
if not runner.queue.qsize():
runner.put(*data)
return
runner = self.makenew()
runner.put(*data)
def running(self):
""" return list of running jobs. """
result = []
for runner in self.runners:
if runner.queue.qsize(): result.append(runner.nowrunning)
return result
def makenew(self):
""" create a new runner. """
runner = None
for i in self.runners:
if not i.queue.qsize(): return i
if len(self.runners) < self.max:
runner = self.runnertype(self.doready)
runner.start()
self.runners.append(runner)
else: runner = random.choice(self.runners)
return runner
def cleanup(self):
""" clean up idle runners. """
#if not len(self.runners): logging.debug("nothing to clean")
for index in range(len(self.runners)-1, -1, -1):
runner = self.runners[index]
#logging.debug("cleanup %s" % runner.name)
if not runner.queue.qsize():
try: runner.stop() ; del self.runners[index]
except IndexError: pass
except: handle_exception()
#else: logging.info("now running: %s" % runner.nowrunning)
## global runners
cmndrunners = defaultrunner = longrunner = Runners(50, BotEventRunner)
cbrunners = Runners(100, BotEventRunner, doready=False)
waitrunners = Runners(10, Runner)
## runners_start
def runners_start():
pass
def runners_stop():
pass
|