Tina4 Services

Tina4 includes a way to define services that will run in the background. This allows you to execute code while without having to define CRON jobs.

Let's create a service that sends messages. I'll outline the service bit here. Have a look at Sending Email messages with Tina4 for more information about sending email.

Ensure your composer.json includes this line: “tina4service”: “@php bin/tina4service”,

Define your First Service

<?php


class MessengerProcess extends \Tina4\Process implements \Tina4\ProcessInterface 
{

    public string $name = "My Service";

    function run () 
    {
        echo "Sending Messages";
        $messages = (new MessageQueue)->load();
        
        foreach ($messages as $message)
        {
          // Send messages
        }


    }

    function canRun(): bool
    {
       $haveMessages = (New MessageQueue)->load();
   
       
       if ($haveMessages)
       {
          //We have email messages queued, let the task run
          return true;
       }
       else
       {
          //We have no messages queued
          return false;
       }
}

Queue the Service Process

$service = (new Tina4\Service);
$service->addProcess(new MessengerProcess("My Process"));

Remove the Service Process from the Queue

(new Tina4\Service)->removeProcess("My Process");

Start Tina4Service

You need to add execute permissions on Linux for the tina4service to allow it to execute from the project folder.

Change the permissions for bin/tina4service in your project directory

chmod +x bin/tina4service

Start the Service

composer start-service &
Powered by ComboStrap