Use REST API in heartbeat callback #130
This commit is contained in:
parent
fae9d42831
commit
3d8939e778
|
@ -55,6 +55,10 @@ class REST_Background_Processes_Controller extends REST_Controller {
|
||||||
'paged' => [
|
'paged' => [
|
||||||
'type' => 'integer',
|
'type' => 'integer',
|
||||||
'description' => __( 'Page to retrieve. Default 1', 'tainacan' ),
|
'description' => __( 'Page to retrieve. Default 1', 'tainacan' ),
|
||||||
|
],
|
||||||
|
'recent' => [
|
||||||
|
'type' => 'bool',
|
||||||
|
'description' => __( 'Return only processes created or updated recently', 'tainacan' ),
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
|
@ -151,7 +155,12 @@ class REST_Background_Processes_Controller extends REST_Controller {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$base_query = "FROM $this->table WHERE 1=1 $status_q $user_q ORDER BY priority DESC, queued_on DESC";
|
$recent_q = '';
|
||||||
|
if ( isset($request['recent']) && $request['recent'] !== false ) {
|
||||||
|
$recent_q = "AND (processed_last >= NOW() - INTERVAL 10 MINUTE OR queued_on >= NOW() - INTERVAL 10 MINUTE)";
|
||||||
|
}
|
||||||
|
|
||||||
|
$base_query = "FROM $this->table WHERE 1=1 $status_q $user_q $recent_q ORDER BY priority DESC, queued_on DESC";
|
||||||
|
|
||||||
$query = "SELECT * $base_query $limit_q";
|
$query = "SELECT * $base_query $limit_q";
|
||||||
$count_query = "SELECT COUNT(ID) $base_query";
|
$count_query = "SELECT COUNT(ID) $base_query";
|
||||||
|
|
|
@ -4,8 +4,6 @@ namespace Tainacan;
|
||||||
class Background_Importer_Heartbeat {
|
class Background_Importer_Heartbeat {
|
||||||
|
|
||||||
public function __construct() {
|
public function __construct() {
|
||||||
global $wpdb;
|
|
||||||
$this->table = $wpdb->prefix . 'tnc_bg_process';
|
|
||||||
add_filter( 'heartbeat_send', array( &$this, 'bg_process_feedback' ), 10, 2 );
|
add_filter( 'heartbeat_send', array( &$this, 'bg_process_feedback' ), 10, 2 );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,18 +16,19 @@ class Background_Importer_Heartbeat {
|
||||||
* @param array $data Data received from the front end (unslashed).
|
* @param array $data Data received from the front end (unslashed).
|
||||||
*/
|
*/
|
||||||
public function bg_process_feedback( $response, $data ){
|
public function bg_process_feedback( $response, $data ){
|
||||||
global $wpdb;
|
|
||||||
|
|
||||||
$user_q = $wpdb->prepare("AND user_id = %d", get_current_user_id());
|
|
||||||
$status_q = "";
|
|
||||||
|
|
||||||
$base_query = "FROM $this->table WHERE processed_last >= NOW() - INTERVAL 10 MINUTE";
|
$request = new \WP_REST_Request( 'GET', '/tainacan/v2/bg-processes' );
|
||||||
|
$request->set_query_params( [ 'recent' => 1 ] );
|
||||||
|
$api_response = rest_do_request( $request );
|
||||||
|
$server = rest_get_server();
|
||||||
|
$data = $server->response_to_data( $api_response, false );
|
||||||
|
|
||||||
$query = "SELECT * $base_query";
|
|
||||||
$result = $wpdb->get_results($query);
|
|
||||||
|
|
||||||
$response['bg_process_feedback'] = $result;
|
$response['bg_process_feedback'] = $data;
|
||||||
|
|
||||||
return $response;
|
return $response;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue