Previously, we analyzed why a simple and seemingly reasonable SPD workflow loop may jump out of the loop during execution. Our goal is to make the workflow more stable.
When a workflow cannot work normally, you may need help from other workflows. One person cannot do the work, and everyone can do it together. We need another workflow instance to "monitor and remind" the first workflow ". Another workflow can be placed on the same list (because they are different templates, two different instances can be generated at the same time) or on another secondary list. It is relatively simple to design a list on the same list, but because two different workflow instances are running at the same time, you must be extremely careful in the analysis and workflow design. If you put them on multiple lists, only one instance is run on each list. It is logically easier to analyze by associating a field. However, it is very troublesome to migrate and deploy the instance (see this article by Lao Zhao ). To perform the test, I first heap all the work on a list.
The idea is that the two templates of the workflow are divided into two roles: the worker and the supervisor. The worker is responsible for the work (perform specific operations) and the supervisor is responsible for reminding the worker. After the worker completes the operation, tell the supervisor "I have finished !", Then the supervisor said, "Oh? Then you can take a rest first. "Then, remind the worker to say," Hey! It's time to work !". In fact, these two workflow instances trigger each other and use a flag to determine whether the current operation should be performed by the worker or the monitor. Of course, in order to avoid the previous saying that "trigger occurs before exit", both sides should have a confirmation period, and they will not start running until the other side ends.
The two workflows are designed as follows:
First, workers:
Then the supervisor:
Mark indicates which workflow should be run and the initial value is set to true. The execution process is like this: both sides are triggered at the beginning, but because Mark is true, the worker starts to quit, and the supervisor exits.> the worker takes a 5-minute break (lazy) -> set the tag to trigger the invigilator operation-> exit after work-> after the invigilator is triggered (the instance is triggered by Mark = false, so the invigilator must continue to run) take a 5-minute break (wait until the worker completes the job and the worker instance exits)-> set the tag to trigger the worker instance-> the worker is triggered (similarly, it is caused by Mark = true, so it is inevitable to continue running)-> 5 minutes of rest (the purpose of these 5 minutes is shown here, it is waiting for the invigilator instance to exit, otherwise, if the supervisor has not exited and is not triggered by subsequent operations by the worker, and the worker's own instance is also triggered earlier than the exit, no one will remind the worker to continue working) -> ...... So the poor workers began to count continuously under the control of the supervisor.
At the cost of the 5-minute rest of the worker instance and the 5-minute rest of the invigilator instance, we exchanged the stable processes triggered by each other. Therefore, the theoretical counting interval of this method is 10 minutes. According to my test, the actual average time is about 15 minutes.
You don't have to worry about it anymore! Why? In practical applications, in addition to mutual triggering, the user may modify the entry to wake up the stopped instance. Let's analyze the possible situation: the worker is sleeping (waiting for the supervisor to quit). At this time, the supervisor has finished his job and exited.-> the worker opens his eyes, before preparing to remind the Supervisor (set the flag), an external user made an update and woke up the supervisor. (because the worker is running, no new worker instance appears) -> the invigilator opened his eyes lazily and found that he should not work now (the flag has not been updated by the worker ), so I planned to quit-> while the supervisor woke up and found that there was no job to quit, the worker completed his job (set the tag bit and count)-> in this way, because the invigilator has not exited while the worker is working, the new invigilator instance does not appear. If a self-reminding occurs before the worker completes the work and exits (self-triggering occurs before exiting ), no new worker instances will appear! As a result, two people ended their work because of the interference of outsiders, and no one was responsible for counting them!
Okay ...... I admit that this situation is extremely extreme. During the test, I set up a console to randomly update entries every 1-60 seconds as an external interference, the process is still running normally after the entire night (31 times have been counted), but theoretically speaking, the above situation is still possible (if the instance exit time> the actual operation time of all workers ). In order to avoid this situation, we need to add the wait process again in the two workflows, wait for the unexpected wake-up generated by the user's external interference to exit and then run down.
I did not perform specific experiments, but I designed such an orchestration method:
In the worker, place the mark setting before the pause to trigger the invigilator in advance. The invigilator waits for one day for the worker to finish the work and then sets the mark to trigger the worker, wait 5 minutes and then create another heartbeat (update a field as needed ). Both 5-minute monitoring and 2-hour monitoring are designed to prevent the other party from being awakened due to external operations. We can talk about the user's external operation, which is inserted between each operation in the above process to analyze whether the external operation will affect the entire counting process. Because of the many situations, I will not analyze them here. It must be ensured that the second pause of the supervisor must be much smaller than that of the worker (because the trigger is irregular, a large floating range should be left ), at the same time, the worker's suspension should be far less than the first suspension of the supervisor. The theoretical counting interval of this process is one day and five minutes.
The article is quite complicated here, but it covers a solution for almost all situations (I say I almost thought I had a big head when designing, I don't know if any information has been missed ). If you have a better solution, please leave a message later.
PS. the actual project background of this item on my side is as follows: there is a list, each entry has a rag (red, Amber & green) Status field, depending on the date due in different cases, the rag status in date due is green. In another month, the rag turns yellow. After more than a month, the rag turns red. It can be said that this is a common application, but the reason why the SPD workflow is used for cyclic triggering is that the customer is not allowed to write background code (otherwise timerjob can easily deal with this requirement). Secondly, the Today formula cannot be used in the calculated field of SharePoint. In other words, the calculated field of Sharepoint is calculated when the value is not calculated, but when the entry is updated (even if