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

Queue job profiling #13796

Open
wants to merge 1 commit into
base: 4.14
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
'id' => 'CraftCMS',
'name' => 'Craft CMS',
'version' => '4.5.6.1',
'schemaVersion' => '4.5.3.0',
'schemaVersion' => '4.6.0.0',
'minVersionRequired' => '3.7.11',
'basePath' => dirname(__DIR__), // Defines the @app alias
'runtimePath' => '@storage/runtime', // Defines the @runtime alias
Expand Down
8 changes: 7 additions & 1 deletion src/controllers/QueueController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Craft;
use craft\helpers\App;
use craft\helpers\DateTimeHelper;
use craft\helpers\Json;
use craft\queue\QueueInterface;
use craft\web\Controller;
Expand Down Expand Up @@ -174,7 +175,12 @@ public function actionGetJobInfo(): Response

return $this->asJson([
'total' => $this->queue->getTotalJobs(),
'jobs' => $this->queue->getJobInfo($limit),
'jobs' => array_map(function(array $info) {
if (isset($info['averageDuration'])) {
$info['averageDurationLabel'] = DateTimeHelper::humanDuration($info['averageDuration']);
}
return $info;
}, $this->queue->getJobInfo($limit)),
]);
}

Expand Down
2 changes: 2 additions & 0 deletions src/db/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ abstract class Table
/** @since 3.4.0 */
public const PROJECTCONFIG = '{{%projectconfig}}';
public const QUEUE = '{{%queue}}';
/** @since 4.6.0 */
public const QUEUEPROFILES = '{{%queueprofiles}}';
public const RELATIONS = '{{%relations}}';
public const SECTIONS = '{{%sections}}';
public const SECTIONS_SITES = '{{%sections_sites}}';
Expand Down
6 changes: 6 additions & 0 deletions src/migrations/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,12 @@ public function createTables(): void
'dateFailed' => $this->dateTime(),
'error' => $this->text(),
]);
$this->createTable(Table::QUEUEPROFILES, [
'key' => $this->string(),
'dateExecuted' => $this->dateTime()->notNull(),
'duration' => $this->integer()->notNull(),
'PRIMARY KEY([[key]], [[dateExecuted]])',
]);
$this->createTable(Table::RELATIONS, [
'id' => $this->primaryKey(),
'fieldId' => $this->integer()->notNull(),
Expand Down
36 changes: 36 additions & 0 deletions src/migrations/m231006_071504_job_profiling.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace craft\migrations;

use craft\db\Migration;
use craft\db\Table;

/**
* m231006_071504_job_profiling migration.
*/
class m231006_071504_job_profiling extends Migration
{
/**
* @inheritdoc
*/
public function safeUp(): bool
{
$this->createTable(Table::QUEUEPROFILES, [
'key' => $this->string(),
'dateExecuted' => $this->dateTime()->notNull(),
'duration' => $this->integer()->notNull(),
'PRIMARY KEY([[key]], [[dateExecuted]])',
]);

return true;
}

/**
* @inheritdoc
*/
public function safeDown(): bool
{
echo "m231006_071504_job_profiling cannot be reverted.\n";
return false;
}
}
13 changes: 12 additions & 1 deletion src/queue/BaseBatchedJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Craft;
use craft\base\Batchable;
use craft\helpers\ArrayHelper;
use craft\helpers\ConfigHelper;
use craft\helpers\Queue as QueueHelper;
use craft\i18n\Translation;
Expand All @@ -31,7 +32,7 @@
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 4.4.0
*/
abstract class BaseBatchedJob extends BaseJob
abstract class BaseBatchedJob extends BaseJob implements ProfilableJobInterface
{
/**
* @var int The number of items that should be processed in a single batch
Expand Down Expand Up @@ -172,4 +173,14 @@ final public function getDescription(): ?string
'total' => $totalBatches,
]);
}

/**
* @inheritdoc
*/
public function getProfileAttributes(): array
{
$arr = ArrayHelper::toArray($this);
unset($arr['batchIndex'], $arr['itemOffset'], $arr['priority'], $arr['ttr']);
return $arr;
}
}
27 changes: 27 additions & 0 deletions src/queue/ProfilableJobInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/

namespace craft\queue;

/**
* ProfilableJobInterface aids in aggregating profile results for queue jobs.
*
* It should be implemented by jobs which could have wide-varying runtime lengths based on their configuration.
*
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 4.6.0
*/
interface ProfilableJobInterface
{
/**
* Returns the job attributes that should be used to semi-uniquely identify this job, based on its configuration,
* so that its profile results can be compared with recent similarly-configured jobs.
*
* @return array
*/
public function getProfileAttributes(): array;
}
Loading
Loading