Skip to content

Commit

Permalink
job report caller
Browse files Browse the repository at this point in the history
  • Loading branch information
rodber committed Jan 21, 2025
1 parent c4276c2 commit ba8fe64
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 14 deletions.
4 changes: 4 additions & 0 deletions src/Interfaces/JobInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,8 @@ public function isSync(): bool;
* @return VectorInterface<ResponseReferenceInterface|VariableInterface>
*/
public function runIf(): VectorInterface;

public function caller(): string;

public function shift(): int;
}
39 changes: 39 additions & 0 deletions src/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ final class Job implements JobInterface

private bool $hasVariadic;

private string $caller;

private int $shift;

/**
* Creates a Job (async by default).
*/
Expand All @@ -75,6 +79,27 @@ public function __construct(
}
$this->arguments = [];
$this->setArguments(...$argument);
$this->shift = 0;
$this->setCaller();
}

public function withShift(int $shift): JobInterface
{
$new = clone $this;
$new->shift = $shift;
$new->setCaller();

return $new;
}

public function shift(): int
{
return $this->shift;
}

public function caller(): string
{
return $this->caller;
}

public function withArguments(mixed ...$argument): JobInterface
Expand Down Expand Up @@ -148,6 +173,20 @@ public function isSync(): bool
return $this->isSync;
}

/**
* @infection-ignore-all
*/
private function setCaller(): void
{
$debugBacktrace = debug_backtrace();
for ($i = 0; $i <= $this->shift; $i++) {
array_shift($debugBacktrace);
}
$file = $debugBacktrace[0]['file'] ?? 'unknown';
$line = $debugBacktrace[0]['line'] ?? '0';
$this->caller = "{$file}:{$line}";
}

private function setArguments(mixed ...$argument): void
{
if (! $this->hasVariadic) {
Expand Down
28 changes: 20 additions & 8 deletions src/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Chevere\Workflow\Interfaces\RunnerInterface;
use Chevere\Workflow\Interfaces\VariableInterface;
use OutOfBoundsException;
use ReflectionMethod;
use Throwable;
use function Amp\Future\await;
use function Amp\Parallel\Worker\submit;
Expand Down Expand Up @@ -88,7 +89,20 @@ public function withRunJob(string $name): RunnerInterface
}
$arguments = $new->getJobArguments($job);
$action = $job->action();
$response = $new->getActionResponse($action, $arguments);

try {
$response = $new->getActionResponse($action, $arguments);
} catch (Throwable $e) {
throw new $e(
code: $e->getCode(),
message: (string) message(
'Workflow error %message% for Job `%job%` in %fileLine%',
message: $e->getMessage(),
fileLine: $job->caller(),
action: $action::class,
)
);
}
$new->addJobResponse($name, $response);

return $new;
Expand All @@ -110,20 +124,18 @@ private function getActionResponse(
array $arguments
): CastInterface {
try {
$toCast = $action->__invoke(...$arguments);

return cast($toCast);
return cast($action(...$arguments));
} catch (Throwable $e) { // @codeCoverageIgnoreStart
$actionTrace = $e->getTrace()[1] ?? [];
$reflector = new ReflectionMethod($action, $action::mainMethod());
$fileLine = strtr('%file%:%line%', [
'%file%' => $actionTrace['file'] ?? 'anon',
'%line%' => $actionTrace['line'] ?? '0',
'%file%' => $reflector->getFileName(),
'%line%' => $e->getLine(),
]);

throw new $e(
code: $e->getCode(),
message: (string) message(
'%message% at `%fileLine%` for action `%action%`',
'%message% for `%action%` in %fileLine%',
message: $e->getMessage(),
fileLine: $fileLine,
action: $action::class,
Expand Down
6 changes: 4 additions & 2 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ function workflow(JobInterface ...$job): WorkflowInterface
*/
function sync(ActionInterface $_action, mixed ...$argument): JobInterface
{
return (new Job($_action, ...$argument))->withIsSync(true);
return (new Job($_action, ...$argument))
->withShift(1)
->withIsSync(true);
}

/**
Expand All @@ -51,7 +53,7 @@ function sync(ActionInterface $_action, mixed ...$argument): JobInterface
*/
function async(ActionInterface $_action, mixed ...$argument): JobInterface
{
return new Job($_action, ...$argument);
return (new Job($_action, ...$argument))->withShift(1);
}

/**
Expand Down
14 changes: 10 additions & 4 deletions tests/FunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,21 @@ public function testFunctionSync(): void
{
$action = new TestActionNoParamsIntResponse();
$job = sync($action);
$alt = (new Job($action))->withIsSync(true);
$this->assertEquals($alt, $job);
$fileLine = __FILE__ . ':' . (__LINE__ - 1);
$this->assertSame($fileLine, $job->caller());
$alt = (new Job($action))
->withShift(1)
->withIsSync(true);
$this->assertSame($alt->shift(), $job->shift());
}

public function testFunctionAsync(): void
{
$action = new TestActionNoParamsIntResponse();
$job = async($action);
$alt = new Job($action);
$this->assertEquals($alt, $job);
$fileLine = __FILE__ . ':' . (__LINE__ - 1);
$this->assertSame($fileLine, $job->caller());
$alt = (new Job($action))->withShift(1);
$this->assertSame($alt->shift(), $job->shift());
}
}
2 changes: 2 additions & 0 deletions tests/JobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ public function testRawArguments(): void
'foo' => 'foo',
];
$job = new Job($action, ...$success);
$fileLine = __FILE__ . ':' . (__LINE__ - 1);
$this->assertSame($fileLine, $job->caller());
$this->assertSame($action, $job->action());
$this->assertSame($success, $job->arguments());
$success = [
Expand Down

0 comments on commit ba8fe64

Please sign in to comment.