PHPackages                             morningtrain/wp-async - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. morningtrain/wp-async

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

morningtrain/wp-async
=====================

Create Async task for WP

v0.1.1(2y ago)0739MITPHP

Since Mar 30Pushed 2y ago3 watchersCompare

[ Source](https://github.com/Morning-Train/wp-async)[ Packagist](https://packagist.org/packages/morningtrain/wp-async)[ RSS](/packages/morningtrain-wp-async/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (3)Used By (0)

Morningtrain\\WP\\Async
=======================

[](#morningtrainwpasync)

Async task handler for WordPress

Table of Contents
-----------------

[](#table-of-contents)

- [Introduction](#introduction)
- [Getting Started](#getting-started)
    - [Installation](#installation)
- [Usage](#usage)
    - [Register Worker](#register-worker)
    - [Create a Task](#create-a-task)
    - [Dispatch Async Task](#dispatch-async-task)
    - [Dispatch Blocking Task](#dispatch-blocking-task)
- [Contributing](#contributing)
- [Contributors](#contributors)
- [Testing](#testing)
- [License](#license)

Introduction
------------

[](#introduction)

This package is made to dispatch task asyncronely to a new thread.

Getting started
---------------

[](#getting-started)

To get started install the package as described below in [Installation](#installation).

To use the package have a look at [Usage](#usage)

### Installation

[](#installation)

Install with composer.

`composer require morningtrain/wp-async`

Usage
-----

[](#usage)

### Register Worker

[](#register-worker)

To get started with the module simply register a worker `\Morningtrain\WP\Async\Async::registerWorker()`.

```
\Morningtrain\WP\Async\Async::registerWorker();
```

### Create a Task

[](#create-a-task)

Jobs can be created by extending `Morningtrain\WP\Async\Abstracts\AbstractAsyncTask` and create a static `handle` method.

```
use Morningtrain\WP\Async\Abstracts\AbstractAsyncTask;

class TestTask extends AbstractAsyncTask {
    public static function handle($arg1, $arg2) {
        // Do something;
        return "$arg1 $arg2";
    }
}
```

### Dispatch Async Task

[](#dispatch-async-task)

You can dispatch a async task by caling the static method `dispatch` on your task class.

This will run the task asyncronely whitout waiting for response.

```
TestTask::dispatch('arg1', 'arg2');
```

### Dispatch Blocking Task

[](#dispatch-blocking-task)

You can dispatch a blocking task by caling the static method `dispatchBlocking` on your task class.

This will run the task in a new thread, and wait for response.

```
TestTask::dispatchBlocking('arg1', 'arg2');
```

#### Timeout

[](#timeout)

There will be a timout after 5 seconds on blocking task. If you need more time to handle your blocking task, you should overwrite the `dispatchBlocking` method on your task class. You can call the `dispatchBlockingTask` method on the worker with timeout in second as third parameter.

```
public static function dispatchBlocking(mixed ...$params) :array|WP_Error
{
    return static::getWorker()->dispatchBlockingTask(static::getCallback(), $params, 30);
}
```

#### Error handling

[](#error-handling)

You can return a `WP_Error` object from your task, and it will be returned as status 400 with the wp error info.

```
use Morningtrain\WP\Async\Abstracts\AbstractAsyncTask;

class TestTask extends AbstractAsyncTask {
    public static function handle($arg1, $arg2) {
        // Do something;

        $somethingWentWrong = true;

        if ($somethingWentWrong) {
            return new \WP_Error('something_went_wrong', 'Something went wrong');
        }

        return "$arg1 $arg2";
    }
}
```

You can also throw a Throwable (Exception), and it will be returned as status 500 with the exception message.

```
use Morningtrain\WP\Async\Abstracts\AbstractAsyncTask;
use Exception;

class TestTask extends AbstractAsyncTask {
    public static function handle($arg1, $arg2) {
        // Do something;

        $somethingWentWrong = true;

        if ($somethingWentWrong) {
            throw new Exception('Something went wrong');
        }

        return "$arg1 $arg2";
    }
}
```

Alternatively you can return your own json response, if you need another response code.

```
use Morningtrain\WP\Async\Abstracts\AbstractAsyncTask;

class TestTask extends AbstractAsyncTask {
    public static function handle($arg1, $arg2) {
        if (!current_user_can('manage_options')) {
            wp_send_json_error('You are not allowed to do this!', 401);
            exit;
        }

        // Do something;

        return "$arg1 $arg2";
    }
}
```

Contributing
------------

[](#contributing)

Thank you for your interest in contributing to the project.

### Bug Report

[](#bug-report)

If you found a bug, we encourage you to make a pull request.

To add a bug report, create a new issue. Please remember to add a telling title, detailed description and how to reproduce the problem.

### Support Questions

[](#support-questions)

We do not provide support for this package.

### Pull Requests

[](#pull-requests)

1. Fork the Project
2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
3. Commit your Changes (git commit -m 'Add some AmazingFeature')
4. Push to the Branch (git push origin feature/AmazingFeature)
5. Open a Pull Request

Contributors
------------

[](#contributors)

- [Martin Schadegg Brønniche](https://github.com/mschadegg)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE) for more information.

---

Developed by

[![Morningtrain logo](https://camo.githubusercontent.com/e5c33559f0458f056816ab1a75dc9fe3bbd5a141e5d8630d85ec32fa45c01526/68747470733a2f2f6d6f726e696e67747261696e2e646b2f77702d636f6e74656e742f7468656d65732f6d74742d776f726470726573732d7468656d652f6173736574732f696d672f6c6f676f2d6f6e6c792d746578742e737667)](https://morningtrain.dk)

###  Health Score

22

—

LowBetter than 22% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity36

Early-stage or recently created project

 Bus Factor1

Top contributor holds 87.5% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~169 days

Total

2

Last Release

976d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/278725?v=4)[morningtrain](/maintainers/morningtrain)[@morningtrain](https://github.com/morningtrain)

---

Top Contributors

[![mschadegg](https://avatars.githubusercontent.com/u/11231039?v=4)](https://github.com/mschadegg "mschadegg (7 commits)")[![matbaek](https://avatars.githubusercontent.com/u/2310644?v=4)](https://github.com/matbaek "matbaek (1 commits)")

---

Tags

asyncphpwordpresswp

### Embed Badge

![Health badge](/badges/morningtrain-wp-async/health.svg)

```
[![Health](https://phpackages.com/badges/morningtrain-wp-async/health.svg)](https://phpackages.com/packages/morningtrain-wp-async)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
