def kill(signal=9): """ Kill zombie (unregistered) modules """ zombies = [] pids = set(m[1] for m in running_modules()) for line in shell('ps -e --no-headers -o pid,command').split('\n'): try: pid, name = re.match(r'\s*(\d+)\s+(.+)$', line).groups() except AttributeError: continue mob = re.search(r'app/(.*?)/main', name) if mob and pid not in pids: zombies.append((mob.group(1), pid)) if not zombies: puts(fore.green('No zombies detected')) else: puts(fore.green('Killing %d zombie modules' % len(zombies))) for name, pid in zombies: puts(' * Killing %-10s (pid: %s) ...' % (fore.cyan(name), fore.red(pid))) try: shell('kill -s %s %s' % (signal, pid), exception=RuntimeError) except RuntimeError as e: puts(fore.magenta(' Error: ') + e.args[0])
def stop(module='*'): """ Stop [module] """ for module, pid, pidfile in running_modules(module): shell('kill %s' % pid) shell('rm -f %s' % pidfile) shell('rm -f tmp/sockets/*/%s.sock' % module) puts(fore.cyan("%-10s" % module) + "(pid: %s) stopped" % fore.red(pid))
def decorator(base): info = ': ' + arg if type(arg) is str else '' header = fore.green('** ' + fore.cyan(base.__name__) + info) def func(*args, **kwargs): sys.stdout.indent_level += 1 puts(header) base(*args, **kwargs) sys.stdout.indent_level -= 1 params = inspect.formatargspec(*inspect.getargspec(base))[1:-1] specformat = fore.cyan('%s') + ' ' + fore.white('%s') func._task = True func._spec = specformat % (base.__name__, params) func._desc = re.sub('\s+', ' ', inspect.getdoc(base) or '') return func
def println(line): # blank line if re.match(r'\s*$', line): return puts(line) # logfile header mobj = re.match(r'==> (.*) <==$', line) if mobj: return puts(style.bright(fore.black('==> ') + fore.white(mobj.group(1)) + fore.black(' <=='))) # exception line if re.match(EXCEPTION_PREFIX, line): return puts(fore.red(line)) # standard log line basic = r'(.*?\s+)' date = basic*3 time = basic type = basic file = '(\[.*?\s+)\])' mobj = re.match(basic*6 + r'(.*)', line) if not mobj: # non-conventional line return puts(line) else: groups = list(mobj.groups()) groups.insert(0, str(fore.cyan)) groups.insert(4, str(fore.blue)) groups.insert(6, str(style.bright)) groups.insert(8, str(style.reset_all)) groups.insert(9, str(fore.cyan)) groups.insert(11, str(style.reset_all)) for idx, string in enumerate(groups): string = re.sub(r'(STATUS)', fore.white(r'\1'), string) string = re.sub(r'(DEBUG)', fore.white(r'\1'), string) string = re.sub(r'(INFO)', fore.green(r'\1'), string) string = re.sub(r'(WARNING)', fore.yellow(r'\1'), string) string = re.sub(r'(ERROR)', fore.red(r'\1'), string) string = re.sub(r'(EXCEPT)', fore.red(r'\1'), string) groups[idx] = string groups[-1] = re.sub(r'\[', fore.cyan(r'['), groups[-1]) groups[-1] = re.sub(r'\]', fore.cyan(r']'), groups[-1]) groups[-1] = re.sub(r'~>', fore.blue(r'~>'), groups[-1]) groups[-1] = re.sub(r'<~', fore.yellow(r'<~'), groups[-1]) groups[-1] = re.sub(r'\(', fore.cyan(r'('), groups[-1]) groups[-1] = re.sub(r'\)', fore.cyan(r')'), groups[-1]) groups[-1] = re.sub(r"'", fore.cyan(r"'"), groups[-1]) groups[-1] = re.sub(r'"', fore.cyan(r'"'), groups[-1]) return puts(''.join(groups))
def start(module='*', env='development'): """ Start [module] """ os.environ['APP_ENV'] = env for module, path in all_modules(module): try: cmd = "PYTHONPATH=. %s %s &>> logs/%s.log" % (python(), path, module) shell("echo '%s' | at now" % cmd, exception=PyscaleError) except PyscaleError as e: msg = str(e).strip('\n') msg = ' '.join(msg.split('\n')) puts(fore.cyan("%-10s" % module) + "(%s) scheduled" % msg)
def status(): """ View running modules """ for module, pid, pidfile in running_modules(): if not osp.exists("/proc/%s" % pid): puts(fore.red("%s (pid %s) crashed" % (module, pid))) pids = map(operator.itemgetter(1), running_modules()) if pids: pscomm = "ps -p %s -o pid,user,command:50,pcpu,pmem,vsz,nice,start,time" % ','.join(pids) psinfo = shell(pscomm).split('\n') if len(psinfo) > 1 and psinfo[1]: puts(fore.green(psinfo[0])) for ps in psinfo[1:]: color = lambda mobj: re.sub(mobj.group(1), fore.cyan(mobj.group(1)), mobj.group(0)) puts(re.sub('app/(.*?)/main', color, ps))
def status(): """ View running modules """ for module, pid, pidfile in running_modules(): if not osp.exists("/proc/%s" % pid): puts(fore.red("%s (pid %s) crashed" % (module, pid))) pids = map(operator.itemgetter(1), running_modules()) if pids: pscomm = "ps -p %s -o pid,user,command:50,pcpu,pmem,vsz,nice,start,time" % ','.join( pids) psinfo = shell(pscomm).split('\n') if len(psinfo) > 1 and psinfo[1]: puts(fore.green(psinfo[0])) for ps in psinfo[1:]: color = lambda mobj: re.sub(mobj.group( 1), fore.cyan(mobj.group(1)), mobj.group(0)) puts(re.sub('app/(.*?)/main', color, ps))
def println(line): # blank line if re.match(r'\s*$', line): return puts(line) # logfile header mobj = re.match(r'==> (.*) <==$', line) if mobj: return puts( style.bright( fore.black('==> ') + fore.white(mobj.group(1)) + fore.black(' <=='))) # exception line if re.match(EXCEPTION_PREFIX, line): return puts(fore.red(line)) # standard log line basic = r'(.*?\s+)' date = basic * 3 time = basic type = basic file = '(\[.*?\s+)\])' mobj = re.match(basic * 6 + r'(.*)', line) if not mobj: # non-conventional line return puts(line) else: groups = list(mobj.groups()) groups.insert(0, str(fore.cyan)) groups.insert(4, str(fore.blue)) groups.insert(6, str(style.bright)) groups.insert(8, str(style.reset_all)) groups.insert(9, str(fore.cyan)) groups.insert(11, str(style.reset_all)) for idx, string in enumerate(groups): string = re.sub(r'(STATUS)', fore.white(r'\1'), string) string = re.sub(r'(DEBUG)', fore.white(r'\1'), string) string = re.sub(r'(INFO)', fore.green(r'\1'), string) string = re.sub(r'(WARNING)', fore.yellow(r'\1'), string) string = re.sub(r'(ERROR)', fore.red(r'\1'), string) string = re.sub(r'(EXCEPT)', fore.red(r'\1'), string) groups[idx] = string groups[-1] = re.sub(r'\[', fore.cyan(r'['), groups[-1]) groups[-1] = re.sub(r'\]', fore.cyan(r']'), groups[-1]) groups[-1] = re.sub(r'~>', fore.blue(r'~>'), groups[-1]) groups[-1] = re.sub(r'<~', fore.yellow(r'<~'), groups[-1]) groups[-1] = re.sub(r'\(', fore.cyan(r'('), groups[-1]) groups[-1] = re.sub(r'\)', fore.cyan(r')'), groups[-1]) groups[-1] = re.sub(r"'", fore.cyan(r"'"), groups[-1]) groups[-1] = re.sub(r'"', fore.cyan(r'"'), groups[-1]) return puts(''.join(groups))