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

fix: Allow fingerprint function to return execution status #303

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@
* Add a `yaml_dump()` function to dump any PHP value to a YAML string
* Add a `yaml_parse()` function to parse a YAML string to a PHP value
* Remove the default timeout of 60 seconds from the Context
* Add `bool` return type to `fingerprint()` function to indicate if the callable was run

## 0.13.1 (2024-02-27)

Expand Down
6 changes: 5 additions & 1 deletion examples/fingerprint.php
Expand Up @@ -47,14 +47,18 @@ function task_with_a_fingerprint_and_force(
): void {
run('echo "Hello Task with Fingerprint!"');

fingerprint(
$hasRun = fingerprint(
callback: function () {
run('echo "Cool, no fingerprint! Executing..."');
},
fingerprint: my_fingerprint_check(),
force: $force // This option will force the task to run even if the fingerprint has not changed
);

if ($hasRun) {
run('echo "Fingerprint has been executed!"');
}

run('echo "Cool! I finished!"');
}

Expand Down
16 changes: 8 additions & 8 deletions src/functions.php
Expand Up @@ -885,16 +885,16 @@ function fingerprint_save(string $fingerprint): void
GlobalHelper::getApplication()->fingerprintHelper->postProcessFingerprintForHash($fingerprint);
}

function fingerprint(callable $callback, string $fingerprint, bool $force = false): void
function fingerprint(callable $callback, string $fingerprint, bool $force = false): bool
{
if (!fingerprint_exists($fingerprint) || $force) {
try {
$callback();
fingerprint_save($fingerprint);
} catch (\Throwable $e) {
throw $e;
}
if ($force || !fingerprint_exists($fingerprint)) {
$callback();
fingerprint_save($fingerprint);

return true;
}

return false;
}

/**
Expand Down
@@ -1,3 +1,4 @@
Hello Task with Fingerprint!
Cool, no fingerprint! Executing...
Fingerprint has been executed!
Cool! I finished!