Agents

Launch

To have your agent listening for tasks to execute, just type:

zenaton listen <parameters>

Your parameters can be in a .env file, then you just need to provide it:

zenaton listen --env=<path/to/.env/file>

or can be inlined:

zenaton listen --<name>=<value>

If you mix both notations, inlined parameters will be prioritized.


Parameters

NameInlinedDefaultDescription
env./.envlocation of .env file containing configuration
boot- location of boot file containing initialisation needed to be done before executing a task. This typically depends on your language. See Below.
ZENATON_APP_IDapp_id- Your application id - to listen only workflows started by your application (cf. Zenaton API section);
ZENATON_API_TOKENapi_token- your API token - to prove you have rights to access above application (cf. Zenaton API section);
ZENATON_APP_ENVapp_env- your Zenaton environment - to distinguish workflows launched in production from workflows launched during development - typically "production", "staging", "dev" or your first name).
ZENATON_CONCURRENT_MAXconcurrent_max10 Maximum number of tasks that the agent can execute concurrently
ZENATON_HANDLE_ONLYhandle_onlyall List of classes that the agent should handle and only them - (comma-separated strings)
ZENATON_HANDLE_EXCEPThandle_exceptnone List of classes that the agent should NOT handle - (comma-separated strings)

Boot file

The Zenaton agent will load your boot file before executing required tasks. This boot file should typically contains a require "./vendor/autoload.php" instruction, as well as the loading of your own classes (see the examples-php repository on Github.) If you use a framework, you may also need to add instructions to boot it. For you convenience, we already provide some integrations below.

If you use a framework, you may also need to add instructions to boot it.


Laravel

If you are using Laravel (from v4), typing will configure you agent with your local .env file and an adhoc boot file.

zenaton listen --laravel

Currently, you can not use automatic dependency injection within your tasks, but you can use facades or other helpers functions.


Symfony

Supported versions of Symfony are 2.7, 2.8, 3.4, 4.0 and 4.1

In order to start using Zenaton with your Symfony application, you need to require the Zenaton PHP library in your composer dependencies.

composer require zenaton/zenaton-php

Then, you will need to initialize the Zenaton Client of the library using your credentials. The easiest way to do it is in the ::boot() method of your Symfony Kernel class. If you are using environment variables in your application, you can use the following implementation:

// app/AppKernel.php or src/Kernel.php
namespace App;

class Kernel
{
    // [...]

    public function boot()
    {
        parent::boot();

        \Zenaton\Client::init(
            getenv('ZENATON_APP_ID'),
            getenv('ZENATON_API_TOKEN'),
            getenv('ZENATON_APP_ENV')
        );
    }

    // [...]
}

If you are using a parameters.yml file, you can use the following implementation:

// app/AppKernel.php or src/Kernel.php
namespace App;

class Kernel
{
    // [...]

    public function boot()
    {
        parent::boot();

        \Zenaton\Client::init(
            $this->container->getParameter('zenaton_app_id'),
            $this->container->getParameter('zenaton_api_token'),
            $this->container->getParameter('zenaton_app_env')
        );
    }

    // [...]
}

Make sure the corresponding environment variables or parameters are available!

You can use any other implementation, as long as you are able to retrieve the correct values for these parameters and call the \Zenaton\Client::init() method.

You can now start your Zenaton worker by typing:

It will configure your worker with your local .env file and an adhoc boot file.

zenaton listen --symfony

Currently, you can not use automatic dependency injection within your tasks, but we provide a container() global function that returns an instance of your Symfony service container.

The only safe place to use the container() function is in your task handle() method. Because the tasks are serialized and unserialized during the workflow execution, if you use the container() function to retrieve some services and store them as task properties, they may not be able to be serialized correctly and will likely not work when using them. To avoid any potential issue, only store services in local variables of the handle().

E.g. to access your User repository:

// MyTask.php
class MyTask
{
    public function handle()
    {
        $repo = container()->get('doctrine')->getManager()->getRepository(User::class);
    }
}

The --symfony option will try to boot a Symfony application by executing the following steps:

  1. Load the autoload file. It will first look for a file named app/autoload.php, or use the default composer autoload file vendor/autoload.php.
  2. If the APP_ENV environment variable is not defined, it will try to load the .env file at the root of your project if it exists and you have the Dotenv component available in your project dependencies. Otherwise the prod environment is assumed.
  3. Get the class name of your Symfony Kernel, by looking for a class named \App\Kernel or \AppKernel, instanciate it, and boot it.

If your project does not use the default autoloading mechanism, or the standard Kernel class, you will have to provide your own boot file and use the --boot option instead of --symfony:

zenaton listen --boot=zenaton.php

In your boot file, make sure you do the following steps:

  • Require your autoload file.
  • Instantiate your kernel using the app environment you want
  • Call the ::boot() method on your kernel instance.
  • Return the kernel instance

Rails

If you are using Rails, you should create an initializer in config/initializers/zenaton.rb like this one.

Zenaton::Client.init(
  Rails.application.credentials.zenaton[:app_id],
  Rails.application.credentials.zenaton[:api_token],
  Rails.env.production? ? 'production' : 'dev'
)

Then from your rails application directory, you can launch your zenaton agent with zenaton listen --rails. This will load your rails application code and your local .env file.


Django

If you are using Django, you should write a script to setup Django (e.g django_setup.py) containing:

import django
import os

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_example.settings')
django.setup()

Then import this script in your boot file and in the script your are dispatching your Workflows from: import setup_django. This will boot the Django framework when needed.

You could include the Zenaton client initialization instructions in the ready() function of your AppConfig.

You can find a full working example of Zenaton's integration with Django here.