Skip to content

Commit 7e40f04

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

5 files changed

Lines changed: 94 additions & 45 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 & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,18 @@
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 {
17-
$tableName = ScheduledJob::TABLE_NAME;
18-
$query = <<<MySQL
19-
SELECT COUNT(*) FROM {$tableName}
20-
WHERE groupname = :groupName
21-
MySQL;
2223
return $this->fetchOne(
23-
$query,
24+
static::TOTAL_COUNT_QUERY,
2425
[
2526
'groupName' => $groupName
2627
],
@@ -31,36 +32,22 @@ public function getTotalJobCount(string $groupName): int {
3132
}
3233

3334
public function getRunningJobCount(string $groupName): int {
34-
$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;
4235
return $this->fetchOne(
43-
$query,
36+
static::RUNNING_COUNT_QUERY,
4437
[
45-
'groupName' => $groupName
38+
'groupName' => $groupName,
39+
'seconds' => $this->scheduler->getStaleJobTimeoutSeconds()
4640
],
4741
[
48-
'groupName' => Types::STRING
42+
'groupName' => Types::STRING,
43+
'seconds' => Types::INTEGER
4944
]
5045
);
5146
}
5247

5348
public function getPendingJobCount(string $groupName): int {
54-
$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;
6249
return $this->fetchOne(
63-
$query,
50+
static::PENDING_COUNT_QUERY,
6451
[
6552
'groupName' => $groupName
6653
],
@@ -70,37 +57,23 @@ public function getPendingJobCount(string $groupName): int {
7057
);
7158
}
7259

73-
public function getStaleJobCount(string $groupName, int $minutes): int {
74-
$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;
60+
public function getStaleJobCount(string $groupName): int {
8261
return $this->fetchOne(
83-
$query,
62+
static::STALE_COUNT_QUERY,
8463
[
8564
"groupName" => $groupName,
86-
"minutes" => $minutes
65+
"seconds" => $this->scheduler->getStaleJobTimeoutSeconds()
8766
],
8867
[
8968
"groupName" => Types::STRING,
90-
"minutes" => Types::INTEGER
69+
"seconds" => Types::INTEGER
9170
]
9271
);
9372
}
9473

9574
public function getFailedJobCount(string $groupName): int {
96-
$tableName = ScheduledJob::TABLE_NAME;
97-
$query = <<<MySQL
98-
SELECT COUNT(*) FROM {$tableName}
99-
WHERE claimed LIKE 'failed(%)'
100-
AND groupname = :groupName
101-
MySQL;
10275
return $this->fetchOne(
103-
$query,
76+
static::FAILED_COUNT_QUERY,
10477
[
10578
'groupName' => $groupName
10679
],

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)