Prevent Unnecessary Task Execution
Learn how to prevent unnecessary task execution in Taskfile
By fingerprinting locally generated files and their sources
We can avoid unnecessary workload of tasks to reduce the time taken. Meaning that the tasks will only run when there are changes in the required files.
For example, when you work on a Node.JS application, you may have a task to install dependencies and build the application.
npm install
- run this command when there are changes in thepackage.json
file.npm run build
- run this command where there are changes in any**/*.js
files.
sources
andgenerates
can be files or glob patternssources
- it will compare the checksum to see whether the files got any changesgenerates
- this will be the files generated by the command, if this file is deleted, the task will run again
exclude
- exclude files from fingerprinting, but you have to make sure thesources
are in glob format and must come after the positive glob it is negating.method: timestamp
- If you prefer to check the timestamp of the files instead of their checksum, you can usemethod: timestamp
. This is useful for large files where checksum calculation might be expensive.method: none
- Skips the validation, meaning the task will always run regardless of changes in the source files.method: checksum
- This is the default method, which checks the checksum of the files to determine if they have changed.
status
- You can also use thestatus
command to check the status of the task. It will show whether the task is up to date or not. Refer Using programmatic checks to indicate a task is up to date
A .task
directory will be created in the source directory to store the fingerprint of the files. This directory is used to track changes in the files specified in the sources
section of the task. If you want to avoid committing this directory to version control, consider adding it to your .gitignore
file.
If you want to change the location of the .task
directory, you can set the TASK_TEMP_DIR
environment variable to the desired path.
export TASK_TEMP_DIR=/path/to/custom/.task
When you rerun the task, it will not run the npm install
command as there are no changes in the package.json
file. You will see a message indicating that the task is up to date.
Using programmatic checks to indicate a task is up to date
Use task --status <tasks>
to check the status of a task is up to date or not.
You can also use programmatic checks to indicate whether a task is up to date. In this case, you can use the status
command to check the status of the task before running the commands. If the task is up to date, it will skip running the commands.
This method is quite useful when you want to create artifacts or files that are not directly related to the source files, or when you want to ensure that certain files exist before running a task.
Here is how it works: - Task always checks the status first.
- On the first run, the status checks will fail (because the directory and files don't exist), so the commands will execute to create them.
- On subsequent runs, the status checks will pass (because the directory and files now exist), so the commands will not run again. The task is skipped.
Combination of sources
and status
checks:
With the combination of sources
and status
checks, if either the sources change or programmatic checks fail, then the task will run again.
Using programmatic checks to cancel the execution of a task and its dependencies (Preconditions Checks)
You can setup preconditions checks to run before the task commands. If any of the checks fail, the task will not run. It is very similar to the status
command just that it support sh
expansion. If any preconditions fail, the task and its dependencies are canceled and do not run.
Here is an example of the dependencies are canceled when the preconditions fail:
Difference between status
and preconditions
Feature | status | preconditions |
---|---|---|
Purpose | Check if task is up to date | Ensure requirements are met before running |
Effect on Task Execution | Skips task if up to date and continue executing tasks that depend on it | Fails task and any dependent task if conditions are not met |
Limiting when tasks run
More Information
run
can also be set at the root level of the Taskfile to change the behavior of all tasks in the Taskfil unless overridden in individual tasks.
Sometimes you may want to limit when tasks run based on certain conditions especially there are multiple cmds
or deps
in the task. In this case, you can use the run
to change the behavior of the task execution.
Supported run
options:
always
(default) - The task will always run regardless of the number of times it has been run.once
- The task will only run once, even if it has been run multiple times.when_changed
- The task will only run once for each unique set of variables passed into the task.
As you can see, the install-deps
task only runs once, even though it is called multiple times in the default
task. The generate-file
task runs only once for each unique set of variables passed into it.