Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix/TEC-3232-scheduled-imports #3052

Merged
merged 24 commits into from Feb 7, 2020
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
02c4415
Update array() to the new syntax []
mitogh Feb 5, 2020
2ce0667
Remove non required else blocks
mitogh Feb 5, 2020
3965472
Add consistent return values
mitogh Feb 5, 2020
8d668ff
Replacnig in_array with instanceof instead
mitogh Feb 5, 2020
2d327b4
Show a failure on the UI screen if the import failed
mitogh Feb 5, 2020
a90678a
Replace constant with a more explicit value
mitogh Feb 5, 2020
d253784
Make sure a job is marked as incomplete if fails
mitogh Feb 5, 2020
bb3048b
Mark job as failures if an error was present
mitogh Feb 5, 2020
3fdf56d
Remove non used variable
mitogh Feb 5, 2020
c20f053
Make sure the process of a post does not return error
mitogh Feb 5, 2020
88020eb
Setup meta query relationships correctly
mitogh Feb 5, 2020
e2227e3
Remove non required early return
mitogh Feb 5, 2020
29c265b
Add test for cron job processing records
mitogh Feb 6, 2020
fdcbe70
Bypass EA is connected call
mitogh Feb 6, 2020
43cf5ee
Update src/Tribe/Aggregator/Record/Queue_Cleaner.php
mitogh Feb 7, 2020
edf9d2e
Update src/Tribe/Aggregator/Record/Queue.php
mitogh Feb 7, 2020
9c8804f
Update src/Tribe/Aggregator/Cron.php
mitogh Feb 7, 2020
6e7d253
Update src/Tribe/Aggregator/Cron.php
mitogh Feb 7, 2020
b7661ee
Merge branch 'release/B20.02' into fix/TEC-3232-scheduled-imports
mitogh Feb 7, 2020
1bb16d2
Merge branch 'fix/TEC-3232-scheduled-imports' of github.com:moderntri…
mitogh Feb 7, 2020
3356381
Make sure organizer does not exists
mitogh Feb 7, 2020
ad5e528
Make sure uses a non existing venue or organizer
mitogh Feb 7, 2020
6f42e0b
Make sure the ID of the organizer is not defined
mitogh Feb 7, 2020
9eaf274
Use PHP_INT_MAX as record ID instead
mitogh Feb 7, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
75 changes: 40 additions & 35 deletions src/Tribe/Aggregator/Cron.php
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
mitogh marked this conversation as resolved.
Show resolved Hide resolved
'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
mitogh marked this conversation as resolved.
Show resolved Hide resolved
'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
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
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
Expand Up @@ -70,15 +70,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 @@ -182,12 +181,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
mitogh marked this conversation as resolved.
Show resolved Hide resolved
*
* @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 @@ -290,6 +293,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
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; // For pre-PHP 5.6 compat, we do not define as 12 * HOUR_IN_SECONDS
mitogh marked this conversation as resolved.
Show resolved Hide resolved

/**
* @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
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
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
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
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