66use SebastianBergmann \Type \VoidType ;
77use Illuminate \Support \Facades \Redis ;
88use HyssaDev \HibikenAsynqClient \Client ;
9+ use Exception ;
910
10- class work
11+ class work extends adminator
1112{
1213 // DI
13- protected \Monolog \Logger $ logger ;
14+ public \Monolog \Logger $ logger ;
1415
15- protected \mysqli |\PDO $ conn_mysql ;
16+ public \mysqli |\PDO $ conn_mysql ;
1617
17- protected \PgSql \Connection |\PDO |null $ conn_pgsql ;
18+ public \PgSql \Connection |\PDO |null $ conn_pgsql ;
19+
20+ public \PDO |null $ pdoMysql ;
1821
1922 protected $ sentinel ;
2023
@@ -29,12 +32,17 @@ class work
2932 */
3033 public array $ p_bs_alerts = [];
3134
35+ public $ action_form ;
36+
37+ public int $ form_single_action ;
38+
3239 public function __construct (ContainerInterface $ container )
3340 {
3441 // $this->container = $container;
3542 $ this ->logger = $ container ->get ('logger ' );
3643 $ this ->conn_mysql = $ container ->get ('connMysql ' );
3744 $ this ->conn_pgsql = $ container ->get ('connPgsql ' );
45+ $ this ->pdoMysql = $ container ->get ('pdoMysql ' );
3846
3947 $ this ->sentinel = $ container ->get ('sentinel ' );
4048
@@ -46,6 +54,49 @@ public function __construct(ContainerInterface $container)
4654 $ this ->logger ->info (message: __CLASS__ . "\\" . __FUNCTION__ . " called " );
4755 }
4856
57+ public function callPdoQueryAndFetch ($ query ): array
58+ {
59+ $ rs_error = null ;
60+ try {
61+ $ rs = $ this ->pdoMysql ->query ($ query );
62+ } catch (Exception $ e ) {
63+ $ rs_error = $ e ->getMessage ();
64+ }
65+
66+ if (is_object ($ rs )) {
67+ $ rs_data = $ rs ->fetchAll ();
68+
69+ } else {
70+ $ this ->logger ->error (__CLASS__ . "\\" . __FUNCTION__ . ": PDO result is not object " );
71+ $ rs_data = [];
72+ }
73+
74+ return [$ rs_data , $ rs_error ];
75+ }
76+
77+ public function getAllItems (): array
78+ {
79+ $ q = "SELECT id, name FROM workitems_names WHERE id > 0 ORDER BY id " ;
80+ list ($ data_rs , $ dotaz_error ) = $ this ->callPdoQueryAndFetch ($ q );
81+
82+ if ($ dotaz_error != null ) {
83+ $ this ->logger ->error (__CLASS__ . "\\" . __FUNCTION__ . ": Caught Exception: " . var_export ($ dotaz_error , true ));
84+ $ this ->p_bs_alerts ["Nelze načíst data pro výpis akcí pro manuální restart. <br>(SQL error: $ dotaz_error) " ] = "danger " ;
85+
86+ return [false , []];
87+
88+ } elseif (count ($ data_rs ) < 1 ) {
89+ $ this ->p_bs_alerts ["Žádné data pro výpis akcí pro manuální restart. " ] = "warning " ;
90+
91+ return [true , []];
92+ } else {
93+ foreach ($ data_rs as $ key => $ val ) {
94+ $ itemsList [] = ["id " => $ val ["id " ], "name " => $ val ["name " ]];
95+ }
96+ return [true , $ itemsList ];
97+ }
98+ }
99+
49100 public function getItemName (int $ id ): string |null
50101 {
51102 $ rs_item_name = $ this ->conn_mysql ->query ("SELECT name FROM workitems_names WHERE id = ' $ id' " );
@@ -56,7 +107,43 @@ public function getItemName(int $id): string|null
56107 return $ item_name ;
57108 }
58109
59- public function taskEnqueue (int $ item_id ): bool |int
110+ public function handleSingleActionForm (): void
111+ {
112+ $ this ->logger ->info (__CLASS__ . "\\" . __FUNCTION__ . " called " );
113+
114+ // get form data
115+ $ this ->action_form = $ this ->formInit ();
116+ $ form_data = $ this ->action_form ->validate ("single_action " );
117+ $ this ->form_single_action = intval ($ form_data ["single_action " ]);
118+
119+ $ this ->logger ->debug (__CLASS__ . "\\" . __FUNCTION__ . ": " . var_export ($ this ->form_single_action , true ));
120+
121+ // check if form was sended
122+ if ($ this ->form_single_action > 0 ) {
123+ // test if we have valid ID
124+ $ item_name = $ this ->getItemName ($ this ->form_single_action );
125+
126+ if (is_null ($ item_name )) {
127+ $ this ->logger ->warning (message: __CLASS__ . "\\" . __FUNCTION__ . ": parsing item_name failed (item_id $ this ->form_single_action ) " );
128+ } else {
129+ [$ queue_rs , $ queue_err ] = $ this ->taskEnqueue ($ this ->form_single_action );
130+ if ($ queue_rs ) {
131+ $ this ->p_bs_alerts ["Manuální přidání akce pro restart bylo provedeno úspěšně " ] = "success " ;
132+ } else {
133+ $ this ->p_bs_alerts ["Manuální přidání akce pro restart selhalo. </br> ( $ queue_err) " ] = "danger " ;
134+ $ this ->logger ->error (message: __CLASS__ . "\\" . __FUNCTION__ . ": single_action failed ( $ queue_err) " );
135+ }
136+ }
137+ }
138+ }
139+
140+ /**
141+ * @return array [
142+ * bool, // false if something failed
143+ * bool|int|string, // results from asynq_client or error message
144+ * ]
145+ */
146+ public function taskEnqueue (int $ item_id ): array
60147 {
61148 $ this ->logger ->info (__CLASS__ . "\\" . __FUNCTION__ . " called " );
62149
@@ -79,10 +166,10 @@ public function taskEnqueue(int $item_id): bool|int
79166 } catch (\RedisException $ ex ) {
80167 $ m = $ ex ->getMessage ();
81168 $ this ->logger ->error (__CLASS__ . "\\" . __FUNCTION__ . ": Redis error: $ m " );
82- return false ;
169+ return [ false , " Redis error: $ m " ] ;
83170 }
84171
85- return $ res ;
172+ return [ true , $ res] ;
86173 }
87174
88175 public function taskGroupList (): array
@@ -144,17 +231,18 @@ public function work_handler($item_id): array
144231 $ item_name = $ this ->getItemName ($ item_id );
145232
146233 if (is_null ($ item_name )) {
147- //TODO: add warning over bootstrap.JS
234+ // TODO: check/fix rendering in objekty/topology page(s)
235+ $ this ->p_bs_alerts ["Nepodařilo se načíst název WorkItem položky. <br>(item_id: $ item_id) " ] = "warning " ;
148236 $ this ->logger ->warning (message: __CLASS__ . "\\" . __FUNCTION__ . ": parsing item_name failed (item_id $ item_id) " );
149237 } else {
150238 $ this ->logger ->info (message: __CLASS__ . "\\" . __FUNCTION__ . ": parsed item_name: " . var_export ($ item_name , true ));
151239 }
152240
153241 // asynqClient part
154- $ rs_queue = $ this ->taskEnqueue ($ item_id );
155- $ this ->logger ->debug (__CLASS__ . "\\" . __FUNCTION__ . ": rs_queue: " . var_export ($ rs_queue , true ));
242+ [ $ queue_rs , $ queue_err ] = $ this ->taskEnqueue ($ item_id );
243+ $ this ->logger ->debug (__CLASS__ . "\\" . __FUNCTION__ . ": rs_queue: " . var_export ($ queue_rs , true ));
156244
157- if ($ rs_queue ) {
245+ if ($ queue_rs ) {
158246 $ rs_write = 1 ;
159247 } else {
160248 $ rs_write = 0 ;
@@ -172,7 +260,7 @@ public function work_handler($item_id): array
172260 // generate output view
173261 $ output .= "<div style= \"\">Požadavek na restart <b> \"" .$ item_name ."\"</b> (No. " .$ item_id .") " ;
174262
175- if ($ rs_queue ) {
263+ if ($ queue_rs ) {
176264 $ output .= "<div> - <span style= \"color: green; \"> úspěšně přidán do fronty</span></div> " ;
177265 } else {
178266 $ output .= "<div> - <span style= \"color: red; \"> chyba při přidání požadavku do fronty</span></div> " ;
0 commit comments