Use REST API in heartbeat callback #130

This commit is contained in:
leogermani 2019-01-08 16:32:50 -02:00
parent fae9d42831
commit 3d8939e778
2 changed files with 39 additions and 31 deletions

View File

@ -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' ),
], ],
], ],
), ),
@ -150,8 +154,13 @@ class REST_Background_Processes_Controller extends REST_Controller {
$status_q = "AND done = 1"; $status_q = "AND done = 1";
} }
} }
$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 ORDER BY priority DESC, queued_on DESC"; $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";

View File

@ -2,34 +2,33 @@
namespace Tainacan; namespace Tainacan;
class Background_Importer_Heartbeat { class Background_Importer_Heartbeat {
public function __construct() { public function __construct() {
global $wpdb; add_filter( 'heartbeat_send', array( &$this, 'bg_process_feedback' ), 10, 2 );
$this->table = $wpdb->prefix . 'tnc_bg_process'; }
add_filter( 'heartbeat_send', array( &$this, 'bg_process_feedback' ), 10, 2 );
} /**
* Receive Heartbeat data and response.
/** *
* Receive Heartbeat data and response. * Processes data received via a Heartbeat request, and returns additional data to pass back to the front end.
* *
* Processes data received via a Heartbeat request, and returns additional data to pass back to the front end. * @param array $response Heartbeat response data to pass back to front end.
* * @param array $data Data received from the front end (unslashed).
* @param array $response Heartbeat response data to pass back to front end. */
* @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; $request = new \WP_REST_Request( 'GET', '/tainacan/v2/bg-processes' );
$request->set_query_params( [ 'recent' => 1 ] );
$user_q = $wpdb->prepare("AND user_id = %d", get_current_user_id()); $api_response = rest_do_request( $request );
$status_q = ""; $server = rest_get_server();
$data = $server->response_to_data( $api_response, false );
$base_query = "FROM $this->table WHERE processed_last >= NOW() - INTERVAL 10 MINUTE";
$query = "SELECT * $base_query"; $response['bg_process_feedback'] = $data;
$result = $wpdb->get_results($query);
return $response;
$response['bg_process_feedback'] = $result;
return $response; }
}
} }