Skip to content

Commit 5214413

Browse files
committed
JobType enum
1 parent ba6b3dc commit 5214413

File tree

11 files changed

+89
-53
lines changed

11 files changed

+89
-53
lines changed

src/JobFactory/FullJobDefinition.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,9 @@ protected function getRootDefinition(TreeBuilder $treeBuilder): ArrayNodeDefinit
108108
)
109109
->end()
110110
->end()
111-
->scalarNode('type')
112-
->defaultValue('standard')
113-
->validate()
114-
->ifNotInArray(JobInterface::TYPES_ALL)
115-
->thenInvalid('Type must be one of ' . implode(', ', JobInterface::TYPES_ALL) . '.')
116-
->end()
111+
->enumNode('type')
112+
->defaultValue(JobType::STANDARD->value)
113+
->values(array_map(fn(JobType $t) => $t->value, JobType::cases()))
117114
->end()
118115
->scalarNode('parallelism')
119116
->defaultNull()

src/JobFactory/Job.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,11 @@ public function getExecutor(): Executor
188188
return isset($this->data['executor']) ? Executor::from($this->data['executor']) : Executor::getDefault();
189189
}
190190

191-
public function getType(): string
191+
public function getType(): JobType
192192
{
193-
return $this->data['type'] ?? JobInterface::TYPE_STANDARD;
193+
return isset($this->data['type']) ?
194+
JobType::from($this->data['type']) :
195+
JobType::STANDARD;
194196
}
195197

196198
public function getParallelism(): ?string

src/JobFactory/JobInterface.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,6 @@ interface JobInterface
6262
self::DESIRED_STATUS_TERMINATING,
6363
];
6464

65-
public const TYPE_STANDARD = 'standard';
66-
public const TYPE_ROW_CONTAINER = 'container';
67-
public const TYPE_PHASE_CONTAINER = 'phaseContainer';
68-
public const TYPE_ORCHESTRATION_CONTAINER = 'orchestrationContainer';
69-
70-
public const TYPES_ALL = [
71-
self::TYPE_STANDARD,
72-
self::TYPE_ROW_CONTAINER,
73-
self::TYPE_PHASE_CONTAINER,
74-
self::TYPE_ORCHESTRATION_CONTAINER,
75-
];
76-
7765
public const PARALLELISM_INFINITY = 'infinity';
7866
public const PARALLELISM_ALL = [
7967
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10',
@@ -114,7 +102,7 @@ public function isFinished(): bool;
114102
public function getUsageData(): array;
115103
public function getBackend(): Backend;
116104
public function getExecutor(): Executor;
117-
public function getType(): string;
105+
public function getType(): JobType;
118106
public function getParallelism(): ?string;
119107
public function getBehavior(): Behavior;
120108
public function jsonSerialize(): array;

src/JobFactory/JobRuntimeResolver.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
class JobRuntimeResolver
2424
{
2525
private const JOB_TYPES_WITH_DEFAULT_BACKEND = [
26-
JobInterface::TYPE_STANDARD,
26+
JobType::STANDARD->value,
2727
];
2828

2929
private const PAY_AS_YOU_GO_FEATURE = 'pay-as-you-go';
@@ -53,7 +53,7 @@ public function resolveJobData(array $jobData, array $tokenInfo): array
5353
$jobData['branchType'] = $this->resolveBranchType($jobData)->value;
5454

5555
// set type after resolving parallelism
56-
$jobData['type'] = $this->resolveJobType($jobData);
56+
$jobData['type'] = $this->resolveJobType($jobData)->value;
5757

5858
// set backend after resolving type
5959
$jobData['backend'] = $this->resolveBackend($jobData, $tokenInfo)->toDataArray();
@@ -273,24 +273,24 @@ private function resolveIsForceRunMode(): bool
273273
return isset($this->jobData['mode']) && $this->jobData['mode'] === JobInterface::MODE_FORCE_RUN;
274274
}
275275

276-
private function resolveJobType(array $jobData): string
276+
private function resolveJobType(array $jobData): JobType
277277
{
278278
if (!empty($jobData['type'])) {
279-
return (string) $jobData['type'];
279+
return JobType::from((string) $jobData['type']);
280280
}
281281

282282
if ((intval($jobData['parallelism']) > 0) || $jobData['parallelism'] === JobInterface::PARALLELISM_INFINITY) {
283-
return JobInterface::TYPE_ROW_CONTAINER;
283+
return JobType::ROW_CONTAINER;
284284
} else {
285285
if ($jobData['componentId'] === JobFactory::ORCHESTRATOR_COMPONENT) {
286286
if (isset($jobData['configData']['phaseId']) && (string) ($jobData['configData']['phaseId']) !== '') {
287-
return JobInterface::TYPE_PHASE_CONTAINER;
287+
return JobType::PHASE_CONTAINER;
288288
} else {
289-
return JobInterface::TYPE_ORCHESTRATION_CONTAINER;
289+
return JobType::ORCHESTRATION_CONTAINER;
290290
}
291291
}
292292
}
293-
return JobInterface::TYPE_STANDARD;
293+
return JobType::STANDARD;
294294
}
295295

296296
private function resolveExecutor(array $jobData): Executor

src/JobFactory/JobType.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Keboola\JobQueueInternalClient\JobFactory;
6+
7+
enum JobType: string
8+
{
9+
case STANDARD = 'standard';
10+
case ROW_CONTAINER = 'container';
11+
case PHASE_CONTAINER = 'phaseContainer';
12+
case ORCHESTRATION_CONTAINER = 'orchestrationContainer';
13+
14+
public function isContainer(): bool
15+
{
16+
return $this !== self::STANDARD;
17+
}
18+
}

src/JobListOptions.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use DateTimeInterface;
88
use Keboola\JobQueueInternalClient\Exception\ClientException;
9+
use Keboola\JobQueueInternalClient\JobFactory\JobType;
910

1011
class JobListOptions
1112
{
@@ -34,7 +35,7 @@ class JobListOptions
3435
private string $sortBy;
3536
private string $sortOrder;
3637
private ?string $parentRunId = null;
37-
private string $type;
38+
private JobType $type;
3839

3940
/** @var string */
4041
public const SORT_ORDER_ASC = 'asc';
@@ -65,6 +66,8 @@ public function getQueryParameters(): array
6566
'limit' => 'limit',
6667
'sortBy' => 'sortBy',
6768
'sortOrder' => 'sortOrder',
69+
];
70+
$enumProps = [
6871
'type' => 'type',
6972
];
7073
$scalarPropsWithEmptyValueAllowed = [
@@ -91,6 +94,11 @@ public function getQueryParameters(): array
9194
$parameters[] = $paramName . '=' . urlencode((string) $this->$propName);
9295
}
9396
}
97+
foreach ($enumProps as $propName => $paramName) {
98+
if (!empty($this->$propName)) {
99+
$parameters[] = $paramName . '=' . urlencode((string) $this->$propName->value);
100+
}
101+
}
94102
foreach ($scalarPropsWithEmptyValueAllowed as $propName => $paramName) {
95103
if (isset($this->$propName)) {
96104
$parameters[] = $paramName . '=' . urlencode((string) $this->$propName);
@@ -386,12 +394,12 @@ public function setParentRunId(?string $value): JobListOptions
386394
return $this;
387395
}
388396

389-
public function getType(): string
397+
public function getType(): JobType
390398
{
391399
return $this->type;
392400
}
393401

394-
public function setType(string $type): JobListOptions
402+
public function setType(JobType $type): JobListOptions
395403
{
396404
$this->type = $type;
397405
return $this;

tests/JobFactory/FullJobDefinitionTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,8 @@ public function invalidJobProvider(): array
374374
'mode' => 'run',
375375
'type' => 'orchestration',
376376
],
377-
'#Invalid configuration for path "job.type": Type must be one of standard, container.#',
377+
'#The value "orchestration" is not allowed for path "job.type". Permissible values: ' .
378+
'"standard", "container", "phaseContainer", "orchestrationContainer"#',
378379
],
379380
'Invalid parallelism' => [
380381
[

tests/JobFactory/JobRuntimeResolverTest.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Keboola\JobQueueInternalClient\JobFactory;
1111
use Keboola\JobQueueInternalClient\JobFactory\JobInterface;
1212
use Keboola\JobQueueInternalClient\JobFactory\JobRuntimeResolver;
13+
use Keboola\JobQueueInternalClient\JobFactory\JobType;
1314
use Keboola\StorageApi\BranchAwareClient;
1415
use Keboola\StorageApi\Client;
1516
use Keboola\StorageApi\ClientException as StorageClientException;
@@ -41,15 +42,15 @@ public function resolveDefaultBackendContextData(): Generator
4142
yield 'standard job' => [
4243
'keboola.ex-db-snowflake',
4344
[],
44-
JobInterface::TYPE_STANDARD,
45+
JobType::STANDARD,
4546
'123-dummy-component-type',
4647
];
4748
yield 'row container job' => [
4849
'keboola.ex-db-snowflake',
4950
[
5051
'parallelism' => '5',
5152
],
52-
JobInterface::TYPE_ROW_CONTAINER,
53+
JobType::ROW_CONTAINER,
5354
null,
5455
];
5556
yield 'phase container job' => [
@@ -59,13 +60,13 @@ public function resolveDefaultBackendContextData(): Generator
5960
'phaseId' => '123',
6061
],
6162
],
62-
JobInterface::TYPE_PHASE_CONTAINER,
63+
JobType::PHASE_CONTAINER,
6364
null,
6465
];
6566
yield 'orchestration job' => [
6667
JobFactory::ORCHESTRATOR_COMPONENT,
6768
[],
68-
JobInterface::TYPE_ORCHESTRATION_CONTAINER,
69+
JobType::ORCHESTRATION_CONTAINER,
6970
null,
7071
];
7172
}
@@ -76,7 +77,7 @@ public function resolveDefaultBackendContextData(): Generator
7677
public function testResolveDefaultBackendContext(
7778
string $componentId,
7879
array $customJobData,
79-
string $expectedJobType,
80+
JobType $expectedJobType,
8081
?string $expectedContext,
8182
): void {
8283
$jobData = $this::JOB_DATA;
@@ -113,7 +114,7 @@ public function testResolveDefaultBackendContext(
113114
$jobRuntimeResolver = new JobRuntimeResolver($storageClientFactoryMock);
114115

115116
$jobData = $jobRuntimeResolver->resolveJobData($jobData, ['owner' => ['features' => []]]);
116-
self::assertSame($expectedJobType, $jobData['type']);
117+
self::assertSame($expectedJobType->value, $jobData['type']);
117118
self::assertSame($expectedContext, $jobData['backend']['context']);
118119
}
119120

tests/JobFactory/JobTypeTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Keboola\JobQueueInternalClient\Tests\JobFactory;
6+
7+
use Keboola\JobQueueInternalClient\JobFactory\JobType;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class JobTypeTest extends TestCase
11+
{
12+
public function testIsContainer(): void
13+
{
14+
self::assertFalse(JobType::STANDARD->isContainer());
15+
self::assertTrue(JobType::ROW_CONTAINER->isContainer());
16+
self::assertTrue(JobType::PHASE_CONTAINER->isContainer());
17+
self::assertTrue(JobType::ORCHESTRATION_CONTAINER->isContainer());
18+
}
19+
}

tests/JobListOptionsTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Keboola\JobQueueInternalClient\Exception\ClientException;
88
use Keboola\JobQueueInternalClient\JobFactory\JobInterface;
9+
use Keboola\JobQueueInternalClient\JobFactory\JobType;
910
use Keboola\JobQueueInternalClient\JobListOptions;
1011
use PHPUnit\Framework\TestCase;
1112
use Safe\DateTime;
@@ -29,7 +30,7 @@ public function testGetQueryParameters(): void
2930
$jobListOptions->setStatuses([JobInterface::STATUS_SUCCESS, JobInterface::STATUS_PROCESSING]);
3031
$jobListOptions->setTags(['1.1.1', '1.2.3']);
3132
$jobListOptions->setParentRunId('123');
32-
$jobListOptions->setType(JobInterface::TYPE_STANDARD);
33+
$jobListOptions->setType(JobType::STANDARD);
3334
$jobListOptions->setCreatedTimeFrom(DateTime::createFromFormat('Y-m-d H:i:s', '2022-02-02 1:12:23'));
3435
$jobListOptions->setCreatedTimeTo(DateTime::createFromFormat('Y-m-d H:i:s', '2022-02-20 1:12:23'));
3536
$jobListOptions->setStartTimeFrom(DateTime::createFromFormat('Y-m-d H:i:s', '2021-02-02 1:12:23'));
@@ -62,7 +63,7 @@ public function testGetQueryParameters(): void
6263
);
6364
self::assertSame(['1.1.1', '1.2.3'], $jobListOptions->getTags());
6465
self::assertSame('123', $jobListOptions->getParentRunId());
65-
self::assertSame(JobInterface::TYPE_STANDARD, $jobListOptions->getType());
66+
self::assertSame(JobType::STANDARD, $jobListOptions->getType());
6667
self::assertSame('2022-02-02 01:12:23', $jobListOptions->getCreatedTimeFrom()->format('Y-m-d H:i:s'));
6768
self::assertSame('2022-02-20 01:12:23', $jobListOptions->getCreatedTimeTo()->format('Y-m-d H:i:s'));
6869
self::assertSame('2021-02-02 01:12:23', $jobListOptions->getStartTimeFrom()->format('Y-m-d H:i:s'));

0 commit comments

Comments
 (0)