-
Notifications
You must be signed in to change notification settings - Fork 5
fix: Ensure consistent task finalization for queued tasks (TaskError and TaskProcessed events) #17
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
base: main
Are you sure you want to change the base?
Conversation
…ding to the next middleware
WalkthroughWraps FinalizeTaskMiddleware's call to the next middleware and task finalization in a try/catch that emits a TasksError event, calls the task's fail() on exception, and rethrows; adds a queued-task test verifying finalization behavior and Processed event dispatch. Changes
Sequence Diagram(s)sequenceDiagram
participant M as FinalizeTaskMiddleware
participant N as Next Middleware
participant T as Task
participant E as EventDispatcher
rect rgb(220,240,220)
Note over M,T: Normal (success) path
M->>N: next($task)
N-->>M: returns
M->>T: finalize()
T-->>M: done
end
rect rgb(255,230,230)
Note over M,E: Error path (new)
M->>N: next($task)
N--x M: throws Throwable
M->>E: dispatch TasksError(task class, payload, runProcessId, meta)
M->>T: fail(Throwable)
M-->>M: rethrow Throwable
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes
Possibly related PRs
Suggested reviewers
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (1)
tests/Feature/TaskTest.php (1)
482-504: Queued-task test doesn’t actually assert middleware ordering and assumes sync queue behaviorThe new test is valuable to cover queued tasks, but two points are worth considering:
Name vs behavior
The namequeued tasks are finalized before going through next middlewaresuggests you are asserting relative ordering (i.e.,finalize()runs before the next middleware), but the body only checks thatProcessedwas dispatched at all. If ordering is the real concern, you may want to:
- Add a secondary middleware placed after
FinalizeTaskMiddlewarethat assertsEvent::assertDispatched(Processed::class)inside itshandle(); or- Capture a flag set by
finalize()and assert that the flag is true when the “next” middleware runs.Alternatively, if you only care that queued tasks get finalized somewhere in the flow, renaming the test to match that behavior would reduce confusion.
Assumption about queue driver
ProcessQueuedTask::dispatchSync()internally callsQueuedTask::dispatch($payload)forShouldQueuetasks. The test relies onQueuedTaskbeing executed synchronously so thatProcessedis dispatched before the assertion. If the default queue connection in the test environment is changed to an async driver (database/redis/etc.), this assertion may start failing or become flaky.To make the test more robust, consider explicitly forcing a sync driver or equivalent within the test (e.g., via configuration) or documenting that the test suite expects a sync queue connection.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
src/Tasks/Middleware/FinalizeTaskMiddleware.php(1 hunks)tests/Feature/TaskTest.php(2 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
tests/Feature/TaskTest.php (3)
src/Task.php (1)
Task(34-309)src/Tasks/Middleware/FinalizeTaskMiddleware.php (1)
handle(11-16)src/Process.php (2)
handle(106-135)Process(29-259)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
♻️ Duplicate comments (1)
src/Tasks/Middleware/FinalizeTaskMiddleware.php (1)
21-38: Error handling pattern looks good; verify that finalization order still matches queued-task requirementsWrapping
$next($task)and$task->finalize()in atry/catch, emitting aTasksErrorevent, calling$task->fail($e), and rethrowing theThrowableis a solid pattern and preserves the “finalize after pipeline completes” behavior on the happy path.Given the PR description and earlier review discussion, there was a requirement that queued tasks be finalized before handing off to the next middleware (while keeping the original “finalize after run” semantics for non-queued tasks). This middleware currently calls
finalize()only after$next($task)for all tasks. If the “queued tasks finalize-before-next” behavior is still desired and not handled elsewhere, you may need to reintroduce a conditional branch (e.g., forShouldQueuetasks) and merge it with this new error-handling structure.Please confirm whether that requirement has changed or is now implemented in another layer; if not, the earlier ShouldQueue-scoped approach likely still applies here.
🧹 Nitpick comments (2)
src/Tasks/Middleware/FinalizeTaskMiddleware.php (2)
19-20: Guard against missing or malformedprocesscontext when destructuring
[, $runProcessId] = Context::get('process');assumes that theprocesscontext is always set and is an indexed array with at least two elements. If it’s ever unset or changes shape, this can lead to notices/TypeErrors and an undefined$runProcessIdin the catch block.Consider defensively handling the context (and allowing a
nullrunProcessId) instead of hard-destructuring, for example by checking for an array and using a default:$processContext = Context::get('process'); $runProcessId = is_array($processContext) ? ($processContext[1] ?? null) : null;This keeps the event dispatch resilient even if the context is missing or partially populated.
25-39: Reconsider excluding the entire error path from code coverage
// @codeCoverageIgnoreStartbefore thecatchmeans the whole error-handling branch (event dispatch,fail($e), and rethrow) is invisible to coverage. Since this is non-trivial behavior, it’s usually worth exercising it in tests (e.g., by faking an exception from$nextorfinalize(), and asserting that the error event is emitted andfail()is called) instead of excluding it.If feasible, prefer a small dedicated test over blanket
@codeCoverageIgnoreon the catch block so regressions in error reporting don’t slip through unnoticed.
What:
Description:
This pull request updates the task finalization middleware to ensure that tasks are finalized before passing them to the next middleware, and adds a new test to verify that this behavior also applies to queued tasks. The main changes focus on the correct ordering of middleware operations and improved test coverage for queued tasks.
Middleware behavior update:
FinalizeTaskMiddlewareso that$task->finalize()is called before invoking the next middleware, ensuring tasks are finalized prior to further processing.Testing improvements:
ShouldQueueare finalized before proceeding to the next middleware. This includes defining aQueuedTaskclass and aProcessQueuedTaskprocess, then asserting the appropriate event is dispatched.ShouldQueueimport in the test file to support the new queued task test.Summary by CodeRabbit
Bug Fixes
Tests
✏️ Tip: You can customize this high-level summary in your review settings.