Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions includes/RapidLoad_Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ class RapidLoad_Api

public $apiKey = null;

/**
* Retry-After (seconds) of the last response, 0 when absent.
*/
public $retry_after = 0;


/**
* RapidLoad_Api constructor.
Expand Down Expand Up @@ -73,8 +78,12 @@ function post( $endpoint, $data = [] ) {
*/
public function handle_response( $response , $data = null) {

$this->retry_after = 0;

if ( is_array( $response ) && ! is_wp_error( $response ) ) {

$this->retry_after = max( 0, (int) wp_remote_retrieve_header( $response, 'retry-after' ) );

if ( $response['response']['code'] === 200 ) {
$body = $response['body'];

Expand Down
84 changes: 62 additions & 22 deletions includes/RapidLoad_DB.php
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ public static function transform_link($link, $rule = 'path'){
$data['applied_successful_links'] = isset( $link->applied_successful_links ) ? $link->applied_successful_links : 0;
$data['applied_links'] = isset( $link->applied_successful_links ) ? $link->applied_successful_links : 0;
$data['time'] = isset( $link->created_at ) ? strtotime( $link->created_at ) * 1000 : null;
$data['status'] = isset( $link->status ) ? $link->status : null;
$data['status'] = isset( $link->status ) ? RapidLoad_Job_Data::display_status($link->status) : null;
if(isset($data['rule_id'])){
$job_url = RapidLoad_Job::find_or_fail($data['rule_id']);
$data['base'] = $job_url->url;
Expand Down Expand Up @@ -689,7 +689,7 @@ public static function requeueJob($id){

global $wpdb;

$wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->prefix}rapidload_job_data SET status = %s WHERE job_id = %d", 'queued', $id ) );
$wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->prefix}rapidload_job_data SET status = %s, attempts = 0, created_at = %s WHERE job_id = %d AND status <> 'submitting'", 'queued', gmdate( 'Y-m-d H:i:s' ), $id ) );

$error = $wpdb->last_error;

Expand All @@ -705,22 +705,42 @@ public static function updateUrlJobDataStatusWhere($status = 'queued', $ids = []
global $wpdb;

if (!empty($ids)) {
$ids = implode(",", array_map('intval', $ids));
$ids = array_map('intval', (array) $ids);
} else {
$ids = $wpdb->get_col($wpdb->prepare("SELECT id FROM {$wpdb->prefix}rapidload_job WHERE rule = %s", 'is_url'));
$ids = implode(',', array_map('intval', $ids));
$ids = array_map('intval', $wpdb->get_col($wpdb->prepare("SELECT id FROM {$wpdb->prefix}rapidload_job WHERE rule = %s", 'is_url')));
}

if (empty($wherStatus)) {
$wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->prefix}rapidload_job_data SET status = %s WHERE job_id IN (%d)", $status, $ids));
} else {
if($wherStatus === "success"){
$wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->prefix}rapidload_job_data SET status = %s WHERE job_id IN (%d) AND status = %s AND warnings IS NOT NULL", $status, $ids, $wherStatus));
}else{
$wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->prefix}rapidload_job_data SET status = %s WHERE job_id IN (%d) AND status = %s", $status, $ids, $wherStatus));
if (empty($ids)) {
return true;
}

// explicit requeue: clear retry state and any backoff
$set = 'status = %s';
$args = [ $status ];
if ($status === 'queued') {
$set .= ', attempts = 0, created_at = %s';
$args[] = gmdate( 'Y-m-d H:i:s' );
}

$where = 'job_id IN (' . implode(',', array_fill(0, count($ids), '%d')) . ')';
$args = array_merge($args, $ids);

if ($status === 'queued') {
// rows mid-submit keep their claim; the in-flight result lands normally
$where .= " AND status <> 'submitting'";
}

if (!empty($wherStatus)) {
$where .= ' AND status = %s';
$args[] = $wherStatus;
if ($wherStatus === "success") {
$where .= ' AND warnings IS NOT NULL';
}
}

// $set and $where are built only from fixed fragments and %s/%d placeholders
$wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->prefix}rapidload_job_data SET $set WHERE $where", $args ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared

$error = $wpdb->last_error;

if(!empty($error)){
Expand All @@ -735,22 +755,42 @@ public static function updateRuleJobDataStatusWhere($status = 'queued', $ids = [
global $wpdb;

if (!empty($ids)) {
$ids = implode(",", array_map('intval', $ids));
$ids = array_map('intval', (array) $ids);
} else {
$ids = $wpdb->get_col($wpdb->prepare("SELECT id FROM {$wpdb->prefix}rapidload_job WHERE rule != %s", 'is_url'));
$ids = implode(',', array_map('intval', $ids));
$ids = array_map('intval', $wpdb->get_col($wpdb->prepare("SELECT id FROM {$wpdb->prefix}rapidload_job WHERE rule != %s", 'is_url')));
}

if (empty($wherStatus)) {
$wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->prefix}rapidload_job_data SET status = %s WHERE job_id IN (%d)", $status, $ids));
} else {
if($wherStatus === "success"){
$wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->prefix}rapidload_job_data SET status = %s WHERE job_id IN (%d) AND status = %s AND warnings IS NOT NULL", $status, $ids, $wherStatus));
}else{
$wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->prefix}rapidload_job_data SET status = %s WHERE job_id IN (%d) AND status = %s", $status, $ids, $wherStatus));
if (empty($ids)) {
return true;
}

// explicit requeue: clear retry state and any backoff
$set = 'status = %s';
$args = [ $status ];
if ($status === 'queued') {
$set .= ', attempts = 0, created_at = %s';
$args[] = gmdate( 'Y-m-d H:i:s' );
}

$where = 'job_id IN (' . implode(',', array_fill(0, count($ids), '%d')) . ')';
$args = array_merge($args, $ids);

if ($status === 'queued') {
// rows mid-submit keep their claim; the in-flight result lands normally
$where .= " AND status <> 'submitting'";
}

if (!empty($wherStatus)) {
$where .= ' AND status = %s';
$args[] = $wherStatus;
if ($wherStatus === "success") {
$where .= ' AND warnings IS NOT NULL';
}
}

// $set and $where are built only from fixed fragments and %s/%d placeholders
$wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->prefix}rapidload_job_data SET $set WHERE $where", $args ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared

$error = $wpdb->last_error;

if(!empty($error)){
Expand Down
2 changes: 1 addition & 1 deletion includes/RapidLoad_Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function __construct($args = [])

}else{

$this->created_at = gmdate( "Y-m-d H:m:s", time() );
$this->created_at = gmdate( "Y-m-d H:i:s", time() );
}

}
Expand Down
Loading
Loading