Skip to content

Commit

Permalink
Merge pull request #3052 from moderntribe/fix/TEC-3232-scheduled-imports
Browse files Browse the repository at this point in the history
fix/TEC-3232-scheduled-imports
  • Loading branch information
mitogh committed Feb 7, 2020
2 parents 7920fe0 + 9eaf274 commit 51043b7
Show file tree
Hide file tree
Showing 15 changed files with 166 additions and 89 deletions.
75 changes: 40 additions & 35 deletions src/Tribe/Aggregator/Cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,14 +293,13 @@ public function verify_child_record_creation() {
if ( ! tribe( 'events-aggregator.main' )->is_service_active() ) {
return;
}

$records = Tribe__Events__Aggregator__Records::instance();
$service = tribe( 'events-aggregator.service' );

$query = $records->query( array(
'post_status' => Tribe__Events__Aggregator__Records::$status->schedule,
$query = $records->query( [
'post_status' => Tribe__Events__Aggregator__Records::$status->schedule,
'posts_per_page' => -1,
) );
] );

if ( ! $query->have_posts() ) {
tribe( 'logger' )->log_debug( 'No Records Scheduled, skipped creating children', 'EA Cron' );
Expand Down Expand Up @@ -344,7 +343,6 @@ public function verify_child_record_creation() {

// Creating the child records based on this Parent
$child = $record->create_child_record();

tribe( 'logger' )->log_debug( sprintf( 'Creating child record %d for %d', $child->id, $record->id ), 'EA Cron' );

if ( ! is_wp_error( $child ) ) {
Expand Down Expand Up @@ -401,31 +399,34 @@ public function verify_fetching_from_service() {

$records = Tribe__Events__Aggregator__Records::instance();

$query = $records->query( array(
$query = $records->query( [
'post_status' => Tribe__Events__Aggregator__Records::$status->pending,
'posts_per_page' => - 1,
'posts_per_page' => -1,
'order' => 'ASC',
'meta_query' => array(
'origin-not-csv' => array(
'key' => '_tribe_aggregator_origin',
'value' => 'csv',
'meta_query' => [
'origin-not-csv' => [
'key' => '_tribe_aggregator_origin',
'value' => 'csv',
'compare' => '!=',
),
// if not specified then assume batch push is not supported
'no-batch-push-support-specified' => array(
'key' => '_tribe_aggregator_allow_batch_push',
'value' => 'bug #23268',
'compare' => 'NOT EXISTS',
),
// if specified and not `1` then batch push is not supported
'explicit-no-batch-push-support' => array(
'key' => '_tribe_aggregator_allow_batch_push',
'value' => '1',
'compare' => '!=',
),
),
],
[
'relation' => 'OR',
// If not specified then assume batch push is not supported.
'no-batch-push-support-specified' => [
'key' => '_tribe_aggregator_allow_batch_push',
'value' => 'bug #23268',
'compare' => 'NOT EXISTS',
],
// If specified, and not `1`, then batch push is not supported.
'explicit-no-batch-push-support' => [
'key' => '_tribe_aggregator_allow_batch_push',
'value' => '1',
'compare' => '!=',
],
],
],
'after' => '-4 hours',
) );
] );

if ( ! $query->have_posts() ) {
tribe( 'logger' )->log_debug( 'No Records Pending, skipped Fetching from service', 'EA Cron' );
Expand Down Expand Up @@ -516,22 +517,22 @@ public function purge_expired_records() {
)
);

$args = array(
'post_status' => array(
$args = [
'post_status' => [
$statuses->pending,
$statuses->success,
$statuses->failed,
$statuses->draft,
),
'date_query' => array(
array(
],
'date_query' => [
[
'before' => date( 'Y-m-d H:i:s', time() - $records->get_retention() ),
'column' => 'post_date_gmt',
),
),
'order' => 'ASC',
],
],
'order' => 'ASC',
'posts_per_page' => 100,
);
];

if ( $records_to_retain ) {
$args['post__not_in'] = $records_to_retain;
Expand Down Expand Up @@ -590,6 +591,10 @@ protected function maybe_process_immediately( Tribe__Events__Aggregator__Record_
$import_data = $record->prep_import_data();

if ( empty( $import_data ) || $import_data instanceof WP_Error || ! is_array( $import_data ) ) {

$record->update_meta( 'last_import_status', 'error:import-failed' );
$record->set_status_as_failed( $import_data );

return;
}

Expand Down
12 changes: 6 additions & 6 deletions src/Tribe/Aggregator/Record/Abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ public function maybe_add_meta_via_pre_wp_44_method( $id, $meta ) {
* post ID if the record had to be re-scheduled due to HTTP request
* limit.
*/
public function queue_import( $args = array() ) {
public function queue_import( $args = [] ) {
$aggregator = tribe( 'events-aggregator.main' );

$is_previewing = (
Expand Down Expand Up @@ -761,11 +761,11 @@ public function queue_import( $args = array() ) {
if ( 'core:aggregator:http_request-limit' === $response->get_error_code() ) {
$this->should_queue_import( true );
return $this->set_status_as_pending();
} else {
$error = $response;

return $this->set_status_as_failed( $error );
}

$error = $response;

return $this->set_status_as_failed( $error );
}

// if the Aggregator response has an unexpected format, set this record as failed
Expand Down Expand Up @@ -1207,7 +1207,6 @@ public function get_last_import_status( $type = 'error', $lookup_children = fals
if ( 'error:usage-limit-exceeded' === $status ) {
return __( 'When this import was last scheduled to run, the daily limit for your Event Aggregator license had already been reached.', 'the-events-calendar' );
}

return tribe( 'events-aggregator.service' )->get_service_message( $status );
}

Expand Down Expand Up @@ -1264,6 +1263,7 @@ public function process_posts( $data = array(), $start_immediately = false ) {
$items = $this->prep_import_data( $data );

if ( is_wp_error( $items ) ) {
$this->set_status_as_failed( $items );
return $items;
}

Expand Down
4 changes: 3 additions & 1 deletion src/Tribe/Aggregator/Record/List_Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -529,12 +529,14 @@ public function column_imported( $post ) {

if ( 'scheduled' === $this->tab->get_slug() ) {
$last_import_error = $record->get_last_import_status( 'error' );
$status = 'success';

if ( $last_import_error ) {
$html[] = '<span class="dashicons dashicons-warning tribe-ea-status-failed" title="' . esc_attr( $last_import_error ) . '"></span>';
$status = 'failed';
}

$has_child_record = $record->get_child_record_by_status( 'success', 1 );
$has_child_record = $record->get_child_record_by_status( $status, 1 );

if ( ! $has_child_record ) {
$html[] = '<i>' . esc_html__( 'Unknown', 'the-events-calendar' ) . '</i>';
Expand Down
10 changes: 7 additions & 3 deletions src/Tribe/Aggregator/Record/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,14 @@ public function __construct( $record, $items = array(), Tribe__Events__Aggregato
$record = Tribe__Events__Aggregator__Records::instance()->get_by_post_id( $record );
}

if ( ! is_object( $record ) || ! in_array( 'Tribe__Events__Aggregator__Record__Abstract', class_parents( $record ) ) ) {
if ( ! is_object( $record ) || ! $record instanceof \Tribe__Events__Aggregator__Record__Abstract ) {
$this->null_process = true;

return;
}

if ( is_wp_error( $items ) ) {
$this->null_process = true;

return;
}

Expand Down Expand Up @@ -184,12 +183,16 @@ public function count() {
}

/**
* Shortcut to check if this queue is empty.
* Shortcut to check if this queue is empty or it has a null process.
*
* @return boolean `true` if this queue instance has acquired the lock and
* the count is 0, `false` otherwise.
*/
public function is_empty() {
if ( $this->null_process ) {
return true;
}

return $this->has_lock && 0 === $this->count();
}

Expand Down Expand Up @@ -292,6 +295,7 @@ public function process( $batch_size = null ) {
|| is_wp_error( $data )
) {
$this->release_lock();
$this->is_fetching = false;
return $this->activity();
}

Expand Down
6 changes: 3 additions & 3 deletions src/Tribe/Aggregator/Record/Queue_Cleaner.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ class Tribe__Events__Aggregator__Record__Queue_Cleaner {
/**
* Default is 12hrs.
*
* @var int The time a record is allowed to stall before havint its status set to to failed since its creation in
* @var int The time a record is allowed to stall before have the status set to to failed since its creation in
* seconds.
*/
protected $time_to_live = 43200; // For pre-PHP 5.6 compat, we do not define as 12 * HOUR_IN_SECONDS
protected $time_to_live = HOUR_IN_SECONDS * 12;

/**
* @var int The time a record is allowed to stall before having
Expand Down Expand Up @@ -94,7 +94,7 @@ public function maybe_fail_stalled_record( Tribe__Events__Aggregator__Record__Ab

$post_status = $record->post->post_status;

if ( ! in_array( $post_status, array( $pending, $failed ) ) ) {
if ( ! in_array( $post_status, [ $pending, $failed ], true ) ) {
return false;
}

Expand Down
6 changes: 3 additions & 3 deletions src/Tribe/Aggregator/Record/Queue_Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,10 @@ public function next_waiting_record( $interactive_only = false ) {

if ( empty( $waiting_records ) ) {
return $this->current_record_id = 0;
} else {
$next_record = array_shift( $waiting_records );
return $this->current_record_id = $next_record->ID;
}

$next_record = array_shift( $waiting_records );
return $this->current_record_id = $next_record->ID;
}

/**
Expand Down
4 changes: 1 addition & 3 deletions src/Tribe/Aggregator/Record/Queue_Realtime.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function update_loop_vars() {

public function render_update_message() {
if ( ! Tribe__Events__Aggregator__Page::instance()->is_screen() ) {
return;
return false;
}

/** @var Tribe__Events__Aggregator__Record__Queue_Processor $processor */
Expand Down Expand Up @@ -121,7 +121,6 @@ public function ajax() {
// Load the queue
/** @var \Tribe__Events__Aggregator__Record__Queue_Interface $queue */
$queue = $this->queue ? $this->queue : Tribe__Events__Aggregator__Record__Queue_Processor::build_queue( $this->record_id );

// We always need to setup the Current Queue
$this->queue_processor->set_current_queue( $queue );

Expand All @@ -138,7 +137,6 @@ public function ajax() {
$current_queue = $this->queue_processor->current_queue;
$done = $current_queue->is_empty() && empty( $current_queue->is_fetching );
$percentage = $current_queue->progress_percentage();

$this->ajax_operations->exit_data( $this->get_progress_message_data( $current_queue, $percentage, $done ) );
}

Expand Down
1 change: 0 additions & 1 deletion src/Tribe/Aggregator/Record/Void_Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public function __construct( $error ) {

return;
}

$this->error = $error;
}

Expand Down
32 changes: 18 additions & 14 deletions src/Tribe/Aggregator/Tabs/Scheduled.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,8 @@ private function action_notice( $action, $ids = array(), $error = null ) {
private function action_delete_record( $records = array() ) {
$record_obj = Tribe__Events__Aggregator__Records::instance()->get_post_type();
$records = array_filter( (array) $records, 'is_numeric' );
$success = array();
$errors = array();
$success = [];
$errors = [];

foreach ( $records as $record_id ) {
$record = Tribe__Events__Aggregator__Records::instance()->get_by_post_id( $record_id );
Expand All @@ -287,7 +287,7 @@ private function action_delete_record( $records = array() ) {
$success[ $record->id ] = true;
}

return array( $success, $errors );
return [ $success, $errors ];
}

/**
Expand All @@ -299,12 +299,11 @@ private function action_delete_record( $records = array() ) {
*
* @return array
*/
public function action_run_import( $records = array() ) {
public function action_run_import( $records = [] ) {
$service = tribe( 'events-aggregator.service' );
$record_obj = Tribe__Events__Aggregator__Records::instance()->get_post_type();
$records = array_filter( (array) $records, 'is_numeric' );
$success = array();
$errors = array();
$success = [];
$errors = [];

foreach ( $records as $record_id ) {
$record = Tribe__Events__Aggregator__Records::instance()->get_by_post_id( $record_id );
Expand Down Expand Up @@ -337,16 +336,21 @@ public function action_run_import( $records = array() ) {
continue;
}

$record->update_meta( 'last_import_status', 'success:queued' );
$child->update_meta( 'import_id', $status->data->import_id );

$child->finalize();
$child->process_posts( array(), true );
$post = $child->process_posts( [], true );

$success[ $record->id ] = $record;
if ( is_wp_error( $post ) ) {
$errors[ $record->id ] = $post;
$record->update_meta( 'last_import_status', 'error:import-failed' );
} else {
$record->update_meta( 'last_import_status', 'success:queued' );
$child->update_meta( 'import_id', $status->data->import_id );

$success[ $record->id ] = $record;
}
}

return array( $success, $errors );
return [ $success, $errors ];
}

/**
Expand All @@ -356,7 +360,7 @@ public function action_run_import( $records = array() ) {
*/
public function maybe_display_aggregator_missing_license_key_message() {
if ( tribe( 'events-aggregator.main' )->is_service_active() ) {
return;
return '';
}

ob_start();
Expand Down
Loading

0 comments on commit 51043b7

Please sign in to comment.