@@ -14,11 +14,17 @@ class Test_Run_Service {
14
14
* Create test from API data.
15
15
*
16
16
* @param array $data Data.
17
+ * @param bool $with_cleanup With cleanup.
17
18
*
18
19
* @return boolean
19
20
*/
20
- public function update_run_from_api_data ( $ data ) {
21
+ public function update_run_from_api_data ( $ data, $ with_cleanup = true ) {
21
22
$ run_id = $ data ['run_id ' ];
23
+
24
+ if ( empty ( $ run_id ) ) {
25
+ return false ;
26
+ }
27
+
22
28
$ test_run = Test_Run::get_by_service_test_run_id ( $ run_id );
23
29
24
30
$ test_run_just_finished = false ;
@@ -45,7 +51,7 @@ public function update_run_from_api_data( $data ) {
45
51
'trigger ' => $ data ['trigger ' ],
46
52
'trigger_notes ' => $ data ['trigger_notes ' ],
47
53
'trigger_meta ' => maybe_serialize ( $ data ['trigger_meta ' ] ),
48
- ], true );
54
+ ], true , $ with_cleanup );
49
55
50
56
if ( $ test_run_just_finished && ! empty ( $ alert_ids ) ) {
51
57
$ email_service = new Email_Service ();
@@ -91,10 +97,11 @@ protected function update_tests_and_create_alerts( $comparisons, $test_run ) {
91
97
* @param string $service_test_run_id Service test run id.
92
98
* @param array $data Data.
93
99
* @param bool $update Update.
100
+ * @param bool $with_cleanup With cleanup.
94
101
*
95
102
* @return boolean
96
103
*/
97
- public function create_test_run ( $ service_test_run_id , $ data , $ update = false ) {
104
+ public function create_test_run ( $ service_test_run_id , $ data , $ update = false , $ with_cleanup = true ) {
98
105
$ test_run = Test_Run::get_by_service_test_run_id ( $ service_test_run_id );
99
106
100
107
if ( $ test_run && ! $ update ) {
@@ -103,10 +110,37 @@ public function create_test_run( $service_test_run_id, $data, $update = false )
103
110
$ test_run_id = Test_Run::save (array_merge ( $ data , [
104
111
'service_test_run_id ' => $ service_test_run_id ,
105
112
]), $ test_run ->id ?? null );
106
- Test_Run::delete_duplicates ();
113
+ if ( $ with_cleanup ) {
114
+ Test_Run::delete_duplicates ();
115
+ Test_Run::delete_empty ();
116
+ $ this ->check_stalled_test_runs ();
117
+ }
107
118
return $ test_run_id ;
108
119
}
109
120
121
+ /**
122
+ * Check stalled test runs.
123
+ *
124
+ * @return void
125
+ */
126
+ public function check_stalled_test_runs () {
127
+ $ test_run_ids = array_column ( Test_Run::get_stalled_test_run_ids (), 'service_test_run_id ' );
128
+ if ( empty ( $ test_run_ids ) ) {
129
+ return ;
130
+ }
131
+ $ response = Service::fetch_test_runs ( $ test_run_ids );
132
+ if ( 200 === $ response ['status_code ' ] ) {
133
+ $ test_runs = $ response ['response ' ]['data ' ] ?? [];
134
+ foreach ( $ test_runs as $ test_run ) {
135
+ $ this ->update_run_from_api_data ( $ test_run , false );
136
+ }
137
+ $ missing_test_run_ids = array_diff ( $ test_run_ids , array_column ( $ test_runs , 'run_id ' ) );
138
+ foreach ( $ missing_test_run_ids as $ missing_test_run_id ) {
139
+ Test_Run::delete_by_service_test_run_id ( $ missing_test_run_id );
140
+ }
141
+ }
142
+ }
143
+
110
144
/**
111
145
* Fetch and update tests.
112
146
*
@@ -119,7 +153,7 @@ public function fetch_and_update_test_runs() {
119
153
if ( array_key_exists ( 'run_updates ' , $ response ) ) {
120
154
$ updates = $ response ['run_updates ' ];
121
155
foreach ( $ updates as $ update ) {
122
- $ this ->update_run_from_api_data ( $ update );
156
+ $ this ->update_run_from_api_data ( $ update, false );
123
157
}
124
158
}
125
159
if (
0 commit comments