0

I'm trying to set up a custom scheduled task and I want to utilise the Rules parameters as part of my logic to get other records from the database.

So my question is how do I get the json rules? I've tried the following in my mytaskTask.

private function mytaskTask(ExecuteTaskEvent $event) : int
{
    // Your task goes here
    $taskType  = $event->getArgument('routineId');

    // get the rules
    $db    = $this->getDatabase();
    $query = $db->getQuery(true);
    $query->select(' execution_rules ' );
    $query->from($db->quoteName('#__scheduler_tasks'));
    $query->where($db->quoteName('type').' = '.$db->quote($taskType) );
    $db->setQuery($query);
    try {
        $rules = $db->loadResult();
        $this->logTask('Retrieved task rules', 'notice');
    } catch (RuntimeException $e) {
        $this->logTask($e->getMessage());
        return Status::KNOCKOUT;
    }
    $rulesObj = json_decode($rules);
    $ruleType = $rulesObj->rule-type;

UPDATED: So now I'm able to get the rules as a JSON string in $rulesObj however when I try to get the rule type I get the terminated dialogue box with the message:

JSON.parse: unexpected character at line 1 column 1 of the JSON data

When I display the variable $rulesObj I see the result as:

stdClass Object
(
    [rule-type] => interval-days
    [interval-days] => 1
    [exec-day] => 21
    [exec-time] => 05:19
)

As usual, all guidance appreciated.

2 Answers 2

1

Apparently, the entire task object is passed to the event but there is no proper method for retrieving it. So you have to resort to using getArgument():

$rules = $event->getArgument('subject')->get('execution_rules');

If you need the JSON string, simply run the result through json_encode().

4
  • Ahh ok that's easier than doing the whole DB query, BUT I'm still faced with how to get the rule-type value and the interval-?? value from the object that is returned in the variable $rules? Commented Apr 23 at 6:59
  • It's a simple object, access the properties directly. Though since properties contain dashes, you need to use curly brace syntax, e.g. $rules->{'rule-type'}.
    – Sharky
    Commented Apr 23 at 7:19
  • Actually on the list view if I use $rules = json_decode($item->execution_rules); and then try to display $rules->rule-type I get the error Undefined constant "type" against the line with this display. Commented Apr 23 at 7:21
  • Ahhhh, thank you I didn't know that. Commented Apr 23 at 7:23
1

Looking at the code in /administrator/components/com_scheduler/src/Task/Task.php the execution_rules aren't included in the event. So going from the code there:

$event = AbstractEvent::create(
    'onExecuteTask',
    [
        'eventClass'      => ExecuteTaskEvent::class,
        'subject'         => $this,
        'routineId'       => $this->get('type'),
        'langConstPrefix' => $this->get('taskOption')->langConstPrefix,
        'params'          => $this->get('params'),
    ]
);

I think you'd need to get the routineId which seems to match the 'type' column in the database table:

[$subject, $routineId, $langConstPrefix, $params] = array_values($event->getArguments());

Then you'd need to find the execution_rules either by doing a database query along the lines of:

select execution_rules from #__scheduler_tasks where type = $routineId; 

or you could load the record via the TaskTable class in Joomla\Component\Scheduler\Administrator\Table\TaskTable, and that should set up the execution_rules property of the Table instance.

I haven't checked all this though...

1
  • Hi Robbie, yes this looks to be the only way, I was hoping for some direct method but as this was a SOC project I'm guessing there could be some enhancements to make this a little easier to incorporate into 3rd party packages. Commented Apr 22 at 23:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.