Skip to content

Commit b72ae92

Browse files
Merge pull request #56 from cleverage/54
#54 When Launch process via http request, add queue parameter which d…
2 parents 4dc9315 + 9beb8b5 commit b72ae92

File tree

5 files changed

+46
-16
lines changed

5 files changed

+46
-16
lines changed

config/services/controller.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ services:
1414
$processExecutionRepository: '@cleverage_ui_process.repository.process_execution'
1515
$intlFormatter: '@EasyCorp\Bundle\EasyAdminBundle\Intl\IntlFormatter'
1616
$translator: '@translator'
17+
$processManager: '@cleverage_process.manager.process'
1718
tags:
1819
- { name: 'controller.service_arguments' }

docs/index.md

+12-4
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ That's all, now you can launch a process via http post request
7575
```bash
7676
curl --location 'http://localhost/http/process/execute' \
7777
--header 'Authorization: Bearer myBearerToken' \
78-
--form 'input=@"/path/to/your/file.csv"' \
79-
--form 'code="demo.dummy"' \
80-
--form 'context="{\"foo\": \"bar\"}"'
78+
--form 'code="demo.upload_and_run"' \
79+
--form 'input="/path/to/your/file.csv"' \
80+
--form 'context="{\"foo\": \"bar\", \"delimiter\": \";\"}"'
8181
```
8282

8383
```bash
@@ -91,10 +91,18 @@ curl --location 'http://localhost/http/process/execute' \
9191
}'
9292
```
9393

94+
```bash
95+
curl --location 'http://localhost/http/process/execute' \
96+
--header 'Authorization: Bearer myBearerToken' \
97+
--form 'code="demo.dummy"' \
98+
--form 'queue="false"'
99+
```
100+
94101
* Query string code parameter must be a valid process code
95102
* Header Authorization: Bearer is the previously generated token
96103
* input could be string or file representation
97-
* context you can pass multiple context values
104+
* you can pass multiple context values
105+
* queue define if the process should be queued (default) or directly run
98106

99107

100108
### Scheduler

src/Controller/ProcessExecuteController.php

+29-10
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313

1414
namespace CleverAge\UiProcessBundle\Controller;
1515

16+
use CleverAge\ProcessBundle\Manager\ProcessManager;
1617
use CleverAge\UiProcessBundle\Http\Model\HttpProcessExecution;
1718
use CleverAge\UiProcessBundle\Message\ProcessExecuteMessage;
1819
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1920
use Symfony\Component\HttpFoundation\JsonResponse;
21+
use Symfony\Component\HttpFoundation\Response;
2022
use Symfony\Component\HttpKernel\Attribute\ValueResolver;
2123
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
2224
use Symfony\Component\Messenger\MessageBusInterface;
@@ -30,6 +32,7 @@ public function __invoke(
3032
#[ValueResolver('http_process_execution')] HttpProcessExecution $httpProcessExecution,
3133
ValidatorInterface $validator,
3234
MessageBusInterface $bus,
35+
ProcessManager $processManager,
3336
): JsonResponse {
3437
$violations = $validator->validate($httpProcessExecution);
3538
if ($violations->count() > 0) {
@@ -39,16 +42,32 @@ public function __invoke(
3942
}
4043
throw new UnprocessableEntityHttpException(implode('. ', $violationsMessages));
4144
}
42-
$bus->dispatch(
43-
new ProcessExecuteMessage(
44-
$httpProcessExecution->code ?? '',
45-
$httpProcessExecution->input,
46-
\is_string($httpProcessExecution->context)
47-
? json_decode($httpProcessExecution->context, true)
48-
: $httpProcessExecution->context
49-
)
50-
);
45+
if ($httpProcessExecution->queue) {
46+
$bus->dispatch(
47+
new ProcessExecuteMessage(
48+
$httpProcessExecution->code ?? '',
49+
$httpProcessExecution->input,
50+
\is_string($httpProcessExecution->context)
51+
? json_decode($httpProcessExecution->context, true)
52+
: $httpProcessExecution->context
53+
)
54+
);
5155

52-
return new JsonResponse('Process has been added to queue. It will start as soon as possible.');
56+
return new JsonResponse('Process has been added to queue. It will start as soon as possible.');
57+
} else {
58+
try {
59+
$processManager->execute(
60+
$httpProcessExecution->code ?? '',
61+
$httpProcessExecution->input,
62+
\is_string($httpProcessExecution->context)
63+
? json_decode($httpProcessExecution->context, true)
64+
: $httpProcessExecution->context
65+
);
66+
} catch (\Throwable $e) {
67+
return new JsonResponse($e->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
68+
}
69+
70+
return new JsonResponse('Process has been proceed well.');
71+
}
5372
}
5473
}

src/Http/Model/HttpProcessExecution.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@
2323
final readonly class HttpProcessExecution
2424
{
2525
/**
26-
* @param array<string|int, mixed> $context
26+
* @param string|array<string|int, mixed> $context
2727
*/
2828
public function __construct(
2929
#[Sequentially(constraints: [new NotNull(message: 'Process code is required.'), new IsValidProcessCode()])]
3030
public ?string $code = null,
3131
public ?string $input = null,
3232
#[AtLeastOneOf(constraints: [new Json(), new Type('array')])]
3333
public string|array $context = [],
34+
public bool $queue = true,
3435
) {
3536
}
3637
}

src/Http/ValueResolver/HttpProcessExecuteValueResolver.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ public function resolve(Request $request, ArgumentMetadata $argument): iterable
5252
$httpProcessExecution = new HttpProcessExecution(
5353
$request->get('code'),
5454
$input,
55-
$request->get('context', [])
55+
$request->get('context', []),
56+
$request->request->getBoolean('queue', true),
5657
);
5758
}
5859

0 commit comments

Comments
 (0)