13
13
14
14
namespace CleverAge \UiProcessBundle \Controller ;
15
15
16
+ use CleverAge \ProcessBundle \Manager \ProcessManager ;
16
17
use CleverAge \UiProcessBundle \Http \Model \HttpProcessExecution ;
17
18
use CleverAge \UiProcessBundle \Message \ProcessExecuteMessage ;
18
19
use Symfony \Bundle \FrameworkBundle \Controller \AbstractController ;
19
20
use Symfony \Component \HttpFoundation \JsonResponse ;
21
+ use Symfony \Component \HttpFoundation \Response ;
20
22
use Symfony \Component \HttpKernel \Attribute \ValueResolver ;
21
23
use Symfony \Component \HttpKernel \Exception \UnprocessableEntityHttpException ;
22
24
use Symfony \Component \Messenger \MessageBusInterface ;
@@ -30,6 +32,7 @@ public function __invoke(
30
32
#[ValueResolver('http_process_execution ' )] HttpProcessExecution $ httpProcessExecution ,
31
33
ValidatorInterface $ validator ,
32
34
MessageBusInterface $ bus ,
35
+ ProcessManager $ processManager ,
33
36
): JsonResponse {
34
37
$ violations = $ validator ->validate ($ httpProcessExecution );
35
38
if ($ violations ->count () > 0 ) {
@@ -39,16 +42,32 @@ public function __invoke(
39
42
}
40
43
throw new UnprocessableEntityHttpException (implode ('. ' , $ violationsMessages ));
41
44
}
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
+ );
51
55
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
+ }
53
72
}
54
73
}
0 commit comments