Skip to content

Commit

Permalink
fix: fix
Browse files Browse the repository at this point in the history
  • Loading branch information
timmylindh committed May 2, 2024
1 parent cca2b16 commit fbb11ef
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 34 deletions.
10 changes: 10 additions & 0 deletions src/SQSJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ public function getSqsReceiveCount()
return (int) $this->job['receiveCount'];
}

/**
* Determine if the job should has timedout and should fail.
*
* @return bool
*/
public function hasTimedoutAndShouldFail()
{
return $this->getSqsReceiveCount() > 1 && $this->shouldFailOnTimeout();
}

/**
* Get the job identifier.
*
Expand Down
46 changes: 12 additions & 34 deletions src/SQSWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,23 @@ class SQSWorker extends Worker
*/
public function process($connectionName, $job, WorkerOptions $options)
{
if ($job->hasTimedoutAndShouldFail()) {
return $this->markJobAsFailedIfItShouldFailOnTimeout(
$job->getConnectionName(),
$job,
$this->timeoutExceededException($job),
);
}

$this->startTimeoutHandler($job, $options);
parent::process($connectionName, $job, $options);
}

/**
* Register the worker timeout handler. This will on timeout reached fail the job.
* Due the the built-in Laravel shutdown function throwing an error on timeout
* we need to buffer the output and return an empty string to avoid the SQS daemon
* requiung the job.
* the SQS daemon will not delete the job and it will be retried. Thus we delete it on
* the next re-queue.
*
* @param \Illuminate\Contracts\Queue\Job|null $job
* @param \Illuminate\Queue\WorkerOptions $options
Expand All @@ -43,44 +51,14 @@ protected function startTimeoutHandler($job, WorkerOptions $options)

set_time_limit(max($this->timeoutForJob($job, $options), 0));

ob_start(function ($str) use ($job, $options) {
register_shutdown_function(function () use ($job, $options) {
if (connection_status() !== CONNECTION_TIMEOUT) {
return $str;
return;
}

$this->markJobAsFailedIfWillExceedMaxAttempts(
$job->getConnectionName(),
$job,
(int) $options->maxTries,
$e = $this->timeoutExceededException($job),
);

$this->markJobAsFailedIfWillExceedMaxExceptions(
$job->getConnectionName(),
$job,
$e,
);

$this->markJobAsFailedIfItShouldFailOnTimeout(
$job->getConnectionName(),
$job,
$e,
);

header('HTTP/1.1 204 No Content');

report($e);
$this->events->dispatch(
new JobTimedOut($job->getConnectionName(), $job),
);

return '';
});

register_shutdown_function(function () {
if (ob_get_level() > 0) {
ob_end_flush();
}
});
}
}

0 comments on commit fbb11ef

Please sign in to comment.