Introducing WordPress Upgrade Task Runner v2

WordPress Batch Processing. Introducing Upgrade Task Runner version 2. Register migration tasks to schedule from the admin UI and run as a cron job.

Upgrade Task Runner is a batch processing library and plugin that untangles the complexity of making changes to large datasets when dealing with multiple environments. Upgrade Task Runner allows developers to create tasks via a class that could run it’s own data changes. Upgrade Task Runner provides the foundation and UI to easily create and run your own custom batch processing tasks.

WordPress Batch Processing

Register custom migration tasks that can be scheduled from the plugin settings dashboard in the admin and run as a cron job.

thefrosty/wp-upgrade-task-runner

In February of this year (2019) I broke some code out of a project I felt could be useful across other WordPress installs. Although marked as version 1.0, it was really a beta version. The latest release bumped the version to 2.0 because of the many breaking changes adhering to Semantic Versioning. There’s still some improvements to be made around the UI/UX for task listeners via some JS listeners. But for now it does it’s job as an interface for listing registered tasks and being able to schedule them.

Requirements

PHP >= 7.3
WordPress >= 5.1

Getting Started

There are only two required steps that are needed to create a new task.

  1. A class needs to be created and this class needs to extend the AbstractTaskRunner class.
    1. See the ExampleMigrationTask example class on GitHub.
  2. Register any new task(s) on the TheFrosty\WpUpgradeTaskRunner\Tasks\TaskLoader\TaskLoader::REGISTER_TASKS_TAG filter.
use TheFrosty\WpUpgradeTaskRunner\Tasks\TaskLoader;

\add_filter(TaskLoader::REGISTER_TASKS_TAG, static function(array $tasks): array {
    $tasks[] = new \Project\SomeCustomTask();
    return $tasks;
});

Your new task \Project\SomeCustomTask() in this example has to extend AbstractTaskRunner, which will require the method that is called on when a task is scheduled: dispatch. Within the dispatch method your should call other functions, classes or methods required by said task then trigger the clearScheduledEvent() and complete() methods to finalize your task.

Example

<?php declare(strict_types=1);

namespace Project;

use TheFrosty\WpUpgradeTaskRunner\Api\AbstractTaskRunner;
use TheFrosty\WpUpgradeTaskRunner\Models\UpgradeModel;

class SomeCustomTask extends AbstractTaskRunner
{

    public const DATE = '2019-12-05';
    public const DESCRIPTION = 'This is an example upgrade/migration task. It does not do anything 
    except sleep for five seconds before it "completes" it\'s task.';
    public const TITLE = 'Example Migration Task';

    /**
     * Dispatch the migration task.
     * @param UpgradeModel $model
     */
    public function dispatch(UpgradeModel $model): void
    {
        \error_log(\sprintf('[Migration] %s is running...', self::class));
        $this->longRunningTask();
        $this->clearScheduledEvent(self::class, $model);
        $this->complete($model);
        \error_log(\sprintf('[Migration] %s completed successfully.', self::class));
    }

    /**
     * Example task that needs to "run".
     */
    private function longRunningTask(): void
    {
        \sleep(5);
    }
}

The $this->clearScheduledEvent() will remove the single cron event, then $this->complete() will add the class to the settings array to show whether a task has been completed from the admin UI.

WP Upgrade Task Runner Dashboard table UI

Grab the latest copy on GitHub and install it today!

Austin
Austin

πŸ’πŸ½β€β™‚οΈ Husband to Jeana.
⚾️ Dodgers & Brewers.
πŸ’» PHP Engineer.
πŸŒπŸΌβ€β™‚οΈGolfer; ~14 HDC.
🌱 Hydroponic Gardner.
🍿 Plex nerd.
πŸš™ '15 WRX, '22 Model 3 LR, & '66 Corvette C2.

Follow me on Twitter @TheFrosty & Instagram @TheFrosty.

Articles: 292

One comment

Comments are closed.