XF 2.2 How to make a cron-job ?

Robert9

Well-known member
What i have now:

1. cron.xml

Code:
<?xml version="1.0" encoding="utf-8"?>
<cron>
  <entry entry_id="lala" cron_class="Robert9\Thumber\Cron\RenewThumb" cron_method="renewThumb" active="1"><![CDATA[{"day_type":"dom","dom":[-1],"hours":[-1],"minutes":[30,40,50]}]]></entry>
</cron>

This job has a name: lala
We have a class: Robert9\Thumber\Cron\RenewThumb
There we call a method: renewThumb

2. Cron class from above

Code:
<?php

namespace Robert9\Thumber\Cron;

class RenewThumb
{
    public static function renewThumb()
    {
        $app = \XF::app();

        /** @var Robert9\Thumber\Job\Thumbnail */
        \XF::app()->jobManager()->enqueueUnique(
            'thThumberRenew' . uniqid(),
            'Robert9\Thumber:Thumbnail',
            [],
            false
        );
    }
}

We set an "app" = something that works?
We give it a unique name, then we say go and use the class Robert9\Thumber\Job\Thumbnail

3. Because this is a longer job, we use AbstractRebuildJob; idea: walk tru with $start and $batch
this needs three methods: getNextIds, rebuildById and getStatusType

Code:
<?php

namespace Robert9\Thumber\Job;

use XF\Job\AbstractRebuildJob;
// the class called by cron.xml
class Thumbnail extends AbstractRebuildJob
{
    protected function getNextIds($start, $batch)
    {
        $db = $this->app->db();
// for testing i want 1 not 100 as $batch!
        $batch = 1;
        return $db->fetchAllColumn($db->limit(
            "
        fetch one/some ids

            ", $batch
        ), $start);
    }

Here i have checked the result, it was "117" like i want to have it, but now
the machine just should send this "117" to rebuildById($id), but it never comes to this method!

If return $db-> has a value (here "117"; it should automatically call rebuildById(117), right?
But the script never reaches my next BREAKPOINT in PHPSTORM with XDEBUG.

Code:
protected function rebuildById($id)
{
    $resource = \XF::finder('XF:ResourceItem')->whereId($id)->fetchOne();
[BREAKPOINT] $b = $resource;
    // fetch the thumb by url
    do more

    }

Maybe it is a problem, that i return one and not 100 values?

Because in the next round $start is still 0
 
Last edited:
Top Bottom