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

#54 When Launch process via http request, add queue parameter which d… #56

Merged
merged 1 commit into from
Mar 20, 2025
Merged
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
1 change: 1 addition & 0 deletions config/services/controller.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ services:
$processExecutionRepository: '@cleverage_ui_process.repository.process_execution'
$intlFormatter: '@EasyCorp\Bundle\EasyAdminBundle\Intl\IntlFormatter'
$translator: '@translator'
$processManager: '@cleverage_process.manager.process'
tags:
- { name: 'controller.service_arguments' }
16 changes: 12 additions & 4 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ That's all, now you can launch a process via http post request
```bash
curl --location 'http://localhost/http/process/execute' \
--header 'Authorization: Bearer myBearerToken' \
--form 'input=@"/path/to/your/file.csv"' \
--form 'code="demo.dummy"' \
--form 'context="{\"foo\": \"bar\"}"'
--form 'code="demo.upload_and_run"' \
--form 'input="/path/to/your/file.csv"' \
--form 'context="{\"foo\": \"bar\", \"delimiter\": \";\"}"'
```

```bash
Expand All @@ -91,10 +91,18 @@ curl --location 'http://localhost/http/process/execute' \
}'
```

```bash
curl --location 'http://localhost/http/process/execute' \
--header 'Authorization: Bearer myBearerToken' \
--form 'code="demo.dummy"' \
--form 'queue="false"'
```

* Query string code parameter must be a valid process code
* Header Authorization: Bearer is the previously generated token
* input could be string or file representation
* context you can pass multiple context values
* you can pass multiple context values
* queue define if the process should be queued (default) or directly run


### Scheduler
Expand Down
39 changes: 29 additions & 10 deletions src/Controller/ProcessExecuteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@

namespace CleverAge\UiProcessBundle\Controller;

use CleverAge\ProcessBundle\Manager\ProcessManager;
use CleverAge\UiProcessBundle\Http\Model\HttpProcessExecution;
use CleverAge\UiProcessBundle\Message\ProcessExecuteMessage;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\ValueResolver;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
use Symfony\Component\Messenger\MessageBusInterface;
Expand All @@ -30,6 +32,7 @@ public function __invoke(
#[ValueResolver('http_process_execution')] HttpProcessExecution $httpProcessExecution,
ValidatorInterface $validator,
MessageBusInterface $bus,
ProcessManager $processManager,
): JsonResponse {
$violations = $validator->validate($httpProcessExecution);
if ($violations->count() > 0) {
Expand All @@ -39,16 +42,32 @@ public function __invoke(
}
throw new UnprocessableEntityHttpException(implode('. ', $violationsMessages));
}
$bus->dispatch(
new ProcessExecuteMessage(
$httpProcessExecution->code ?? '',
$httpProcessExecution->input,
\is_string($httpProcessExecution->context)
? json_decode($httpProcessExecution->context, true)
: $httpProcessExecution->context
)
);
if ($httpProcessExecution->queue) {
$bus->dispatch(
new ProcessExecuteMessage(
$httpProcessExecution->code ?? '',
$httpProcessExecution->input,
\is_string($httpProcessExecution->context)
? json_decode($httpProcessExecution->context, true)
: $httpProcessExecution->context
)
);

return new JsonResponse('Process has been added to queue. It will start as soon as possible.');
return new JsonResponse('Process has been added to queue. It will start as soon as possible.');
} else {
try {
$processManager->execute(
$httpProcessExecution->code ?? '',
$httpProcessExecution->input,
\is_string($httpProcessExecution->context)
? json_decode($httpProcessExecution->context, true)
: $httpProcessExecution->context
);
} catch (\Throwable $e) {
return new JsonResponse($e->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
}

return new JsonResponse('Process has been proceed well.');
}
}
}
3 changes: 2 additions & 1 deletion src/Http/Model/HttpProcessExecution.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@
final readonly class HttpProcessExecution
{
/**
* @param array<string|int, mixed> $context
* @param string|array<string|int, mixed> $context
*/
public function __construct(
#[Sequentially(constraints: [new NotNull(message: 'Process code is required.'), new IsValidProcessCode()])]
public ?string $code = null,
public ?string $input = null,
#[AtLeastOneOf(constraints: [new Json(), new Type('array')])]
public string|array $context = [],
public bool $queue = true,
) {
}
}
3 changes: 2 additions & 1 deletion src/Http/ValueResolver/HttpProcessExecuteValueResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public function resolve(Request $request, ArgumentMetadata $argument): iterable
$httpProcessExecution = new HttpProcessExecution(
$request->get('code'),
$input,
$request->get('context', [])
$request->get('context', []),
$request->request->getBoolean('queue', true),
);
}

Expand Down