PHP code example of shewa / wp-job-queue

1. Go to this page and download the library: Download shewa/wp-job-queue library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

shewa / wp-job-queue example snippets


use WpJobQueue\Database\Migrator;

register_activation_hook( __FILE__, function () {
    Migrator::install();
} );

use WpJobQueue\Bootstrap;
use WpJobQueue\JobManager;

// Boot on plugins_loaded
Bootstrap::init( [
    'jobs' => [
        MyCustomJob::class,
    ],
] );

// Dispatch a job
$job_id = JobManager::instance()->dispatch(
    MyCustomJob::class,
    [ 'step' => 0 ]
);

if ( is_wp_error( $job_id ) ) {
    // Handle error
}



namespace MyPlugin\Jobs;

use WpJobQueue\AbstractJob;
use WpJobQueue\JobRecord;
use WpJobQueue\JobResult;
use WpJobQueue\JobStatus;

class MyCustomJob extends AbstractJob {

    public static function getType(): string {
        return 'my_custom_job';
    }

    public static function getLabel(): string {
        return 'My Custom Job';
    }

    public function handle( JobRecord $job ): JobResult {
        if ( JobStatus::CANCELLED === $job->status ) {
            return JobResult::failed( 'Job was cancelled.' );
        }

        $payload = $job->payload;
        $step    = (int) ( $payload['step'] ?? 0 );

        // Do one chunk of work…
        $step++;
        $payload['step'] = $step;

        if ( $step < 10 ) {
            return JobResult::continue(
                $payload,
                $step * 10,
                sprintf( 'Processing step %d…', $step )
            );
        }

        return JobResult::complete(
            [ 'done' => true ],
            'All steps finished.'
        );
    }
}

Bootstrap::init( [
    'jobs' => [
        MyCustomJob::class,
    ],
] );

use WpJobQueue\JobManager;

$job_id = JobManager::instance()->dispatch(
    MyCustomJob::class,
    [ 'step' => 0 ],
    get_current_user_id() // optional; defaults to current user
);

if ( is_wp_error( $job_id ) ) {
    // Possible codes: invalid_job, unregistered_job, job_already_running, job_create_failed
}

use WpJobQueue\JobManager;
use WpJobQueue\JobStatus;

$job = JobManager::instance()->getJob( $job_id );

$active_jobs = JobManager::instance()->listJobs( [
    'job_type' => 'my_custom_job',
    'status'   => JobStatus::active(),
] );

wp_create_nonce( \WpJobQueue\Bootstrap::nonceAction() );

$result = JobManager::instance()->cancel( $job_id );

if ( is_wp_error( $result ) ) {
    // job_not_found, unauthorized, cancel_failed
}

if ( JobStatus::CANCELLED === $job->status ) {
    return JobResult::failed( 'Job was cancelled.' );
}

public function handle( JobRecord $job ): JobResult {
    $payload = $job->payload;
    $page    = (int) ( $payload['page'] ?? 1 );
    $items   = (array) ( $payload['items'] ?? [] );

    $response = $this->fetchPage( $page );

    if ( is_wp_error( $response ) ) {
        return JobResult::failed( $response->get_error_message() );
    }

    $items  = array_merge( $items, $response['items'] );
    $total  = (int) $response['total_pages'];
    $page++;

    if ( $page <= $total ) {
        return JobResult::continue(
            [ 'page' => $page, 'items' => $items ],
            (int) round( ( $page / $total ) * 100 ),
            sprintf( 'Fetched page %d of %d…', $page - 1, $total )
        );
    }

    return JobResult::complete(
        [ 'items' => $items, 'count' => count( $items ) ],
        'Export complete.'
    );
}

add_filter( 'wp_job_queue_table_name', function ( $table ) {
    return $GLOBALS['wpdb']->prefix . 'my_custom_jobs';
} );
bash
*/1 * * * * wget -q -O - https://yoursite.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1