prefix . 'aipt_job_products'; // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Table name from $wpdb->prefix; values prepared; plugin-owned table. $count = $wpdb->get_var( $wpdb->prepare( "SELECT interrupted_attempts FROM {$table} WHERE job_id = %d AND product_id = %d", $job_id, $product_id ) ); // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching return max( 0, (int) $count ); } /** * This product's generation starts now: until attemptEnded(), the attempt * counts as interrupted. The caller holds the product's claim, so no other * request writes this count meanwhile. * * @param int $job_id Job ID. * @param int $product_id Product ID. * @param int $interrupted What interruptedAttempts() answered for it. * @return void */ public function attemptStarted( int $job_id, int $product_id, int $interrupted ): void { global $wpdb; $table = $wpdb->prefix . 'aipt_job_products'; // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Table name from $wpdb->prefix; values prepared; plugin-owned table. $stored = $wpdb->query( $wpdb->prepare( "UPDATE {$table} SET interrupted_attempts = %d WHERE job_id = %d AND product_id = %d", $interrupted + 1, $job_id, $product_id ) ); // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching if ( false === $stored ) { Logger::error( 'automation', 'Interrupted attempt could not be marked', array( 'job_id' => $job_id, 'product_id' => $product_id, 'error' => (string) $wpdb->last_error ) ); } } /** * The request that started this product's attempt left it alive (any * outcome), or the product was set aside: forget its interrupted attempts. * Writes only a row that holds a count. * * @param int $job_id Job ID. * @param int $product_id Product ID. * @return void */ public function attemptEnded( int $job_id, int $product_id ): void { global $wpdb; $table = $wpdb->prefix . 'aipt_job_products'; // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Table name from $wpdb->prefix; values prepared; plugin-owned table. $cleared = $wpdb->query( $wpdb->prepare( "UPDATE {$table} SET interrupted_attempts = 0 WHERE job_id = %d AND product_id = %d AND interrupted_attempts <> 0", $job_id, $product_id ) ); // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching if ( false === $cleared ) { Logger::error( 'automation', 'Interrupted attempts could not be cleared', array( 'job_id' => $job_id, 'product_id' => $product_id, 'error' => (string) $wpdb->last_error ) ); } } /** * A tick starts working on the job's products. * * @param int $job_id Job ID. * @return int Ticks in a row, this one included, that have not finished a product. */ public function tickStarted( int $job_id ): int { $key = $this->tickKey( $job_id ); $count = (int) get_transient( $key ) + 1; set_transient( $key, $count, self::TTL_SECONDS ); return $count; } /** * Take back the tick tickStarted() counted: the merchant's monthly spend * limit stopped it before a product finished, so its products waited for * the limit, not on a stuck job. Without this, ticks around a wait at the * limit would add up to a pause for a stall. * * @param int $job_id Job ID. * @return void */ public function tickNotCounted( int $job_id ): void { $key = $this->tickKey( $job_id ); $count = (int) get_transient( $key ) - 1; if ( $count > 0 ) { set_transient( $key, $count, self::TTL_SECONDS ); return; } delete_transient( $key ); } /** * The job's count of ticks without progress starts over: a product * finished (completed, or failed for a reason of its own), the job paused * for that count, or it starts running again (a resume, a new run or * cycle), so ticks before a pause never count against the next run. * * @param int $job_id Job ID. * @return void */ public function resetTicks( int $job_id ): void { delete_transient( $this->tickKey( $job_id ) ); } /** * @param int $job_id Job ID. * @return string */ private function tickKey( int $job_id ): string { return 'aipt_jt_' . $job_id; } }