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

Execution hooks (core features) #508

Merged
merged 157 commits into from
Dec 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
157 commits
Select commit Hold shift + click to select a range
d1c9158
Prototype implementation for execution hooks.
csadorf Jun 13, 2018
75fa4e5
Fix issue that occurs when the add_operation 'hooks' argument is None.
csadorf Jun 13, 2018
fb61dd4
Properly handle exceptions for on_start hooks.
csadorf Jun 15, 2018
a3942ce
Reduce verbosity of hook module on errors.
csadorf Jun 17, 2018
4a8192f
Implement unit tests for run execution hooks.
csadorf Jul 24, 2018
6caf1c0
Revised hooks implementation and provide basic hook modules.
csadorf Jul 26, 2018
e066d45
Fix docstrings in snapshots module (typo/style).
csadorf Aug 10, 2018
3ca7655
Add option to exclude files/directories based on regular expression.
csadorf Aug 10, 2018
6b55bca
Rename the 'track-operations' executable to 'flow-track-operations'.
csadorf Aug 10, 2018
c1b4f3d
Add fork execution directive.
csadorf Dec 8, 2018
652ed87
Remove fork execution directive.
csadorf Dec 5, 2019
4232c06
Fix rebase error.
csadorf Dec 6, 2019
201b7f1
Install hooks through config as callable.
csadorf Dec 6, 2019
130e85d
Use hidden files for the operations log and tracking log.
csadorf Dec 6, 2019
b03d211
Rename '_metadata_schema_version' to '_schema_version'.
csadorf Dec 6, 2019
460e1e7
Remove left-over print statement.
csadorf Dec 6, 2019
03fd426
Use git-notes for automated git-tracking.
csadorf Dec 6, 2019
ca207d9
Fix style error.
csadorf Dec 6, 2019
5602701
Revert changes to base submission script.
csadorf Dec 6, 2019
4f21e6a
Update changelog.
csadorf Dec 6, 2019
6c93fdc
Remove obselete internal function.
csadorf Dec 15, 2019
f05d45e
Update metadata schema.
csadorf Dec 15, 2019
6d8afef
Remove print_function from __future__ import.
csadorf Dec 15, 2019
556446e
fixup! Update metadata schema.
csadorf Dec 15, 2019
28f621a
Rename '.snapshots' directory to '.signac_snapshots'.
csadorf Dec 15, 2019
860abd6
Refactor git repot initialization and git-ignore handling.
csadorf Dec 15, 2019
074cb5b
Use improved storage layout for snapshot system.
csadorf Dec 17, 2019
05e0a81
changes I failed to commit at the time
csadorf Feb 11, 2020
20dcb32
Merge branch 'master' into execution-hooks
csadorf Mar 1, 2020
40169c8
Fix issue with test_context unit test.
csadorf Mar 1, 2020
cf19cb9
Fix error message.
csadorf Mar 1, 2020
3a55d5b
Merged master.
klywang Mar 5, 2021
2b3b0ec
Fixed some errors made during merge conflict resolution.
klywang Mar 5, 2021
d0a15bc
Merge pull request #475 from glotzerlab/execution-hooks-merged-try-3
vyasr Mar 7, 2021
1ec837c
Style changes from pre-commit
cbkerr Mar 12, 2021
2e3a15a
remove a temp todo comment
cbkerr Mar 12, 2021
0f4cadb
Merge branch 'master' into execution-hooks
klywang Mar 18, 2021
87bd081
Use more descriptive name _hook_triggers
cbkerr Mar 18, 2021
dc3bcdb
pre commit formatting
cbkerr Mar 18, 2021
eb8317f
Merge branch 'master' into execution-hooks
klywang Apr 8, 2021
97294f3
Merge branch 'master' into execution-hooks
klywang Apr 13, 2021
993c2bb
Merge branch 'master' into execution-hooks
klywang Apr 15, 2021
3ce929c
Merge branch 'master' into execution-hooks
klywang Apr 30, 2021
9b9bd6e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 30, 2021
0fadaa7
fix typo in docstring link
cbkerr Mar 25, 2021
1d31725
Remove flow-track-operations.
bdice Apr 30, 2021
f350702
Remove non-core hook features.
bdice Apr 30, 2021
079d58b
Update changelog.
bdice Apr 30, 2021
2daa4c7
Use explicit keyword names.
bdice Apr 30, 2021
b6f6a34
Remove from_dict constructor, fix user API to use (name, *jobs) inste…
bdice Apr 30, 2021
a987130
Rename class to _InstallHook.
bdice Apr 30, 2021
8563853
Fix comment.
bdice Apr 30, 2021
5cf373c
Remove _install_config_hooks.
bdice Apr 30, 2021
766253b
Revert changes to logging configuration. Not needed until the LogOper…
bdice Apr 30, 2021
2aa96df
Merge branch 'master' into feature/hooks-core
klywang May 9, 2021
2e35fd1
Merge remote-tracking branch 'origin/master' into feature/hooks-core
bdice May 17, 2021
4adab1b
Merge remote-tracking branch 'origin/master' into feature/hooks-core
bdice May 20, 2021
b763a5a
Merge branch 'master' into feature/hooks-core
klywang May 21, 2021
42dd5ae
Merge branch 'master' into feature/hooks-core
klywang Jun 2, 2021
ca179c9
Merge branch 'master' into feature/hooks-core
klywang Aug 2, 2021
41ce973
Merge branch 'master' into feature/hooks-core
klywang Aug 2, 2021
948eb56
Merge branch 'feature/hooks-core' of https://github.com/glotzerlab/si…
klywang Aug 3, 2021
d9e5895
Added base hooks test, no error.
klywang Aug 5, 2021
52f8ed5
Used a function get get values from dictionary instead of typing the …
klywang Aug 5, 2021
9040b76
Added test for when hook.on_fail is triggered plus changes to test to…
klywang Aug 5, 2021
0bc20a5
Added base tests for a cmd function.
klywang Aug 5, 2021
b6f8ab1
Less clutter and neater in define_hooks_test_project.
klywang Aug 5, 2021
3e5b11b
Added hooks install.
klywang Aug 9, 2021
3ce9fe0
Removed test length of hooks.
klywang Aug 9, 2021
f22c804
Added cmd tests.
klywang Aug 9, 2021
6ea4163
Added test install test for cmd functions.
klywang Aug 9, 2021
39360a8
Merge branch 'master' into feature/hooks-core
klywang Aug 9, 2021
ac7906e
Ignore fail test when exception not raised.
klywang Aug 9, 2021
80380f8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 9, 2021
686bda8
Add docstrings to Hooks class.
klywang Aug 12, 2021
6e52a68
Merge branch 'master' into feature/hooks-core
klywang Aug 12, 2021
bd16433
Merge branch 'master' into feature/hooks-core
klywang Aug 15, 2021
0b0af9b
flake8 errors.
klywang Aug 16, 2021
f7dfed6
Merge branch 'master' into feature/hooks-core
klywang Aug 20, 2021
db82d4c
Merge branch 'master' into feature/hooks-core
klywang Aug 24, 2021
182cf03
Flake8 formatting.
klywang Aug 24, 2021
7606c16
Update flow/hooks/__init__.py
klywang Aug 26, 2021
527d921
Moved to changelog for 0.17
klywang Aug 26, 2021
9f4ecef
Reference class in docs
klywang Aug 26, 2021
04b8ef6
Merge branch 'feature/hooks-core' of https://github.com/glotzerlab/si…
klywang Aug 26, 2021
fa5ef90
Consistent language in the docstring
klywang Aug 26, 2021
ffaf05f
Input is operation, not operation_name
klywang Aug 26, 2021
237f22e
Language/wording
klywang Aug 26, 2021
29b781b
Missing parenthesis
klywang Aug 26, 2021
443c064
More accurate description of parameters
klywang Aug 26, 2021
e8dca0f
Grammar
klywang Aug 26, 2021
077981b
Directly return the function
klywang Aug 26, 2021
d3d19b8
Remove unneeded list
klywang Aug 26, 2021
91dbf1d
Return function directly instead of lambda function
klywang Aug 26, 2021
e404155
Fix typo (on_failure to on_fail)
klywang Aug 26, 2021
9290156
Merge branch 'feature/hooks-core' of https://github.com/glotzerlab/si…
klywang Aug 26, 2021
d3b356f
Change documentation wording to be more user friendly
klywang Aug 26, 2021
0a4f9cf
Swap order so variable can be used
klywang Aug 27, 2021
7ea174f
Use raise instead of raise error for more readable outputs
klywang Aug 27, 2021
cbc107f
Various edits to code.
klywang Aug 27, 2021
c6a0c39
Added tests for invalid hooks.
klywang Aug 27, 2021
9727566
set_job_doc_with_error needed an argument.
klywang Aug 27, 2021
5731d47
Merge branch 'feature/hooks-core' of https://github.com/glotzerlab/si…
klywang Aug 30, 2021
40ca4d0
Rename for clairty.
klywang Aug 30, 2021
e8d2995
Renamed for clarity and accuracy.
klywang Aug 30, 2021
db2ada9
Important distinction in docs
klywang Aug 30, 2021
87acacb
Merge branch 'feature/hooks-core' of https://github.com/glotzerlab/si…
klywang Aug 30, 2021
cd21900
More important distinctions in docs.
klywang Aug 30, 2021
9928584
Correct version number in changelog.
klywang Aug 30, 2021
13c63c9
Merge branch 'feature/hooks-core' of https://github.com/glotzerlab/si…
klywang Aug 30, 2021
fcab466
Renamed _HooksDecorator and added more descriptive docstring.
klywang Aug 30, 2021
8af01ad
Remove unnecessary __call__.
klywang Aug 30, 2021
87101c3
More changes for clarity on tests.
klywang Aug 30, 2021
f087140
Corrected docstring for accuracy.
klywang Aug 30, 2021
ad3e184
Edited string for clarity.
klywang Sep 1, 2021
0adaf64
Fixed typo in test.
klywang Sep 1, 2021
9649d18
Check for invalid hook attributes before instantiating the project.
klywang Sep 1, 2021
f8ac6ee
Added test for exceptions in hook.
klywang Sep 1, 2021
7c93f39
Make tests more readable.
klywang Sep 1, 2021
70e3659
Fixed typo in registry.
klywang Sep 1, 2021
7d482b6
Only test one thing at a time.
klywang Sep 1, 2021
314aa06
Only test one thing at a time.
klywang Sep 1, 2021
7dfcd0a
Merge branch 'master' into feature/hooks-core
klywang Sep 3, 2021
8e7beff
Moved _HookRegistry to class method.
klywang Sep 14, 2021
a981d31
Moved Hooks and _HooksList class out of __init__ file.
klywang Sep 14, 2021
2be393f
Added message to describe why.
klywang Sep 14, 2021
f7cf2c6
Changed from hook to add_hook for clarity.
klywang Sep 14, 2021
788febf
Removed unneeded changelong subheadings.
klywang Sep 14, 2021
b3838c8
Raise error with message.
klywang Sep 14, 2021
ef1b596
Renamed from HooksRegistry to HooksRegister because naming consistenc…
klywang Sep 14, 2021
fcf1657
Import hooks without flake8 error
klywang Sep 15, 2021
627a648
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 15, 2021
11eb205
Removed unneccessary assert lines.
klywang Sep 17, 2021
504b3b2
Merge branch 'feature/hooks-core' of https://github.com/glotzerlab/si…
klywang Sep 17, 2021
d9c7446
Better docs
klywang Sep 17, 2021
f6ea7ec
Used more explicit naming for project vs operation hooks.
klywang Sep 24, 2021
ca7219b
Merge branch 'master' into feature/hooks-core
klywang Sep 24, 2021
a93cd2e
Merge branch 'master' into feature/hooks-core
klywang Nov 8, 2021
d06a5ea
Apply suggestions from code review
klywang Nov 8, 2021
9e09c6a
Merge branch 'master' into feature/hooks-core
bdice Nov 17, 2021
b6d0a98
Apply suggestions to docstrings.
bdice Nov 17, 2021
b17dd45
Revise docstring.
bdice Nov 17, 2021
923bace
Remove metaclass initialization of _operation_hooks.
bdice Nov 17, 2021
0d7a87e
Fix code examples for operation hook.
bdice Nov 17, 2021
dea7b50
Rename _hooks to _project_hooks for clarity and symmetry with _operat…
bdice Nov 17, 2021
33eb3e5
Update docs.
bdice Nov 17, 2021
bfb7b17
Document operation_hook in API docs.
b-butler Nov 19, 2021
5cc4341
Improvements to FlowProject.operation_hook documentation
b-butler Nov 19, 2021
594cdbf
Changed to plural project hooks.
klywang Nov 22, 2021
47cae5c
Swapped hook and operation decorators in docstring and tests.
klywang Nov 22, 2021
c4d3ff0
Forgot to commit change in docstring for operation and hook decorator…
klywang Nov 22, 2021
286b7ec
Change Hooks to _Hooks
klywang Nov 22, 2021
bc62e80
Update docs with operation_hooks name change
b-butler Nov 22, 2021
3cbbcb5
Flip order of test directives to make operation first
b-butler Nov 22, 2021
1ee2124
Merge branch 'master' into feature/hooks-core
bdice Nov 23, 2021
3e58f30
Merge remote-tracking branch 'origin/master' into feature/hooks-core
bdice Dec 5, 2021
2d5b72d
Merge branch 'master' into feature/hooks-core
klywang Dec 25, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ Changelog
The **signac-flow** package follows `semantic versioning <https://semver.org/>`_.
The numbers in brackets denote the related GitHub issue and/or pull request.

Version 0.18

[0.18.0] -- 2022-xx-xx
----------------------

Added
+++++

- Feature to install execution hooks for the automated execution of functions with operations (#28, #189, #508).

Version 0.17
============

Expand Down
18 changes: 17 additions & 1 deletion doc/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ The FlowProject
FlowProject.make_group
FlowProject.operation
FlowProject.operation.with_directives
FlowProject.operation_hooks
FlowProject.operation_hooks.on_fail
FlowProject.operation_hooks.on_finish
FlowProject.operation_hooks.on_start
FlowProject.operation_hooks.on_success
FlowProject.operations
FlowProject.post
FlowProject.post.copy_from
Expand All @@ -52,6 +57,7 @@ The FlowProject
FlowProject.pre.not_
FlowProject.pre.true
FlowProject.print_status
FlowProject.project_hooks
FlowProject.run
FlowProject.scheduler_jobs
FlowProject.submit
Expand All @@ -60,12 +66,22 @@ The FlowProject
.. autoclass:: FlowProject
:show-inheritance:
:members:
:exclude-members: pre,post,operation
:exclude-members: pre,post,operation,operation_hooks

.. automethod:: flow.FlowProject.operation(func, name=None)

.. automethod:: flow.FlowProject.operation.with_directives(directives, name=None)

.. automethod:: flow.FlowProject.operation_hooks(hook_func, trigger)

.. automethod:: flow.FlowProject.operation_hooks.on_fail

.. automethod:: flow.FlowProject.operation_hooks.on_finish

.. automethod:: flow.FlowProject.operation_hooks.on_start

.. automethod:: flow.FlowProject.operation_hooks.on_success

.. automethod:: flow.FlowProject.post

.. automethod:: flow.FlowProject.post.copy_from
Expand Down
3 changes: 2 additions & 1 deletion flow/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

.. _signac: https://signac.io/
"""
from . import environment, errors, scheduling, testing
from . import environment, errors, hooks, scheduling, testing
from .aggregates import aggregator, get_aggregate_id
from .environment import get_environment
from .operations import cmd, directives, with_job
Expand All @@ -24,6 +24,7 @@
__all__ = [
"environment",
"errors",
"hooks",
"scheduling",
"testing",
"aggregator",
Expand Down
7 changes: 7 additions & 0 deletions flow/hooks/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Copyright (c) 2018 The Regents of the University of Michigan
klywang marked this conversation as resolved.
Show resolved Hide resolved
# All rights reserved.
# This software is licensed under the BSD 3-Clause License.
"""Operation hooks."""
from .hooks import _Hooks

__all__ = ["_Hooks"]
108 changes: 108 additions & 0 deletions flow/hooks/hooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# Copyright (c) 2018 The Regents of the University of Michigan
# All rights reserved.
# This software is licensed under the BSD 3-Clause License.
"""Operation hooks."""
import logging

logger = logging.getLogger(__name__)


class _HooksList(list):
def __call__(self, *args, **kwargs):
"""Call all hook functions as part of this list."""
for hook in self:
logger.debug(f"Executing hook function '{hook}'.")
try:
hook(*args, **kwargs)
except Exception as error:
logger.error(
"Error occurred during execution of "
"hook '{}': {}.".format(hook, error)
)
raise


class _Hooks:
""":class:`~._Hooks` execute an action or set of actions at specific stages of operation execution.

:class:`~._Hooks` can execute a user defined function when an operation
starts, succeeds, fails, or finishes (regardless of whether the operation
executed successfully or failed).

Hooks can be installed at the operation level as decorators, or on an
instance of :class:`~.FlowProject` through
:meth:`~.FlowProject.project_hooks`.

Examples
--------
The example below shows an operation level decorator that prints the
operation name and job id at the start of the operation execution.

.. code-block:: python

def start_hook(operation_name, job):
print(f"Starting operation {operation_name} on job {job.id}.")

@FlowProject.operation
@FlowProject.operation_hook.on_start(start_hook)
klywang marked this conversation as resolved.
Show resolved Hide resolved
def foo(job):
pass
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example here is no longer correct and we should probably also add an example for project-level hooks.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed this example but realized there are a few missing pieces. I'll write a separate comment.


Parameters
----------
on_start : list of callables
Function(s) to execute before the operation begins execution.
on_finish : list of callables
Function(s) to execute after the operation exits, with or without errors.
on_success : list of callables
Function(s) to execute after the operation exits without error.
on_fail : list of callables
Function(s) to execute after the operation exits with an error.

"""

_hook_triggers = [
"on_start",
"on_finish",
"on_success",
"on_fail",
]

def __init__(self, *, on_start=None, on_finish=None, on_success=None, on_fail=None):
def set_hooks(self, trigger_name, trigger_value):
if trigger_value is None:
trigger_value = []
setattr(self, trigger_name, _HooksList(trigger_value))

set_hooks(self, "on_start", on_start)
set_hooks(self, "on_finish", on_finish)
set_hooks(self, "on_success", on_success)
set_hooks(self, "on_fail", on_fail)

def __setattr__(self, name, value):
"""Convert to _HooksList when setting a hook trigger attribute."""
if name in self._hook_triggers:
super().__setattr__(name, _HooksList(value))
else:
super().__setattr__(name, value)

def update(self, other):
"""Update this instance with hooks from another instance."""
for hook_trigger in self._hook_triggers:
getattr(self, hook_trigger).extend(getattr(other, hook_trigger))

def __str__(self):
"""Return a string representation of all hooks."""
return "{}({})".format(
type(self).__name__,
", ".join(
f"{hook_trigger}={getattr(self, hook_trigger)}"
for hook_trigger in self._hook_triggers
),
)

def __bool__(self):
"""Return True if hooks are defined."""
return any(
getattr(self, hook_trigger, None) for hook_trigger in self._hook_triggers
)
Loading