• 结合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
    ~                     
  • 相关阅读:
    信号发生器的设计(期末课程设计)
    小程序异步获取openid解决方案
    微信小程序生成二维码之传参(接收的参数乱码该咋解决)
    小程序tabBar,点击进入tabBar刷新tabBar页面
    微信小程序函数执行顺序问题
    RESTful规范与常用状态码
    dedeCMS二次开发api简单接口代码
    DEDECMS怎么获取当前栏目及所有子栏目的文章数量
    DedeCMSV57数据库结构文档(数据字典)
    【JQuery插件】把网页或某div或table表格内容转为图片并下载
  • 原文地址:https://www.cnblogs.com/laodageblog/p/4288948.html
Copyright © 2020-2023  润新知