/zenaton-laravel

🐘 Laravel package to run and orchestrate background jobs with Zenaton Workflow Engine.

Primary LanguagePHPApache License 2.0Apache-2.0

⚠️ This repository is abandoned.


Easy Asynchronous Jobs Manager for Developers
Explore the docs »
Website · Examples in PHP · Tutorial in PHP

Packagist License

Zenaton for Laravel

Zenaton helps developers to easily run, monitor and orchestrate background jobs on your workers without managing a queuing system. In addition to this, a monitoring dashboard shows you in real-time tasks executions and helps you to handle errors.

The Zenaton Laravel package lets you code and launch tasks in your Laravel project, using Zenaton platform, as well as write workflows as code. You can sign up for an account on Zenaton and go through the tutorial in PHP.

PHP Documentation

You can find all details on Zenaton's website.

Table of contents

Getting started

Installation

Install the Zenaton Agent

To install the Zenaton agent, run the following command:

curl https://install.zenaton.com/ | sh

Then, you need your agent to listen to your application. To do this, you need your Application ID and API Token. You can find both on your Zenaton account.

Put them in your .env file:

ZENATON_APP_ID=YourAppId
ZENATON_API_TOKEN=YourApiToken
ZENATON_APP_ENV=dev

Now, to make the agent listen, run the following command:

zenaton listen --laravel

Install the Laravel package

To add the latest version of the package to your project, run the following command:

composer require zenaton/zenaton-laravel

Then, publish the default configuration file using the following command:

php artisan vendor:publish --tag=zenaton-config

Quick start

Executing a background job

A background job in Zenaton is a class implementing the Zenaton\Interfaces\TaskInterface interface.

Let's start by implementing a first task printing something, and returning a value. You can generate tasks using the zenaton:make:task artisan command:

php artisan zenaton:make:task HelloWorldTask

Open the app/Zenaton/Tasks/HelloWorldTask.php file that was generated for you and implement the ::handle() method as the following:

namespace App\Zenaton\Tasks;

use Zenaton\Interfaces\TaskInterface;
use Zenaton\Traits\Zenatonable;

class HelloWorldTask implements TaskInterface
{
    use Zenatonable;
    
    public function handle()
    {
        echo "Hello World\n";

        return mt_rand(0, 1);
    }
}

Now, when you want to run this task as a background job, you need to do the following:

(new \App\Zenaton\Tasks\HelloWorldTask())->dispatch();

That's all you need to get started. With this, you can run many background jobs. However, the real power of Zenaton is to be able to orchestrate these jobs. The next section will introduce you to job orchestration.

Orchestrating background jobs

Job orchestration is what allows you to write complex business workflows in a simple way. You can execute jobs sequentially, in parallel, conditionally based on the result of a previous job, and you can even use loops to repeat some tasks.

We wrote about some use-cases of job orchestration, you can take a look at these articles to see how people use job orchestration.

Using workflows

A workflow in Zenaton is a class implementing the Zenaton\Interfaces\WorkflowInterface interface.

We will implement a very simple workflow:

First, it will execute the HelloWorld task. The result of the first task will be used to make a condition using an if statement. When the returned value will be greater than 0, we will execute a second task named FinalTask. Otherwise, we won't do anything else.

One important thing to remember is that your workflow implementation must be idempotent. You can read more about that in our documentation.

Let's generate the workflow class to be able to work on it using the corresponding artisan command:

php artisan zenaton:make:workflow MyFirstWorkflow

Open the app/Zenaton/Workflows/MyFirstWorkflow.php file that was generated for you and implement the ::handle() method as the following:

namespace App\Zenaton\Workflows;

use Zenaton\Interfaces\WorkflowInterface;
use Zenaton\Traits\Zenatonable;

class MyFirstWorkflow implements WorkflowInterface
{
    use Zenatonable;

    public function handle()
    {
        $n = (new HelloWorldTask())->execute();
        if ($n > 0) {
            (new FinalTask())->execute();
        }
    }
}

Now that your workflow is implemented, you can execute it by calling the dispatch method:

(new \App\Zenaton\Workflows\MyFirstWorkflow())->dispatch();

If you really want to run this example, you will need to implement the FinalTask task.

There are many more features usable in workflows in order to get the orchestration done right. You can learn more in our documentation.

Getting help

Need help? Feel free to contact us by chat on Zenaton.

Found a bug? You can open a GitHub issue.