def nsDaemonStop(ns, *args, **kw): nsconsole(ns, "Stop requested") f = lf(ns) V = f("V") if V("/etc/daemonize") is True: nsconsole(ns, "No can do: +daemonize is requested.") return f("/usr/local/bin/_stop")(*args, **kw)
def nsEnvRemovePid(ns): daemonFile = "{}.daemon".format(nsGet(ns, "/sys/env/pidFile")) if os.path.exists(daemonFile) and os.path.isfile(daemonFile): return f = lf(ns) V = f("V") if nsGet(ns, "/etc/daemonize") is True: return nsconsole(ns, "Removing PID file") if os.path.exists(nsGet(ns, "/sys/env/pidFile")) and os.path.isfile( nsGet(ns, "/sys/env/pidFile")): os.remove(nsGet(ns, "/sys/env/pidFile"))
def nsMetaInit(ns, *args, **kw): if len(nsDir(ns, "/etc/meta")) == 0: return nsconsole(ns, "Metaprogramming start") for m in nsDir(ns, "/etc/meta"): nsInfo(ns, "Bringing metamodel {}".format(m)) try: mm = metamodel_from_str(nsGet(ns, "/etc/meta/{}".format(m)), memoization=True) except (IndexError, textx.exceptions.TextXSyntaxError): nsError(ns, "Metamodel {} failed".format(m)) continue nsSet(ns, "/meta/{}".format(m), mm) nsSet(ns, "/metaprocessor/{}".format(m), partial(nsMeta, ns, "/meta/{}".format(m)))
def nsDaemonLoop(ns, *args, **kw): f = lf(ns) V = f("V") if V("/etc/daemonize") is True: nsConsole(ns, "[/bin/loop] No can do: +daemonize is requested.") return pid = nsEnvLoadPid(ns) if pid is not None: me = psutil.Process(pid) if me.is_running() is True: nsconsole(ns, "Process already running with PID %d" % pid) return f("/bin/loop")(*args, **kw)
def nsDaemonStart(ns, *args, **kw): f = lf(ns) V = f("V") if V("/etc/daemonize") is False: nsConsole(ns, "No can do: +daemonize is not requested.") return pid = nsDaemonLoadPid(ns) if pid is not None: nsconsole(ns, "Application already daemonized. No can do.") return nsConsole(ns, "Starting core.NS($etc.name) daemon") nsConsole(ns, "APP name: $etc.name") nsConsole(ns, "HOST name: $etc.hostname") nsConsole(ns, "PID file: $sys.env.pidFile") f("/usr/local/bin/_start")(*args, **kw)
def nsCmd(ns, *args, **kw): argv = nsGet(ns, "/etc/argv", []) root = nsGet(ns, "/config/cmd.path") kw.update(nsGet(ns, "/config/cmd.kw")) out = None is_cmd_run = nsGet(ns, "/config/cmd.run") if is_cmd_run is True: return ns for k in argv: _args = tuple([ out, ] + list(args)) try: nsconsole(ns, "CMD executed: {}/{}".format(root, k)) out = f(ns, "{}/{}".format(root, k))(*_args, **kw) nsSet(ns, "/config/cmd.run", True) except TypeError: #except KeyboardInterrupt: nsconsole(ns, "CMD not found: {}/{}".format(root, k)) nsGlobalError(ns, "Command {}/{} not found".format(root, k)) continue return ns
def nsZRpcInit(ns, *args, **kw): if len(nsGet(ns, "/etc/zrpc")) == 0: return nsconsole(ns, "ZRPC start") for r in nsGet(ns, "/etc/zrpc"): nsconsole(ns, "ZRPC(user) service {} on {}".format(r, nsGet(ns, "/etc/zrpc")[r])) if listenHost is None: nsconsole(ns, "Failed to parse {}".format(nsGet(ns, "/etc/zrpc")[r])) continue if nsRPCBringupServer(ns, "/usr/local/rpc/user/{}".format(r), nsGet(ns, "/etc/zrpc")[r], nsGet(ns, "/config/defaultZRPCMax")) is not True: nsError(ns, "Unable to bring up {}:{} ZRPC server".format(listenHost, listenPort))
def nsInternalServerStart(ns, *args, **kw): if nsGet(ns, "/etc/flags/internalServer", False) is False: nsconsole(ns, "Internal RPC server disabled") return else: nsconsole(ns, "Internal RPC server enabled") nsconsole(ns, "INTERNAL RPC start") if nsRPCBringupServer(ns, "/usr/local/rpc/internal", nsGet(ns, "/config/defaultInternalListen"), nsGet(ns, "/config/defaultInternalPort"), nsGet(ns, "/config/defaultInternalMax")) is not True: nsError(ns, "Unable to bring up internal RPC server") nsSchedulerIntervalJob(ns, 15, "ShutdownWatcher", nsInternalServerShutdownWatcher)
def nsDaemonStatus(ns, *args, **kw): f = lf(ns) V = f("V") pid = nsEnvLoadPid(ns) if pid is None: nsconsole(ns, "No process was found") nsDaemonCleanUp(ns) return me = psutil.Process(pid) if me.is_running() is True: nsconsole(ns, "Process is running with PID %d" % pid) else: nsconsole(ns, "Process is not running") nsDaemonCleanUp(ns)
def nsZMQStop(ns, *args, **kw): if len(nsGet(ns, "/etc/zmq")) == 0: return nsconsole(ns, "ZMQ stop")
def nsZRpcStop(ns, *args, **kw): if len(nsGet(ns, "/etc/rpc")) == 0: return nsconsole(ns, "ZRPC stop")
def nsInternalServerStop(ns, *args, **kw): if nsGet(ns, "/etc/flags/internalServer", False) is False: return nsconsole(ns, "INTERNAL RPC stop")
def nsInternalServerShutdownWatcher(ns): if nsGet(ns, "/etc/shutdownRequested") is True: nsconsole(ns, "Shutdown is executing") nsKillAll(ns)
def nsTasksInit(ns, *args, **kw): nsconsole(ns, "TASKS start")
def nsDaemonStopInt(ns, *args, **kw): if os.path.exists(nsGet(ns, "/sys/env/pidFile")) and os.path.isfile(nsGet(ns, "/sys/env/pidFile")): nsconsole(ns, "$sys.env.pidFile exists. Checking.") pid = nsEnvLoadPid(ns) if pid is None: nsconsole(ns, "PID in $sys.env.pidFile is not parseable") return if psutil.pid_exists(pid) is not True: nsconsole(ns, "PID[%d] in $sys.env.pidFile not exists in system"%pid) return me = psutil.Process(pid) if me.is_running(): nsconsole(ns, "Termnating daemon PID=%d"%pid) me.terminate() c = 0 for i in range(5): nsconsole(ns, "Wait %s"%("."*i)) time.sleep(1) if me.is_running() is False: break if me.is_running() is True: nsconsole(ns, "Killing daemon PID=%d"%pid) me.kill() nsDaemonCleanUp(ns) else: nsconsole(ns, "$sys.env.pidFile do not exists. Is daemon running ?")
def send_event(ns): f = lf(ns) nsconsole( ns, "SEND: {}".format(f("/tasks/gate/test/in")(random.randint(1, 100))))
def nsTasksStop(ns, *args, **kw): nsconsole(ns, "TASKS stop")
def nsMetaStop(ns, *args, **kw): if len(nsDir(ns, "/etc/meta")) == 0: return nsconsole(ns, "Metaprogrammming stop")
def recv_event(ns): nsconsole(ns, "RECV: {}".format(f("/tasks/gate/test/empty")()))
def nsZMQInit(ns, *args, **kw): if len(nsGet(ns, "/etc/zmq")) == 0: return nsconsole(ns, "ZMQ start")