Skip to content

Commit a9cdfb5

Browse files
committed
feat(TestRuns): clean up db after recieving test run update
1 parent bddac40 commit a9cdfb5

File tree

3 files changed

+90
-5
lines changed

3 files changed

+90
-5
lines changed

includes/features/class-service.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,18 @@ public static function fetch_updates() {
315315
return self::rest_service_request( $service_api_route, [], 'get' );
316316
}
317317

318+
/**
319+
* Get test runs from the service.
320+
*
321+
* @param string[] $test_run_ids the test run ids.
322+
*/
323+
public static function fetch_test_runs( $test_run_ids ) {
324+
$service_project_id = get_option( 'vrts_project_id' );
325+
$service_api_route = 'sites/' . $service_project_id . '/runs';
326+
$service_api_route = add_query_arg( 'ids', implode( ',', $test_run_ids ), $service_api_route );
327+
return self::rest_service_request( $service_api_route, [], 'get' );
328+
}
329+
318330
/**
319331
* Delete project from the service.
320332
*/

includes/models/class-test-run.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,23 @@ public static function delete_duplicates() {
273273
);
274274
}
275275

276+
/**
277+
* Delete empty test runs from database.
278+
*
279+
* @return void
280+
*/
281+
public static function delete_empty() {
282+
global $wpdb;
283+
284+
$test_runs_table = Test_Runs_Table::get_table_name();
285+
286+
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- It's ok.
287+
$wpdb->query(
288+
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- It's ok.
289+
"DELETE FROM $test_runs_table WHERE service_test_run_id IS NULL"
290+
);
291+
}
292+
276293
/**
277294
* Insert multiple test data
278295
*
@@ -591,4 +608,26 @@ public static function get_next_scheduled_run() {
591608
);
592609
return $test_run;
593610
}
611+
612+
/**
613+
* Get test run by service test run id
614+
*
615+
* @return mixed
616+
*/
617+
public static function get_stalled_test_run_ids() {
618+
global $wpdb;
619+
620+
$test_runs_table = Test_Runs_Table::get_table_name();
621+
622+
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- It's ok.
623+
$test_runs = $wpdb->get_results(
624+
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- It's ok.
625+
"SELECT service_test_run_id FROM $test_runs_table
626+
WHERE ( finished_at IS NULL AND started_at IS NULL AND scheduled_at < DATE_SUB( now(), INTERVAL 1 HOUR ) )
627+
OR ( finished_at IS NULL AND started_at IS NOT NULL AND started_at < DATE_SUB( NOW(), INTERVAL 1 HOUR ) )
628+
OR ( finished_at IS NULL AND started_at IS NULL AND scheduled_at IS NULL )
629+
"
630+
);
631+
return $test_runs;
632+
}
594633
}

includes/services/class-test-run-service.php

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,17 @@ class Test_Run_Service {
1414
* Create test from API data.
1515
*
1616
* @param array $data Data.
17+
* @param bool $with_cleanup With cleanup.
1718
*
1819
* @return boolean
1920
*/
20-
public function update_run_from_api_data( $data ) {
21+
public function update_run_from_api_data( $data, $with_cleanup = true ) {
2122
$run_id = $data['run_id'];
23+
24+
if ( empty( $run_id ) ) {
25+
return false;
26+
}
27+
2228
$test_run = Test_Run::get_by_service_test_run_id( $run_id );
2329

2430
$test_run_just_finished = false;
@@ -45,7 +51,7 @@ public function update_run_from_api_data( $data ) {
4551
'trigger' => $data['trigger'],
4652
'trigger_notes' => $data['trigger_notes'],
4753
'trigger_meta' => maybe_serialize( $data['trigger_meta'] ),
48-
], true);
54+
], true, $with_cleanup );
4955

5056
if ( $test_run_just_finished && ! empty( $alert_ids ) ) {
5157
$email_service = new Email_Service();
@@ -91,10 +97,11 @@ protected function update_tests_and_create_alerts( $comparisons, $test_run ) {
9197
* @param string $service_test_run_id Service test run id.
9298
* @param array $data Data.
9399
* @param bool $update Update.
100+
* @param bool $with_cleanup With cleanup.
94101
*
95102
* @return boolean
96103
*/
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 ) {
98105
$test_run = Test_Run::get_by_service_test_run_id( $service_test_run_id );
99106

100107
if ( $test_run && ! $update ) {
@@ -103,10 +110,37 @@ public function create_test_run( $service_test_run_id, $data, $update = false )
103110
$test_run_id = Test_Run::save(array_merge( $data, [
104111
'service_test_run_id' => $service_test_run_id,
105112
]), $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+
}
107118
return $test_run_id;
108119
}
109120

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+
110144
/**
111145
* Fetch and update tests.
112146
*
@@ -119,7 +153,7 @@ public function fetch_and_update_test_runs() {
119153
if ( array_key_exists( 'run_updates', $response ) ) {
120154
$updates = $response['run_updates'];
121155
foreach ( $updates as $update ) {
122-
$this->update_run_from_api_data( $update );
156+
$this->update_run_from_api_data( $update, false );
123157
}
124158
}
125159
if (

0 commit comments

Comments
 (0)