Skip to content

Commit

Permalink
[#195] use direct SQL queries to CRUD job options
Browse files Browse the repository at this point in the history
  • Loading branch information
ragulka committed Apr 12, 2017
1 parent b2f044d commit 60bf7a2
Showing 1 changed file with 35 additions and 7 deletions.
42 changes: 35 additions & 7 deletions woocommerce/utilities/class-sv-wp-background-job-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ protected function time_exceeded() {
* @return object|null
*/
public function create_job( $attrs ) {
global $wpdb;

if ( empty( $attrs ) ) {
return null;
Expand All @@ -349,7 +350,11 @@ public function create_job( $attrs ) {
'status' => 'queued',
), $attrs );

update_option( "{$this->identifier}_job_{$job_id}" , json_encode( $attrs ) );
$wpdb->insert( $wpdb->options, array(
'option_name' => "{$this->identifier}_job_{$job_id}",
'option_value' => json_encode( $attrs ),
'autoload' => 'no'
) );

$job = new stdClass();

Expand Down Expand Up @@ -396,7 +401,13 @@ public function get_job( $id = null ) {
", $key, $queued, $processing ) );

} else {
$results = get_option( "{$this->identifier}_job_{$id}" );

$results = $wpdb->get_var( $wpdb->prepare( "
SELECT option_value
FROM {$wpdb->options}
WHERE option_name = %s
", "{$this->identifier}_job_{$id}" ) );

}

if ( ! empty( $results ) ) {
Expand Down Expand Up @@ -435,7 +446,6 @@ public function get_job( $id = null ) {
* @return array|null Found jobs or null if none found
*/
public function get_jobs( $args = array() ) {

global $wpdb;

$args = wp_parse_args( $args, array(
Expand Down Expand Up @@ -634,7 +644,7 @@ public function update_job( $job ) {

$job->updated_at = current_time( 'mysql' );

update_option( "{$this->identifier}_job_{$job->id}" , json_encode( $job ) );
$this->update_job_option( $job );

/**
* Run when a job is updated
Expand Down Expand Up @@ -666,7 +676,7 @@ public function complete_job( $job ) {
$job->status = 'completed';
$job->completed_at = current_time( 'mysql' );

update_option( "{$this->identifier}_job_{$job->id}", json_encode( $job ) );
$this->update_job_option( $job );

/**
* Run when a job is completed
Expand Down Expand Up @@ -707,7 +717,7 @@ public function fail_job( $job, $reason = '' ) {
$job->failure_reason = $reason;
}

update_option( "{$this->identifier}_job_{$job->id}", json_encode( $job ) );
$this->update_job_option( $job );

/**
* Run when a job is failed
Expand All @@ -727,6 +737,7 @@ public function fail_job( $job, $reason = '' ) {
* @return false on failure
*/
public function delete_job( $job ) {
global $wpdb;

if ( is_string( $job ) ) {
$job = $this->get_job( $job );
Expand All @@ -736,7 +747,7 @@ public function delete_job( $job ) {
return false;
}

delete_option( "{$this->identifier}_job_{$job->id}" );
$wpdb->delete( $wpdb->options, array( 'option_name' => "{$this->identifier}_job_{$job->id}" ) );

/**
* Run after a job is deleted
Expand Down Expand Up @@ -878,6 +889,23 @@ public function handle_shutdown( $job ) {
}


/**
* Update a job option in options database.
*
* @since 4.7.0-dev.1
* @param object $job the job instance to update in database
* @return int|bool number of rows updated or false on failure, see wpdb::update()
*/
private function update_job_option( $job ) {
global $wpdb;

return $wpdb->update(
$wpdb->options,
array( 'option_value' => json_encode( $job ) ),
array( 'option_name' => "{$this->identifier}_job_{$job->id}" )
);
}

}

endif; // Class exists check

0 comments on commit 60bf7a2

Please sign in to comment.