Skip to content

Commit c8be9b9

Browse files
committed
fix: separate mysql and postgres job status queries
1 parent b02ab50 commit c8be9b9

5 files changed

Lines changed: 94 additions & 40 deletions

File tree

Classes/Domain/AbstractScheduler.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,4 +349,8 @@ protected function validateGroupName(string $groupName): void
349349
public function getConnection(): Connection {
350350
return $this->dbal;
351351
}
352+
353+
public function getStaleJobTimeoutSeconds(): int {
354+
return $this->staleJobTimeoutSecs;
355+
}
352356
}

Classes/Domain/Scheduler.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,6 @@ public function activity(ScheduledJob $job): void;
2626
public function resetStaleJobs(string $groupName): int;
2727

2828
public function getConnection(): Connection;
29+
30+
public function getStaleJobTimeoutSeconds(): int;
2931
}

Classes/Service/JobStatusService.php

Lines changed: 18 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,19 @@
1010
#[Flow\Scope("singleton")]
1111
abstract class JobStatusService {
1212

13+
protected const string TOTAL_COUNT_QUERY = "";
14+
protected const string RUNNING_COUNT_QUERY = "";
15+
protected const string PENDING_COUNT_QUERY = "";
16+
protected const string STALE_COUNT_QUERY = "";
17+
protected const string FAILED_COUNT_QUERY = "";
18+
1319
#[Flow\Inject]
1420
protected Scheduler $scheduler;
1521

1622
public function getTotalJobCount(string $groupName): int {
1723
$tableName = ScheduledJob::TABLE_NAME;
18-
$query = <<<MySQL
19-
SELECT COUNT(*) FROM {$tableName}
20-
WHERE groupname = :groupName
21-
MySQL;
2224
return $this->fetchOne(
23-
$query,
25+
self::TOTAL_COUNT_QUERY,
2426
[
2527
'groupName' => $groupName
2628
],
@@ -32,35 +34,23 @@ public function getTotalJobCount(string $groupName): int {
3234

3335
public function getRunningJobCount(string $groupName): int {
3436
$tableName = ScheduledJob::TABLE_NAME;
35-
$query = <<<MySQL
36-
SELECT COUNT(*) FROM {$tableName}
37-
WHERE running = 1
38-
AND claimed NOT LIKE 'failed(%)'
39-
AND groupname = :groupName
40-
AND activity > NOW() - INTERVAL 2 SECOND
41-
MySQL;
4237
return $this->fetchOne(
43-
$query,
38+
self::RUNNING_COUNT_QUERY,
4439
[
45-
'groupName' => $groupName
40+
'groupName' => $groupName,
41+
'seconds' => $this->scheduler->getStaleJobTimeoutSeconds()
4642
],
4743
[
48-
'groupName' => Types::STRING
44+
'groupName' => Types::STRING,
45+
'seconds' => Types::INTEGER
4946
]
5047
);
5148
}
5249

5350
public function getPendingJobCount(string $groupName): int {
5451
$tableName = ScheduledJob::TABLE_NAME;
55-
$query = <<<MySQL
56-
SELECT COUNT(*) FROM {$tableName}
57-
WHERE ((running = 0
58-
AND claimed = '')
59-
OR running = 2)
60-
AND groupname = :groupName
61-
MySQL;
6252
return $this->fetchOne(
63-
$query,
53+
self::PENDING_COUNT_QUERY,
6454
[
6555
'groupName' => $groupName
6656
],
@@ -70,37 +60,25 @@ public function getPendingJobCount(string $groupName): int {
7060
);
7161
}
7262

73-
public function getStaleJobCount(string $groupName, int $minutes): int {
63+
public function getStaleJobCount(string $groupName): int {
7464
$tableName = ScheduledJob::TABLE_NAME;
75-
$query = <<<MySQL
76-
SELECT COUNT(*) FROM {$tableName}
77-
WHERE running = 1
78-
AND claimed NOT LIKE 'failed(%)'
79-
AND groupname = :groupName
80-
AND activity < NOW() - INTERVAL :minutes MINUTE
81-
MySQL;
8265
return $this->fetchOne(
83-
$query,
66+
self::STALE_COUNT_QUERY,
8467
[
8568
"groupName" => $groupName,
86-
"minutes" => $minutes
69+
"seconds" => $this->scheduler->getStaleJobTimeoutSeconds()
8770
],
8871
[
8972
"groupName" => Types::STRING,
90-
"minutes" => Types::INTEGER
73+
"seconds" => Types::INTEGER
9174
]
9275
);
9376
}
9477

9578
public function getFailedJobCount(string $groupName): int {
9679
$tableName = ScheduledJob::TABLE_NAME;
97-
$query = <<<MySQL
98-
SELECT COUNT(*) FROM {$tableName}
99-
WHERE claimed LIKE 'failed(%)'
100-
AND groupname = :groupName
101-
MySQL;
10280
return $this->fetchOne(
103-
$query,
81+
self::FAILED_COUNT_QUERY,
10482
[
10583
'groupName' => $groupName
10684
],

Classes/Service/MySQLJobStatusService.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,41 @@
44

55
class MySQLJobStatusService extends JobStatusService {
66

7+
protected const string TOTAL_COUNT_QUERY = <<<MySQL
8+
SELECT COUNT(*) FROM netlogix_jobqueue_scheduled_job
9+
WHERE groupname = :groupName
10+
MySQL;
11+
12+
protected const string RUNNING_COUNT_QUERY = <<<MySQL
13+
SELECT COUNT(*) FROM netlogix_jobqueue_scheduled_job
14+
WHERE running = 1
15+
AND claimed NOT LIKE 'failed(%)'
16+
AND groupname = :groupName
17+
AND activity > NOW() - INTERVAL :seconds SECOND
18+
MySQL;
19+
20+
protected const string PENDING_COUNT_QUERY = <<<MySQL
21+
SELECT COUNT(*) FROM netlogix_jobqueue_scheduled_job
22+
WHERE ((running = 0
23+
AND claimed = '')
24+
OR running = 2)
25+
AND groupname = :groupName
26+
MySQL;
27+
28+
protected const string STALE_COUNT_QUERY = <<<MySQL
29+
SELECT COUNT(*) FROM netlogix_jobqueue_scheduled_job
30+
WHERE running = 1
31+
AND claimed NOT LIKE 'failed(%)'
32+
AND groupname = :groupName
33+
AND activity <= NOW() - INTERVAL :seconds SECOND
34+
MySQL;
35+
36+
protected const string FAILED_COUNT_QUERY = <<<MySQL
37+
SELECT COUNT(*) FROM netlogix_jobqueue_scheduled_job
38+
WHERE claimed LIKE 'failed(%)'
39+
AND groupname = :groupName
40+
MySQL;
41+
742
protected function fetchOne(string $query, array $params = [], array $types = []) {
843
return $this->scheduler->getConnection()->fetchOneReadUncommited($query, $params, $types);
944
}

Classes/Service/PostgreSQLJobStatusService.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,39 @@
44

55
class PostgreSQLJobStatusService extends JobStatusService {
66

7+
protected const string TOTAL_COUNT_QUERY = <<<PostgreSQL
8+
SELECT COUNT(*) FROM netlogix_jobqueue_scheduled_job
9+
WHERE groupname = :groupName
10+
PostgreSQL;
11+
12+
protected const string RUNNING_COUNT_QUERY = <<<PostgreSQL
13+
SELECT COUNT(*) FROM netlogix_jobqueue_scheduled_job
14+
WHERE running = 1
15+
AND claimed NOT LIKE 'failed(%)'
16+
AND groupname = :groupName
17+
AND activity > NOW() - make_interval(secs => :seconds)
18+
PostgreSQL;
19+
20+
protected const string PENDING_COUNT_QUERY = <<<PostgreSQL
21+
SELECT COUNT(*) FROM netlogix_jobqueue_scheduled_job
22+
WHERE ((running = 0
23+
AND claimed = '')
24+
OR running = 2)
25+
AND groupname = :groupName
26+
PostgreSQL;
27+
28+
protected const string STALE_COUNT_QUERY = <<<PostgreSQL
29+
SELECT COUNT(*) FROM netlogix_jobqueue_scheduled_job
30+
WHERE running = 1
31+
AND claimed NOT LIKE 'failed(%)'
32+
AND groupname = :groupName
33+
AND activity <= NOW() - make_interval(secs => :seconds)
34+
PostgreSQL;
35+
36+
protected const string FAILED_COUNT_QUERY = <<<PostgreSQL
37+
SELECT COUNT(*) FROM netlogix_jobqueue_scheduled_job
38+
WHERE claimed LIKE 'failed(%)'
39+
AND groupname = :groupName
40+
PostgreSQL;
41+
742
}

0 commit comments

Comments
 (0)