celery / jumpstarter

MIT License
7 stars 3 forks source link

Restarting #18

Closed ahopkins closed 3 years ago

ahopkins commented 3 years ago

Here is a proposal for how to add .init() and .restart() trriggers.

Example is easiest explanation.

import asyncio
from jumpstarter import Actor

class Workflow(Actor):
    def on_enter_initializing(self, *args):
        print("\tEnter to initializing")

    def on_exit_initializing(self, *args):
        print("\tExit from initializing")

    def on_enter_initialized(self, *args):
        print("\tEnter to initialized")

    def on_exit_initialized(self, *args):
        print("\tExit from initialized")

    def on_enter_starting(self, *args):
        print("\tEnter to starting")

    def on_exit_starting(self, *args):
        print("\tExit from starting")

    def on_enter_starting(self, *args):
        print("\tEnter to starting")

    def on_exit_starting(self, *args):
        print("\tExit from starting")

    def on_enter_stopping(self, *args):
        print("\tEnter to stopping")

    def on_exit_stopping(self, *args):
        print("\tExit from stopping")

    def on_enter_stopped(self, *args):
        print("\tEnter to stopped")

    def on_exit_stopped(self, *args):
        print("\tExit from stopped")

async def main():
    workflow = Workflow()
    print(f"{workflow.state=}")

    print(">>> Init")
    await workflow.init()
    print(f"{workflow.state=}")

    print(">>> Start")
    await workflow.start()
    print(f"{workflow.state=}")

    print(">>> Stop")
    await workflow.stop()
    print(f"{workflow.state=}")

    print(">>> Start")
    await workflow.start()
    print(f"{workflow.state=}")

    print(">>> Start")
    await workflow.start()
    print(f"{workflow.state=}")

    print(">>> Restart")
    await workflow.restart()
    print(f"{workflow.state=}")

    print(">>> Start")
    await workflow.start()
    print(f"{workflow.state=}")

asyncio.run(main())

Output

$ poetry run python /path/to/proposal.py
workflow.state=<ActorState.initializing: 1>
>>> Init
    Exit from initializing
    Enter to initialized
workflow.state=<ActorState.initialized: 2>
>>> Start
    Exit from initialized
    Enter to starting
    Exit from starting
workflow.state=<ActorState.started: 3>
>>> Stop
    Enter to stopping
    Exit from stopping
    Enter to stopped
workflow.state=<ActorState.stopped: 4>
>>> Start
    Exit from stopped
    Enter to starting
workflow.state=<ActorState.starting: <enum 'ActorStartingState'>>
>>> Start
    Exit from starting
workflow.state=<ActorState.started: 3>
>>> Restart
    Enter to stopping
    Exit from stopping
    Enter to stopped
workflow.state=<ActorState.stopped: 4>
>>> Start
    Exit from stopped
    Enter to starting
workflow.state=<ActorState.starting: <enum 'ActorStartingState'>>
ghost commented 3 years ago

DeepCode's analysis on #76b198 found:

Description Example fixes
Use comprehensions instead of list(filter(lambda: x ...)) Occurrences: :wrench: Example fixes
Access to a protected member _state_machine of a client class Occurrences: :wrench: Example fixes
Redefining name 'resource' from outer scope (line 92) Occurrences: :wrench: Example fixes

👉 View analysis in DeepCode’s Dashboard | Configure the bot

lgtm-com[bot] commented 3 years ago

This pull request introduces 1 alert when merging 76b198a662bcbef5ca4f08a5b4cf26b45f85b64d into a3a320f19421c3dcff0255c87fe8d978fc693bbd - view on LGTM.com

new alerts: