XF 2.3 Creating an add on using XF's native cron system

frm

Well-known member
I'm creating a simple "Happy birthday" add on that replies to the thread owned by the user whose birthday it is (to try and learn cron jobs mainly). I'm able to make replies, etc., using unconventional methods (directly starting XF.php and running queries to post replies), but trying to put it into an add on with the appropriate structure to run it as an installable add on using XF's native cron job system.

I could keep it all in 1 file and use *nix cron jobs to call it, but I'm trying to build an add on and not a separate file that XF doesn't recognize in any way.

The file structure is:
Code:
/
└── upload
    └── src
        └── addons
            └── FRM
                └── BirthdayPost
                    ├── Setup.php
                    ├── addon.json
                    ├── Cron
                    │   └── Birthday.php
                    └── _data
                        └── cron.xml

As I get this error on installation:
Exception: Callback FRM\BirthdayPost\Cron\Birthday::postBirthday is invalid (error_invalid_class).

cron.xml:
XML:
<?xml version="1.0" encoding="utf-8"?>
<cron>
    <entry entry_id="frm_birthday_post" cron_class="FRM\BirthdayPost\Cron\Birthday" cron_method="postBirthday" active="1">
        <![CDATA[ {"day_type":"dom","dom":[-1],"hours":[3],"minutes":[0]} ]]>
    </entry>
</cron>

Setup.php
PHP:
<?php

namespace FRM\BirthdayPost;

Birthday.php
PHP:
<?

namespace FRM\BirthdayPost\Cron;

class Birthday
{
    public static function postBirthday()
    {

    }
}

It installs fine once cron.xml is removed.

So, I've tried adding it as a manual cron in the ACP linking it to the add on.

1739684612969.webp

And it just stays on the same page with the loading bar with this in the console:
Code:
Uncaught (in promise) SyntaxError: Unexpected token '<', "<?

namesp"... is not valid JSON

I'm at a loss for what to do as I'm following add ons that do create cron jobs.

Does it require a function to create a cron job? Does it require putting it directly into the database (xf_cron_entry) over cron.xml?

I can't seem to reverse engineer any add on that creates a cron job that doesn't make use of /_data/cron.xml.

Thanks in advance.
 
Should be: <?php at the start of Birthday.php - you've got <?

... but otherwise it looks okay - you need a static function as the Cron callback, which you have - so it should work if you fix the php bit at the start.
 
  • Love
Reactions: frm
Should be: <?php at the start of Birthday.php - you've got <?
As simple as that. Lazy programming. I typically start all my independent PHP scripts with <? because my system will recognize it as a PHP script and run it.

Looks like XenForo requires it though, as I'm in action with a cron job. Sure wish there were some errors, but likely everyone coding is using an IDE that'll stuff <?php, so it's probably not that big of a deal.

1739702930023.webp

Now on to find out if the cron.xml handles phrasing for the title/description or if it comes from phrases.xml. I sure hope not the latter as I'm doing this by hand and phrases.xml looks like it needs a version id, which will frequently get bumped as I move on.

But at least it's there!

I'll probably get it functioning properly on the cron job and then look into phrasing it as it looks icky, even though it's just for me.

Thank you, @Sim!

Edit: Looks like phrases.xml isn't strict on phrasing as I'm on 1.0.4 and successfully phrased cron_entry.birthday_post as 1.0.0. I'm sure it's required to bump the version if I ever change the phrase to update potential caches if the phrase ever changes. Looking at other add ons, the version number is all over the place, so I can only assume a developer has to bump the phrase version to the "newest" release when a phrase is changed so it updates the cache. Suffice it to say, the phrase likely won't ever change and works as is.
 
Last edited:
Have you seen https://xenforo.com/docs/dev/ ??

You'll probably want to do this all via Development Mode - https://xenforo.com/docs/dev/development-tools/

You shouldn't be editing the XML files directly - you edit the various parts via the Admin CP and it creates them for you.

Versions for phrases and templates and such don't typically matter - they relate to the version you were using when they were created, so that you'll know if things are out of date if you've edited a core template and then installed an update which updated that template. Again, you don't typically change those by hand - the system takes care of it for you when you update things via the Admin CP.
 
  • Like
Reactions: frm
I've been doing it file by file (manually upping the version in addon.json), not using xf:build.

I've successfully created it to execute the task and set some birthdays to see if it will run overnight as it does run and if double-run won't create a 2nd reply.

I'm in development mode, but was unaware that it'd create the files. I did phrases.xml on my own as it was just the 1 and only 1 ever needed for this.

I want to get more knowledgeable in development before investing in an IDE and setting up a local test environment.

As of now, I have been manually editing the files and packaging them to be installed and upgraded via the ACP installer.

I will surely go over those docs though if it can make it a bit easier using the build function!
 
Back
Top Bottom