Creating a Custom Task

Hi all,

I am trying to create a custom task and change a setting on the database.
I am able create task and link to a php and run it but it doesn’t work.

I would like to use the TaskTrait from other tasks but I am not sure if it is possible with Xibo\Custom namespace.

Hey there,

We would need a bit more details on what you want to achieve, what you have and what exactly does not work please.

CMS version you have would be helpful as well.

Yes, you can use TaskTrait in your custom task, in fact, you most likely want to use it :slight_smile: .

If you look at the example here - Tasks - Extend Xibo | Xibo Open Source Digital Signage

You need to create the .task file as explained in the link.
Then create the task file itself, which if we follow the example from docs, at the very least needs this as base

<?php


namespace Xibo\Custom;


use Xibo\XTR\TaskInterface;
use Xibo\XTR\TaskTrait;

class CustomTask implements TaskInterface
{
    use TaskTrait;

    public function run()
    {
        // TODO: Implement run() method.
    }

    public function setFactories($container)
    {
        // TODO: Implement setFactories() method.
    }

}

You can have a look at one of the existing tasks to see how the workflow should work.

Once you’re happy you can go to Task page in your CMS, click add Task and select your custom task from the dropdown, specify cron schedule and options if needed.

Hi Peter,

Thank you for the answer.

I have tried on last few versions of cms (3.0.3 and 2.3.11).
I have a very basic task for now as shown below.

<?php

namespace Xibo\Custom;

use Xibo\XTR\TaskInterface;
use Xibo\XTR\TaskTrait;

class CustomTask implements TaskInterface
{
    use TaskTrait;

    public function run()
    {
        $this->runMessage = "test" . PHP_EOL . PHP_EOL;
        $this->log->debug("test");
    }
}

Ever get this to work? Doesn’t work for us that’s for sure. And there is a complete lack of documentation on the website. Devs mention the .task file but nothing about the .php and how to implement it. They also have conflicting info depending on how far back in the forums you go

@dan
Instead of the skeleton function above, can you provide us with a working example either in here or on the Task page documentation (Tasks - Extend Xibo | Xibo Digital Signage)? You link to it even, and it’s very incomplete. As it stands right now, latest version of the CMS in a Docker container, and even the simplest of tasks outputs nothing to the log, other than it ran. Why does $this->log->debug(“Test info”) not actually do anything?

1 Like

I ran into the same frustration about lack of documentation.

This one I actually learned by trial-and-error - the debug() part means it only shows up when you have debug logging enabled.

Hey both,

You’re absolutely right that we need to improve documentation for tasks, and our developer documentation more generally.

We use Monolog for logging, and you’re right the debug logs are only recorded when the CMS is in debug mode. For development it actually makes the most sense to put the CMS in “Test” mode via admin → settings, that way you get maximum logging and error output.

I will try to that! What are you actually trying to achieve with your task? perhaps I can tailor the example somewhat?