1. 程式人生 > >單一工作流的執行➕回滾框架初步想法(基於事件驅動模式)

單一工作流的執行➕回滾框架初步想法(基於事件驅動模式)

思路來源: 事件驅動框架。

 

Part 1. 定義一個框架 event_flow.py  (需要再改動)

 1 #! /usr/bin/env python
 2 
 3 
 4 deploy_phase_list = []
 5 
 6 def deploy():
 7     for phase in deploy_phase_list:
 8         phase_obj = phase()
 9         phase_obj.deploy()
10 
11 
12 def rollback():
13     for phase in
reversed(deploy_phase_list): 14 phase_obj = phase() 15 phase_obj.rollback() 16 17 18 class BasePhase(object): 19 def deploy(): 20 raise Exception("deploy() must be overwrite") 21 22 def rollback(): 23 raise Exception("rollback() must be overwrite")
event base frame

// 此處可以考慮➕裝飾器,當deploy  順利完成,才把該類append 到rollback list 

 

Part 2. 使用該框架

 

#! /usr/bin/env python

import event_flow

class MyFirstStep(event_flow.BasePhase):
    def deploy():
        print("Do First Step to deploy")

    def rollback():
        print("Rollback First Step")



class MySecondStep(event_flow.BasePhase):
    def deploy():
        print("Do Second Step to deploy")

    def rollback():
        print("Nothing to rollback")
        pass


class MyThirdStep(event_flow.BasePhase):
    def deploy():
        print("Do Third Step to deploy")

    def rollback():
        print("Rollback third Step")


if __name__ == "__main__":
    event_flow.deploy_phase_list.append(MyFirstStep, MySecondStep, MyThirdStep)
    try:
        event_flow.deploy()
    exception Exception as e:
        event_flow.rollback()

  

 

但是該方法給可以完成單一工作流的部署以及回滾。後續的這種單一工作流,可以參考這種模式進行開發。

其中各個步驟的返回等資料互動,需要藉助第三方(例如資料庫等)