When and how is the WorkChain triggered to save a checkpoint?

Hi all, I have a question about the AiiDA persister. When and how is the WorkChain (or Process) triggered to save a checkpoint?

There are essentially three moments in a processs’ lifetime when its state gets persisted:

  1. When it is first submitted (not, this therefore does not apply when a process is run):
    See
    https://github.com/aiidateam/aiida-core/blob/1dafdf2ddb38c801d2075d9af9bbde9e0d26c8ca/aiida/engine/runners.py#L196
    and https://github.com/aiidateam/aiida-core/blob/1dafdf2ddb38c801d2075d9af9bbde9e0d26c8ca/aiida/engine/launch.py#L117
  2. When the process is paused:
    https://github.com/aiidateam/aiida-core/blob/1dafdf2ddb38c801d2075d9af9bbde9e0d26c8ca/aiida/engine/processes/process.py#L499
  3. When the process enters a new state:
    https://github.com/aiidateam/aiida-core/blob/1dafdf2ddb38c801d2075d9af9bbde9e0d26c8ca/aiida/engine/processes/process.py#L414-L434

The latter one is the most important. For a WorkChain it essentially means whenever a new step of the outline is called, the state is persisted. This makes it possible for the daemon to pick up where it left off with a workchain, even if it got stopped abruptly.

@sphuber , thanks for your reply.
Since others may not be very clear on the enters a new state. I tried to add a comment on the workchain part. WorkChain uses the outline to execute the workflow. Each step in the outline can submit sub-processes and wait until all sub-processes are finished.
So the workchain state will change like:

run step1 --> running
wait step1 --> waiting
run next step2 --> running
...

The workchain will save a checkpoint every time a new state is entered.

Great clarification, thanks @Xing . One final detail: every step will cause a state transition and so will cause a checkpoint to be created. It doesn’t have to submit any subprocesses. Even a simple step that is used in an if_ clause will have a state transition. So checkpoints will happen between each and every step in the outline