{"id":19450841,"url":"https://github.com/morning-train/wp-async","last_synced_at":"2025-02-25T09:22:20.824Z","repository":{"id":149038649,"uuid":"621236492","full_name":"Morning-Train/wp-async","owner":"Morning-Train","description":"Async Task handling for WordPress","archived":false,"fork":false,"pushed_at":"2023-09-15T12:11:27.000Z","size":9,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-02-09T01:16:11.458Z","etag":null,"topics":["async","php","wordpress","wp"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Morning-Train.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-03-30T09:03:10.000Z","updated_at":"2023-04-11T07:53:23.000Z","dependencies_parsed_at":null,"dependency_job_id":"513feb94-d89b-4cf9-8267-145a8c5d82fa","html_url":"https://github.com/Morning-Train/wp-async","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Morning-Train%2Fwp-async","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Morning-Train%2Fwp-async/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Morning-Train%2Fwp-async/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Morning-Train%2Fwp-async/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Morning-Train","download_url":"https://codeload.github.com/Morning-Train/wp-async/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240637767,"owners_count":19833190,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["async","php","wordpress","wp"],"created_at":"2024-11-10T16:39:14.851Z","updated_at":"2025-02-25T09:22:20.784Z","avatar_url":"https://github.com/Morning-Train.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Morningtrain\\WP\\Async\n\nAsync task handler for WordPress\n\n## Table of Contents\n\n- [Introduction](#introduction)\n- [Getting Started](#getting-started)\n    - [Installation](#installation)\n- [Usage](#usage)\n    - [Register Worker](#register-worker)\n    - [Create a Task](#create-a-task)\n    - [Dispatch Async Task](#dispatch-async-task)\n    - [Dispatch Blocking Task](#dispatch-blocking-task)\n- [Contributing](#contributing)\n- [Contributors](#contributors)\n- [Testing](#testing)\n- [License](#license)\n\n## Introduction\n\nThis package is made to dispatch task asyncronely to a new thread.\n\n## Getting started\n\nTo get started install the package as described below in [Installation](#installation).\n\nTo use the package have a look at [Usage](#usage)\n\n### Installation\n\nInstall with composer.\n\n```composer require morningtrain/wp-async```\n\n## Usage\n\n### Register Worker\n\nTo get started with the module simply register a worker `\\Morningtrain\\WP\\Async\\Async::registerWorker()`.\n\n```php\n\\Morningtrain\\WP\\Async\\Async::registerWorker();\n```\n\n### Create a Task\nJobs can be created by extending `Morningtrain\\WP\\Async\\Abstracts\\AbstractAsyncTask` and create a static `handle` method.\n\n```php\nuse Morningtrain\\WP\\Async\\Abstracts\\AbstractAsyncTask;\n\nclass TestTask extends AbstractAsyncTask {\n    public static function handle($arg1, $arg2) {\n        // Do something;\n        return \"$arg1 $arg2\";\n    }\n}\n```\n\n### Dispatch Async Task\nYou can dispatch a async task by caling the static method `dispatch` on your task class.\n\nThis will run the task asyncronely whitout waiting for response.\n\n```php\nTestTask::dispatch('arg1', 'arg2');\n```\n\n### Dispatch Blocking Task\nYou can dispatch a blocking task by caling the static method `dispatchBlocking` on your task class.\n\nThis will run the task in a new thread, and wait for response. \n\n```php\nTestTask::dispatchBlocking('arg1', 'arg2');\n```\n\n#### Timeout\n\nThere will be a timout after 5 seconds on blocking task. \nIf you need more time to handle your blocking task, you should overwrite the `dispatchBlocking` method on your task class.\nYou can call the `dispatchBlockingTask` method on the worker with timeout in second as third parameter.\n\n```php\npublic static function dispatchBlocking(mixed ...$params) :array|WP_Error\n{\n    return static::getWorker()-\u003edispatchBlockingTask(static::getCallback(), $params, 30);\n}\n```\n\n#### Error handling\n\nYou can return a `WP_Error` object from your task, and it will be returned as status 400 with the wp error info.\n\n```php\nuse Morningtrain\\WP\\Async\\Abstracts\\AbstractAsyncTask;\n\nclass TestTask extends AbstractAsyncTask {\n    public static function handle($arg1, $arg2) {\n        // Do something;\n        \n        $somethingWentWrong = true;\n        \n        if ($somethingWentWrong) {\n            return new \\WP_Error('something_went_wrong', 'Something went wrong');\n        }\n        \n        return \"$arg1 $arg2\";\n    }\n}\n```\n\nYou can also throw a Throwable (Exception), and it will be returned as status 500 with the exception message.\n\n```php\nuse Morningtrain\\WP\\Async\\Abstracts\\AbstractAsyncTask;\nuse Exception;\n\nclass TestTask extends AbstractAsyncTask {\n    public static function handle($arg1, $arg2) {\n        // Do something;\n        \n        $somethingWentWrong = true;\n        \n        if ($somethingWentWrong) {\n            throw new Exception('Something went wrong');\n        }\n        \n        return \"$arg1 $arg2\";\n    }\n}\n```\n\nAlternatively you can return your own json response, if you need another response code.\n\n```php\nuse Morningtrain\\WP\\Async\\Abstracts\\AbstractAsyncTask;\n\nclass TestTask extends AbstractAsyncTask {\n    public static function handle($arg1, $arg2) {        \n        if (!current_user_can('manage_options')) {\n            wp_send_json_error('You are not allowed to do this!', 401);\n            exit;\n        }\n        \n        // Do something;\n        \n        return \"$arg1 $arg2\";\n    }\n}\n```\n\n## Contributing\n\nThank you for your interest in contributing to the project.\n\n### Bug Report\n\nIf you found a bug, we encourage you to make a pull request.\n\nTo add a bug report, create a new issue. Please remember to add a telling title, detailed description and how to reproduce the problem.\n\n### Support Questions\n\nWe do not provide support for this package.\n\n### Pull Requests\n\n1. Fork the Project\n2. Create your Feature Branch (git checkout -b feature/AmazingFeature)\n3. Commit your Changes (git commit -m 'Add some AmazingFeature')\n4. Push to the Branch (git push origin feature/AmazingFeature)\n5. Open a Pull Request\n\n## Contributors\n\n- [Martin Schadegg Brønniche](https://github.com/mschadegg)\n- [All Contributors](../../contributors)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE) for more information.\n\n\n---\n\n\u003cdiv align=\"center\"\u003e\nDeveloped by \u003cbr\u003e\n\u003c/div\u003e\n\u003cbr\u003e\n\u003cdiv align=\"center\"\u003e\n\u003ca href=\"https://morningtrain.dk\" target=\"_blank\"\u003e\n\u003cimg src=\"https://morningtrain.dk/wp-content/themes/mtt-wordpress-theme/assets/img/logo-only-text.svg\" width=\"200\" alt=\"Morningtrain logo\"\u003e\n\u003c/a\u003e\n\u003c/div\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmorning-train%2Fwp-async","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmorning-train%2Fwp-async","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmorning-train%2Fwp-async/lists"}