XF 2.2 Questions about CRON task

tpmedia

Member
I took over a XenForo site a while ago and since a Xenforo upgrade a cron entry has been throwing errors. A fair amount of googling has not got me anywhere and I always feel like I am missing something in terms of Xenforo developer documentation, I never seem to find anything along the lines of "here's how to write a cron task script".
Anyway I modified the failing script to remove the line that I thought was causing the error and now the the script is still failing and Xenforo is raising a warning that a file has unexpected content, presumably because I modified it at the command line rather than via the Admin panel. I couldn't find a way of modifying the cron callback vai the admin panel, is there an "official" way to do it?

The script, below is failing with
Code:
[E_NOTICE] Trying to get property 'title' of non-object
src/addons/MB/Classifieds/XF/Cron/ArchiveThreads.php:56

Any thoughts, help, pointers to tutorials, etc much appreciated


PHP:
namespace MB\Classifieds\XF\Cron;

use XF\Util\File;

class ArchiveThreads
{
    public static function process()
    {
        $daysBeforeArchive = \XF::options()->mbcl_daysBeforeArchive;
        $timeNow = date('l d-m-Y H:i:s');
        $timeCode = \XF::$time;
        $lastPostDate = ($timeCode - ($daysBeforeArchive * 86400));

        self::log("===== Begin archiving classifieds threads =====");

        // get all forums and see if they have the archive enabled ticked
        // if so, add the forum id to an array
        $forumFinder = \XF::finder('XF:Forum');
        $classifiedsForums = $forumFinder
            ->where('mbcl_enabled', 1)
            ->where('mbcl_archive_forum', '>', 0)
            ->where('mbcl_complete_prefix', '>', 0)
            ->fetch();

        // process the forums one by one
        foreach ($classifiedsForums as $forum)
        {
            $threadFinder = \XF::finder('XF:Thread');
            $threads = $threadFinder
                ->where('node_id', $forum->node_id)
                ->where('last_post_date', '<', $lastPostDate)
                ->where('sticky', 0)
                ->where('prefix_id', '=', $forum->mbcl_complete_prefix)
                ->limit(100)
                ->fetch();
            $completedPrefix = \XF::finder('XF:ThreadPrefix')->where('prefix_id', $forum->mbcl_complete_prefix)->fetchOne();

            // if there are threads that match the criteria, lets archive them
            if (!empty($threads))
            {
                $numThreads = count($threads);
                $destForumId = $forum->mbcl_archive_forum;
                $destForum = \XF::finder('XF:Forum')->where('node_id', $destForumId)->fetchOne();

                self::log("- Moving "
                    . $numThreads
                    . " threads from --- "
                    . $forum->title
                    . " to --- "
                    . $destForum->title
                    . " ---");

                foreach ($threads as $thread)
                {
                    self::log("-- Archiving thread "
                        . $thread->thread_id
                        . ", "
                        . $thread->title);
                    // Move thread into relevant archive forum
                    $mover = \XF::app()->service('XF:Thread\Mover', $thread);
                    $mover->move($destForum);

                }
            }
        }
        self::log("===== End archiving classifieds threads =====");

    }

    private static function log($logEntry)
    {
        $file = File::canonicalizePath(\XF::app()->config('internalDataPath') . "/" . "cron_MB_Classifieds_Archive_Threads.log");
        if ($fp = @fopen($file, ('aw')))
        {
            fwrite($fp, date('Y-m-d H:i:s') . " " .$logEntry . "\n");
            fclose($fp);
            return true;
        }
    }


}
 

0815

Well-known member
Code:
         $destForum = \XF::finder('XF:Forum')->where('node_id', $destForumId)->fetchOne();

                self::log("- Moving "
                    . $numThreads
                    . " threads from --- "
                    . $forum->title
                    . " to --- "
                    . $destForum->title
                    . " ---");

This could be the mistake!

The error says that an object does not exist. Since you are querying a forum here but do not know whether it exists, the error could be here. You should check whether " $destForum" exists.


Anyway I modified the failing script to remove the line that I thought was causing the error and now the the script is still failing and Xenforo is raising a warning that a file has unexpected content,
This is normal. Since you have changed a file. Here you must correct the hash control hash.
 

tpmedia

Member
Thanks @0815 fair point about checking if $destFroum is null/empty. I have no idea how to "correct the hash control hash" which goes back to my question about where is this sort of thing documented?
 

0815

Well-known member
I have no idea how to "correct the hash control hash" which goes back to my question about where is this sort of thing documented?


Not at all - it is also not intended to work this way!
You can hash the content of the file using sha256.... Then transfer the hash into the "hashes.json" file to the add-on. Or you can create a completely new build through XF and get a clean package.

 
Top