pytransitions / transitions

A lightweight, object-oriented finite state machine implementation in Python with many extensions
MIT License
5.49k stars 524 forks source link

Add Retry mix-in #461

Closed rgov closed 2 years ago

rgov commented 3 years ago

Note: This involved a bit of in-browser coding, so please check for formatting issues and bugs.

This change introduces a Retry mix-in which lets you limit the number of times a state may be re-entered from itself. This is useful if a state represents an operation that may need to be retried a few times, but should eventually give up.

This works really well with the Timeout mix-in:

{ 'name': 'PingServer',
  'timeout': 1, 'on_timeout': 'to_PingServer',
  'retries': 3, 'on_failure': 'to_CriticalFailure',
},

In this example, the PingServer state might involve sending a ping packet to a server and expecting a pong reply, whereupon it will transition to another state. If the timeout is met, then it would re-enter the PingServer state, triggering a second ping to be sent. But on the 5th time the state is entered, it will instead transition to CriticalFailure without sending another ping.

cliffburdick commented 2 years ago

Hi @rgov and @aleneum, was this feature ever merged into the main branch? I noticed that there's no workaround currently because setting a timeout and going back to your original state doesn't work.

rgov commented 2 years ago

It is on the dev-0.9 branch, which is not yet merged into main.