A PHP implementation of a bare task loop.

Overview

TaskLoop

A PHP implementation of a bare task loop.

Installation.

$ composer require thenlabs/task-loop 1.0.x-dev

Usage.

The file example.php contains the below code which show that once the loop is started, it will runs each one of his tasks.

Each loop iteration is executed with a time interval which it's specified with the start() method.

Like can be seen, for create the tasks exists the class ThenLabs\TaskLoop\AbstractTask which has some useful methods, but any instance of the interface ThenLabs\TaskLoop\TaskInterface may be considered as a task.

<?php

require_once __DIR__.'/vendor/autoload.php';

use ThenLabs\TaskLoop\{AbstractTask, TaskLoop};

define('DATE_FORMAT', 'Y-m-d H:i:s');

// create a loop instance.
$loop = new TaskLoop();

// adds the task1.
$loop->addTask(new class extends AbstractTask {

    protected $counter = 10;

    public function run(): void
    {
        echo date(DATE_FORMAT)." Task1: {$this->counter}\n";
        $this->counter--;

        if ($this->counter <= 0) {
            echo date(DATE_FORMAT)." Task1: End\n\n";

            // when task1 ends, the loop will be stopped.
            $this->loop->stop();
        }
    }
});

// adds the task2.
$loop->addTask(new class extends AbstractTask {

    protected $counter = 5;

    public function run(): void
    {
        echo date(DATE_FORMAT)." Task2: {$this->counter}\n";
        $this->counter--;

        if ($this->counter <= 0) {
            echo date(DATE_FORMAT)." Task2: End\n\n";

            // when task2 ends, will be dropped from the loop.
            $this->loop->dropTask($this);
        }
    }
});

$delay = 1000000; // value for the usleep function.
$loop->start($delay);

echo 'Good bye!';

That file produce the next result:

2021-10-09 11:30:31 Task1: 10
2021-10-09 11:30:31 Task2: 5
2021-10-09 11:30:32 Task1: 9
2021-10-09 11:30:32 Task2: 4
2021-10-09 11:30:33 Task1: 8
2021-10-09 11:30:33 Task2: 3
2021-10-09 11:30:34 Task1: 7
2021-10-09 11:30:34 Task2: 2
2021-10-09 11:30:35 Task1: 6
2021-10-09 11:30:35 Task2: 1
2021-10-09 11:30:35 Task2: End

2021-10-09 11:30:36 Task1: 5
2021-10-09 11:30:37 Task1: 4
2021-10-09 11:30:38 Task1: 3
2021-10-09 11:30:39 Task1: 2
2021-10-09 11:30:40 Task1: 1
2021-10-09 11:30:40 Task1: End

Good bye!

Development.

Running the tests.

For run the tests, runs the next command:

$ ./vendor/bin/pyramidal
You might also like...
 Laravel-Tasks is a Complete Build of Laravel 5.2 with Individual User Task Lists
Laravel-Tasks is a Complete Build of Laravel 5.2 with Individual User Task Lists

An app of tasks lists for each individual user. Built on Laravel 5.2, using 5.2 authentication and middleware. This has robust verbose examples using Laravel best practices.

xcron - the souped up, modernized cron/Task Scheduler for Windows, Mac OSX, Linux, and FreeBSD server and desktop operating systems.

xcron is the souped up, modernized cron/Task Scheduler for Windows, Mac OSX, Linux, and FreeBSD server and desktop operating systems. MIT or LGPL.

Laravel Cron Scheduling - The ability to run the Laravel task scheduler using different crons

Laravel Cron Scheduling Laravel Task Scheduling is a great way to manage the cron. But the documentation contains the following warning: By default, m

Reset the live preset for debug settings with a task
Reset the live preset for debug settings with a task

Debug Settings Task This TYPO3 extension resets TYPO3 debug settings to the »live« preset on production using a scheduler task. Vision “Better safe th

PHP cron job scheduler

PHP Cron Scheduler This is a framework agnostic cron jobs scheduler that can be easily integrated with your project or run as a standalone command sch

PHP port of resque (Workers and Queueing)

php-resque: PHP Resque Worker (and Enqueue) Resque is a Redis-backed library for creating background jobs, placing those jobs on one or more queues, a

Elegant SSH tasks for PHP.

Laravel Envoy Introduction Laravel Envoy provides a clean, minimal syntax for defining common tasks you run on your remote servers. Using Blade style

Crunz is a framework-agnostic package to schedule periodic tasks (cron jobs) in PHP using a fluent API.

Crunz Install a cron job once and for all, manage the rest from the code. Crunz is a framework-agnostic package to schedule periodic tasks (cron jobs)

A PHP-based job scheduler
A PHP-based job scheduler

Crunz Install a cron job once and for all, manage the rest from the code. Crunz is a framework-agnostic package to schedule periodic tasks (cron jobs)

Owner
ThenLabs
We make open source software.
ThenLabs
Modern task runner for PHP

RoboTask Modern and simple PHP task runner inspired by Gulp and Rake aimed to automate common tasks: writing cross-platform scripts processing assets

Consolidation 2.6k Jan 3, 2023
Pure PHP task runner

task/task Got a PHP project? Heard of Grunt and Gulp but don't use NodeJS? Task is a pure PHP task runner. Leverage PHP as a scripting language, and a

null 184 Sep 28, 2022
Modern task runner for PHP

RoboTask Modern and simple PHP task runner inspired by Gulp and Rake aimed to automate common tasks: writing cross-platform scripts processing assets

Consolidation 2.6k Dec 28, 2022
🐺 Asynchronous Task Queue Based on Distributed Message Passing for PHP.

?? Asynchronous Task Queue Based on Distributed Message Passing for PHP.

Ahmed 36 Aug 11, 2022
A versatile and lightweight PHP task runner, designed with simplicity in mind.

Blend A versatile and lightweight PHP task runner, designed with simplicity in mind. Table of Contents About Blend Installation Config Examples API Ch

Marwan Al-Soltany 42 Sep 29, 2022
Modern and simple PHP task runner inspired by Gulp and Rake aimed to automate common tasks

RoboTask Modern and simple PHP task runner inspired by Gulp and Rake aimed to automate common tasks: writing cross-platform scripts processing assets

Consolidation 2.6k Jan 3, 2023
Awesome Task Runner

Bldr Simplified Build System/Task Runner Uses Yaml, JSON, XML, PHP, or INI for configs Quick Usage To develop, run ./script/bootstrap, and then ./scri

null 223 Nov 20, 2022
Flow Framework Task Scheduler

This package provides a simple to use task scheduler for Neos Flow. Tasks are configured via settings, recurring tasks can be configured using cron syntax. Detailed options configure the first and last executions as well as options for the class handling the task.

Flowpack 11 Dec 21, 2022
Manage your Laravel Task Scheduling in a friendly interface and save schedules to the database.

Documentation This librarian creates a route(default: /schedule) in your application where it is possible to manage which schedules will be executed a

Roberson Faria 256 Dec 21, 2022
Task Scheduling with Cron Job in Laravel

About Laravel Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experie

Shariful Islam 1 Oct 16, 2021