Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add callback function to electron #1676

Open
xjf729 opened this issue May 30, 2023 · 0 comments
Open

Add callback function to electron #1676

xjf729 opened this issue May 30, 2023 · 0 comments
Labels
feature New feature addition priority / medium One of the priorities in a month

Comments

@xjf729
Copy link

xjf729 commented May 30, 2023

What should we add?

Discussed in #1673

Now, electron have call_after hooks, which means electron can do something after electron completed successfully. However, there isn't some hooks to let electron do something when status of electron is Failed or Cancelled. Maybe there should have some callback hooks for electron when it is failure or cancelled.

Describe alternatives you've considered.

There is a suggested UX to use callback hooks.

UX

def call_before_hook(value):
    Path("data.txt").write_text(value)
    return value

def call_after_hook():
    Path("data.txt").unlink()

# Remove `data.txt` when `get_my_value_completed` is completed. It has been archieved by `Covalent`. 
# ref: https://covalent.readthedocs.io/en/latest/how_to/coding/add_callable_dependencies_to_electron.html
@ct.electron(
    call_before=ct.DepsCall(call_before_hook, args=(1, )),
    call_after=ct.DepsCall(call_after_hook),
)
def get_my_value_completed():
    value = Path("data.txt").read_text()
    return int(value)

# Remove `data.txt`, regardless of the status of `get_my_value`. It uses new `call_finally` hook.
@ct.electron(
    call_before=ct.DepsCall(call_before_hook, args=(1, )),
    call_finally=ct.DepsCall(call_after_hook),
)
def get_my_value_finally():
    value = Path("data.txt").read_text()
    return int(value)

# Remove `data.txt` when `get_my_value` is cancelled. It uses new `call_after_cancelled` hook
@ct.electron(
    call_before=ct.DepsCall(call_before_hook, args=(1, )),
    call_after_cancelled=ct.DepsCall(call_after_hook),
)
def get_my_value_cancelled():
    value = Path("data.txt").read_text()
    return int(value)

This example will create a file named data.txt, and when function get_my_value* goes to a certain status, data.txt will be removed.

@xjf729 xjf729 added the feature New feature addition label May 30, 2023
@jimmylism jimmylism added the priority / medium One of the priorities in a month label Dec 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature addition priority / medium One of the priorities in a month
Projects
None yet
Development

No branches or pull requests

2 participants