zoukankan      html  css  js  c++  java
  • 结合rpyc使用python实现动态升级的方法

    动态升级,就是程序不退出的情况下,将其代码更新的策略。假设集群含有多个机器,然后每个机器部署一套程序,当升级的时候就要去所有的上面部署一把。

    (1)有个包装程序专门负责接口并检查是否需要更新,当需要更新的时候,下载下来。

    (2)动态引用,将实质程序放到独立文件和文件夹下面,通过动态引用,调用的时候reload;

    客户端代码:

    import rpyc
    import sys
    def update(remoteHost):
            c=rpyc.connect(remoteHost,12233)
            content="".join(file("Test.py","r").readlines())
            print c.root.update(content)
            c.close()
    
    
    ipList=["10.101.92.211","10.101.90.203","10.101.91.239"]
    for ip in ipList:
            update(ip)

    服务器端:

    import time,subprocess
    from rpyc import Service
    from rpyc.utils.server import ThreadedServer
    import sys
    g_module=None
    
    class TimeService(Service):
        def exposed_sum(self,a,b):
            return a+b
        def exposed_show(self,cmd):
            #cmd='''grep "FAIL"  /home/admin/alisatasknode/taskinfo/*/*/*/*/*/*/*.log   '''
            return self.do_cmd(cmd)[1]
        def do_cmd(self,cmd):
            p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            out, err = p.communicate()
            return p.returncode, out, err
        def exposed_process(self,starttime):
            self.modulePath="/home/admin/rpyc-3.3.0/server.py"
            global g_module
            if g_module is None:
                print("none")
                g_module=__import__('Test')
            else:
                print("reload")
                g_module=reload(g_module)
            Test=getattr(g_module,"Test")
            t=Test()
            return t.getFailure(starttime)
        def exposed_update(self,content):
            outfile=open("Test.py","w")
            outfile.write(content)
            return "OK"
    
    s=ThreadedServer(TimeService,port=12233,auto_register=False)
    s.start()

     在Test.py里面实现真正的逻辑

    import subprocess
    import os,time
    
    class Test():
        def do_cmd(self,cmd):
            p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            out, err = p.communicate()
            return p.returncode, out, err
    
        def getFailure(self,starttime):
            key="Table not found"
            keyfile= key.replace(" ","_")+".txt"
            timeArray = time.strptime(starttime,"%Y:%m:%d:%H:%M:%S")
            timeStamp = int(time.mktime(timeArray))
            print("timeStamp:%s" % (timeStamp))
            cmd="grep '%s' /home/admin/alisatasknode/taskinfo/20141222/phoenix/20141221/*/*/*/*.log" % (key)
            print("cmd:%s" % (cmd))
            r=self.do_cmd(cmd)
            print(r[0])
            infos=""
            if r[0] == 0:
                filelist=r[1].split("
    ")
                for info in filelist:
                    filename=info.split(":")[0]
                    #print("filename:%s" % (filename))
                    if filename.find("taskinfo") < 0:
                        continue
                    filestamp=os.path.getmtime(filename)
                    if filestamp >= timeStamp:
                        print("timeStamp:%s	filestamp:%s
    info:%s" % (timeStamp,filestamp,info))
                        if info.find(key):
                            infos=infos+"
    "+info
            return infos
    ~                     
  • 相关阅读:
    Java正则表达式的语法与示例
    python调用另一个文件中的代码,pycharm环境下:同文件夹下文件(.py)之间的调用,出现红线问题
    开发者应当了解的WebKit知识
    strings和nm命令
    CSS元素选择器 element selector(type selector)
    如何查看本机正在监听的端口
    Error 0x80070020 when you try to start a Web site in IIS 7.0
    Windows cannot find ". Make sure you typed the name correctly, and then try again
    Gitblit从一个服务器,迁移到另外一个服务器
    Firefox访问https的网站,一直提示不安全
  • 原文地址:https://www.cnblogs.com/laodageblog/p/4288948.html
Copyright © 2011-2022 走看看