{"id":16954356,"url":"https://github.com/nanosector/tasks","last_synced_at":"2025-03-21T13:48:56.314Z","repository":{"id":42448337,"uuid":"96789254","full_name":"NanoSector/tasks","owner":"NanoSector","description":"Simple task controller for long-running tasks","archived":false,"fork":false,"pushed_at":"2023-12-01T04:10:46.000Z","size":84,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-26T19:47:35.483Z","etag":null,"topics":["php","task","tasks"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/NanoSector.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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":"2017-07-10T14:51:13.000Z","updated_at":"2022-04-05T16:20:43.000Z","dependencies_parsed_at":"2024-11-27T22:30:59.157Z","dependency_job_id":null,"html_url":"https://github.com/NanoSector/tasks","commit_stats":{"total_commits":25,"total_committers":6,"mean_commits":4.166666666666667,"dds":0.48,"last_synced_commit":"ac6a2ac548b10814febe59fbe91ce89f6a3ed77f"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NanoSector%2Ftasks","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NanoSector%2Ftasks/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NanoSector%2Ftasks/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NanoSector%2Ftasks/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/NanoSector","download_url":"https://codeload.github.com/NanoSector/tasks/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244807722,"owners_count":20513694,"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":["php","task","tasks"],"created_at":"2024-10-13T22:09:30.052Z","updated_at":"2025-03-21T13:48:56.273Z","avatar_url":"https://github.com/NanoSector.png","language":"PHP","readme":"# Task Controller\n[![Build Status](https://scrutinizer-ci.com/g/Yoshi2889/tasks/badges/build.png)](https://scrutinizer-ci.com/g/Yoshi2889/tasks/build-status/master)\n[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/Yoshi2889/tasks/badges/quality-score.png)](https://scrutinizer-ci.com/g/Yoshi2889/tasks/?branch=master)\n[![Scrutinizer Code Coverage](https://scrutinizer-ci.com/g/Yoshi2889/tasks/badges/coverage.png)](https://scrutinizer-ci.com/g/Yoshi2889/tasks/code-structure/master/code-coverage)\n[![Latest Stable Version](https://poser.pugx.org/yoshi2889/tasks/v/stable)](https://packagist.org/packages/yoshi2889/tasks)\n[![Latest Unstable Version](https://poser.pugx.org/yoshi2889/tasks/v/unstable)](https://packagist.org/packages/yoshi2889/tasks)\n[![Total Downloads](https://poser.pugx.org/yoshi2889/tasks/downloads)](https://packagist.org/packages/yoshi2889/tasks)\n\nSimple task controller supporting multiple types of tasks.\n\n## Installation\nYou can install this class via `composer`:\n\n```composer require yoshi2889/tasks```\n\n## Usage\nCreate an instance of `TaskController` and add any instance of `TaskInterface` to it:\n\n```php\n\u003c?php\n\n$loop = React\\EventLoop\\Factory::create();\n$taskController = new \\Yoshi2889\\Tasks\\TaskController($loop);\n\n// A simple callback task, run only once, which will trigger in 10 seconds:\n$callbackTask = new \\Yoshi2889\\Tasks\\CallbackTask(function ()\n{\n\techo 'Hello world!' . PHP_EOL;\n}, 10);\n// Output (after 10 seconds): Hello world!\n```\n\n### Repeatable Tasks\nA `RepeatableTask` instance is a Task which runs its child task on an interval. Before a `RepeatableTask` starts\n running the child task, it first checks if its expiry time has passed. If it has not, it will not run the task.\n \nFor example, to create a new `RepeatableTask` that runs the previous `CallbackTask` every 5 seconds:\n```php\n$repeatableTask = new \\Yoshi2889\\Tasks\\RepeatableTask($callbackTask, 5);\n```\n\nThis task would only start running after the 10 seconds defined before in the `CallableTask` have passed.\n\n### Cancelling Tasks\nTasks can be cancelled prematurely. How a cancel is handled depends on the task type.\nFor instance, if we cancel a RepeatableTask, it will internally cancel its child task and stop repeating,\nafter which it will be discarded:\n\n```php\n$repeatableTask-\u003ecancel();\n```\n\nHowever, if we were to cancel a `CallbackTask`, it will just be put in a state where it can no longer be run\n by the `TaskController` and will thus be discarded on the next run.\n \n`TaskController` must never cancel tasks on its own, this is up to the user.\n \n### Discarding Tasks\nBy default, a Task which does not return a new Task on its run() method will be discarded.\nHowever, if a Task does pass back a new Task, the original Task itself gets discarded, but\nthe Task instance which is passed back will be added in its place. We can observe this with\nthe following snippet:\n\n```php\n$callbackTask = new \\Yoshi2889\\Tasks\\CallbackTask(function ()\n{\n\techo 'Hello ';\n\treturn new \\Yoshi2889\\Tasks\\CallbackTask(function ()\n\t{\n\t\techo 'world!' . PHP_EOL;\n\t}, 5); \n}, 5);\n// Output (after 10 seconds): Hello world!\n```\n\nDiscarded tasks will be removed from the `TaskController`.\n\n## Implementing custom Tasks\nThe `TaskController` accepts any class that implements the `TaskInterface` interface. This interface contains the following methods:\n\n* `getExpiryTime(): int`: Gets the UNIX timestamp on which the task should be run and discarded afterwards. Please note that\n by default, `TaskController` runs at a 1-second interval and timing might be slightly off.\n* `run(): ?TaskInterface`: Runs the actual task. Return an object implementing `TaskInterface` to insert a new task, or `null`\n to discard the current task.\n* `cancel(): void`: Used to cancel the task, or to bring it in a state where it cannot be run. It is a good idea to have\n`getExpiryTime()` always return 0 after this method is called so that the task will be discarded.\n\n## License\nThis code is released under the MIT License. Please see `LICENSE` to read it.","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnanosector%2Ftasks","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnanosector%2Ftasks","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnanosector%2Ftasks/lists"}