On the programming method of state machine workflow

Source: Internet
Author: User

Had not yet thought how to write, first had a strange name. Well, this article is purely weeding, and only one approximate solution. However, we probably have to solve the two concepts first: one is the state machine, and the other is the workflow. What is a state machine? Presumably, I have a bunch of states here, and I have a series of states when I'm doing a job; I want to move from one state to another. The simplest chestnut: a light bulb, with two states of "on" and "off". I am the operation of this bulb is to flip the switch, the light "on" when I press the lights off, to the "closed" state; if I press the light again, it's "on". The state transition graph is used to indicate that this is probably the case: What is a workflow? The so-called workflow is that there are certain steps and sequences that need to be done sequentially. Suppose that in the work, we have a research topic, need the company to give a certain amount of support, but the company can not guarantee the feasibility of the research, whether it is in line with the company's strategic objectives, whether it is legal, whether in the company's financial budget and so on a series of issues, need a, B, C, D four person approval, A approval after passing to the b,b by hand to c,c through to D,d pass to calculate completely through. If one of them does not pass, it will have to start over again. Then the state machine workflow is better understood, that is, the state machine and workflow together. Also with the above approval of the chestnut, we can draw such a state transfer diagram: OK, then the general idea how to do it?
def approve ():     if A by:         if B by:             if C by:                 if d by:                     return through     return does not pass

It doesn't work if you think about it with your heel. If a approval is passed, B is not in line, can not be passed immediately? Open a thread to block out? What happens if the server suddenly goes down if C is approved? The previous approvals are all going to start again? Our approval process is already very simple and clear, if the state machine is more complex ... Sorry, the code is not so stacked. So, my idea is this: since the most important thing is state transfer, then we might as well save each state of the workflow as a step. We can add a separate field in the database that represents the state. For example, when the status is 1, indicating "A approval required", the status of 2, indicating "B approval required" and so on. The function that represents approval is, of course, simple:
def approve ():     if agree:         + = 1    else:        =    0return status

The next job may be to see which type of workflow is specific. If it is the approval of this, of course, it is very simple, need B approval, we put the database status of 2 of those data out of it. Some types are not shown by the front end, but rather a series of actions performed by the backend, which can be more complicated. If the time requirement is not particularly high, it can be handled with a timed task. For example, we replace ABCD four approvers with ABCD four links, then, we choose the database of the unfinished task at a certain time, the status of 1 of the task of a link, the status of 2 of the task of the B link, and so on. Scheduled tasks, if very simple, you can use the schedule library, a more complex task is recommended celery--because celery will assign a separate task queue and thread to the task, it is much more convenient to operate than schedule. and need to set the approximate time, so as not too many tasks, build up. Schedule I introduced in the previous blog post, celery relatively complex, I will now only use part of it. Online there is a more complete celery use of the article, the official website of the introduction is more comprehensive, need to use what to check the good.

On the programming method of state machine workflow

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.