Skip to content

Commit

Permalink
feat!: add wait/handle time to ProcessedMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
kbond committed Nov 11, 2024
1 parent 9e89ebd commit 23cc03e
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 5 deletions.
42 changes: 42 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Upgrade Guide

## 0.5.0

Two new `integer` columns were added to the `processed_messages` table:
`wait_time` and `handle_time`. You will need to create a migration to
add these columns to your database. They are not nullable so your
migration will need to account for existing data. You can either
truncate (purge) the `processed_messages` table have your migration
calculate these values based on the existing data.

Here's a calculation example for MySQL:

```php
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

final class VersionXXX extends AbstractMigration
{
public function getDescription(): string
{
return 'Add processed_messages.wait_time and handle_time columns';
}

public function up(Schema $schema): void
{
// Add the columns as nullable
$this->addSql('ALTER TABLE processed_messages ADD wait_time INT DEFAULT NULL, ADD handle_time INT DEFAULT NULL');

// set the times from existing data
$this->addSql('UPDATE processed_messages SET wait_time = TIMESTAMPDIFF(SECOND, dispatched_at, received_at), handle_time = TIMESTAMPDIFF(SECOND, received_at, finished_at)');

// Make the columns not nullable
$this->addSql('ALTER TABLE processed_messages CHANGE wait_time wait_time INT NOT NULL, CHANGE handle_time handle_time INT NOT NULL');
}

public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE processed_messages DROP wait_time, DROP handle_time');
}
}
```
2 changes: 2 additions & 0 deletions config/doctrine/mapping/ProcessedMessage.orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
<field name="dispatchedAt" column="dispatched_at" type="datetime_immutable" />
<field name="receivedAt" column="received_at" type="datetime_immutable" />
<field name="finishedAt" column="finished_at" type="datetime_immutable" />
<field name="waitTime" column="wait_time" type="integer" />
<field name="handleTime" column="handle_time" type="integer" />
<field name="memoryUsage" column="memory_usage" type="integer" />
<field name="transport" column="transport" />
<field name="tags" column="tags" nullable="true" />
Expand Down
10 changes: 7 additions & 3 deletions src/History/Model/ProcessedMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ abstract class ProcessedMessage
private int $memoryUsage;
private string $transport;
private ?string $tags;
private int $waitTime;
private int $handleTime;

/** @var class-string<\Throwable> */
private ?string $failureType = null;
Expand All @@ -61,6 +63,8 @@ public function __construct(Envelope $envelope, Results $results, ?\Throwable $e
$this->transport = $monitorStamp->transport();
$this->tags = $tags->count() ? (string) $tags : null;
$this->results = $results;
$this->waitTime = \max(0, $this->receivedAt->getTimestamp() - $this->dispatchedAt->getTimestamp());
$this->handleTime = \max(0, $this->finishedAt->getTimestamp() - $this->receivedAt->getTimestamp());

if ($retryStamp = $envelope->last(RedeliveryStamp::class)) {
$this->attempt += $retryStamp->getRetryCount();
Expand Down Expand Up @@ -143,17 +147,17 @@ final public function isFailure(): bool

final public function timeInQueue(): int
{
return \max(0, $this->receivedAt->getTimestamp() - $this->dispatchedAt->getTimestamp());
return $this->waitTime;
}

final public function timeToHandle(): int
{
return \max(0, $this->finishedAt->getTimestamp() - $this->receivedAt->getTimestamp());
return $this->handleTime;
}

final public function timeToProcess(): int
{
return \max(0, $this->finishedAt->getTimestamp() - $this->dispatchedAt->getTimestamp());
return $this->waitTime + $this->handleTime;
}

final public function memoryUsage(): Bytes
Expand Down
4 changes: 2 additions & 2 deletions src/History/Storage/ORMStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function averageWaitTime(Specification $specification): ?float
{
$qb = $this
->queryBuilderFor($specification, false)
->select('AVG(m.receivedAt - m.dispatchedAt)')
->select('AVG(m.waitTime)')
;

return (new EntityResult($qb))->asFloat()->first();
Expand All @@ -102,7 +102,7 @@ public function averageHandlingTime(Specification $specification): ?float
{
$qb = $this
->queryBuilderFor($specification, false)
->select('AVG(m.finishedAt - m.receivedAt)')
->select('AVG(m.handleTime)')
;

return (new EntityResult($qb))->asFloat()->first();
Expand Down
11 changes: 11 additions & 0 deletions tests/Fixture/Factory/ProcessedMessageFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ protected function initialize(): static
{
return parent::initialize()
->instantiateWith(Instantiator::withoutConstructor()->alwaysForce())
->beforeInstantiate(function(array $attributes) {
if (!isset($attributes['waitTime'])) {
$attributes['waitTime'] = \max(0, $attributes['receivedAt']->getTimestamp() - $attributes['dispatchedAt']->getTimestamp());
}

if (!isset($attributes['processingTime'])) {
$attributes['handleTime'] = \max(0, $attributes['finishedAt']->getTimestamp() - $attributes['receivedAt']->getTimestamp());
}

return $attributes;
})
;
}

Expand Down

0 comments on commit 23cc03e

Please sign in to comment.