RFC: Pre and Post Run API #29637
Replies: 6 comments 11 replies
-
I know you have your power packs. But I think a lot of custom runners was used for caching even by custom API. How can this be done with this |
Beta Was this translation helpful? Give feedback.
-
The pre and post hooks part makes sense. The only thing I'm still a bit unsure on is this comment
We have definitely used a custom task runner to influence how the default task runner is about to run in fairly trivial ways. It's very possible env variables will do the job, but I don't really know what all is going to be exposed that way in v21 to compare. |
Beta Was this translation helpful? Give feedback.
-
Awesome work. Thank you so much for this! 1.)
2.) 3.) This is because I would like to be able to collect data, such as freemem (and more), when the task starts and use that in the post-run hook |
Beta Was this translation helpful? Give feedback.
-
One big missing piece from the proposed API in the PR is in the This makes the first step of the custom cache flow impossible.
If the task details could simple be provided as part of the |
Beta Was this translation helpful? Give feedback.
-
Thank you all for your feedback on the API. We have multiple reports of this API being helpful and sufficient for them. I'll move forward getting this landed for you all soon! |
Beta Was this translation helpful? Give feedback.
-
@FrozenPandaz Will post task runner allow to mark the task as failed, let's say if status is not 'remote-cache' I want to fail the task itself. |
Beta Was this translation helpful? Give feedback.
-
RFC: Pre and Post Tasks Execution API
Problem
Workspaces have no clear way of defining hooks which happen before and after tasks are executed.
Custom task runners have traditionally handled this functionality, but their APIs had unclear extension points and it created a lot of problems for us while evolving Nx. This new API provides a well-defined way to handle common task runner use cases without compromises.
Prior Art
This is akin to how npm has preinstall and postinstall hooks.
Example Use Cases
Solution
Nx Plugins will be able to define functions which will be executed prior and after Nx runs tasks. The API will look something like the following (open to feedback):
plugin.ts
Like npm's
preinstall
andpostinstall
hooks, there are many use cases where this API could be useful. However, just as npm'spreinstall
andpostinstall
hooks do little to influence the actual installation of dependencies, these hooks will also have little to no influence on the actual execution of tasks. ThepreRun
hook may be used to prevent Nx from executing tasks if some criteria is not met by throwing an error.Extra remarks
The reason the API is not affecting our ability to evolve Nx and is much easier for us to support is that it doesn't augment how Nx actually executes tasks: scheduling, orchestration, distribution, caching, handling io, terminal output will all happen in between the pre and post hooks. The only way to directly affect the execution is via env variables. Everything else is done indirectly (creating files, starting processes etc)
Demo
I've opened a Draft PR with an implementation of this API: #29636. A version of Nx will be published for those interested in trying out the API before it is released.
Edits
I changed the name of the API to
preTasksExecution
andpostTasksExecution
I'll update the PR in a bit.Beta Was this translation helpful? Give feedback.
All reactions