Jump to content

Lazycron routine in autoload module throws error


Ken Muldrew
 Share

Recommended Posts

I'm trying to get a short routine to run once per day that will look at some pages and send a reminder email when that customer's subscription (yearly) is about to expire. When I run the code in a template then it works without issue, but inside my lazycron service routine, I get an "Error: Uncaught Error: Call to a member function get() on null" as if the database cannot be found. My autoload module is just the sample HelloWorld module included with ProcessWire, editted to perform this task. The whole of it is included below (I've stripped out the code that generates the email because it never gets past $pages->find):

 

<?php namespace ProcessWire;
/**
 * ProcessWire 'LazyCronLoad'  module
 *
 */
class LazyCronLoad extends WireData implements Module {

    public static function getModuleInfo() {
        return array(
            'title' => 'LazyCronLoad', 
            'version' => 1, 
            'summary' => 'Just loads a lazy cron callback.',
            'singular' => true, 
            'autoload' => true, 
            );
    }

    public function init() {
        // initialize the hook in the AutoLoad module
        $this->addHook('LazyCron::everyDay', $this, 'myHook');
        
    }

    public function myHook(HookEvent $e) {
        // called once per day
        wire('log')->save('user_activities',' lazy cron service routine');
            $transport_pages = $pages->find("template=aggregate-entry, aggregate_type.title='Transport'");
            foreach ($transport_pages as $page) {
                if (($page->purchase_date + 30325800 < time()) && ($page->purchase_date + 30412600 > time())) { // between 351 and 352 days
                    wire('log')->save('user_activities', $page->id . ' email reminder sent');
                    // send email
            }
        }
    }        
    
}

The first wire('log') shows up but the second one doesn't (the purchase_date condition is met (as demonstrated by running the code in a template close in time to when the lazycron routine executes)). The error log gives the Uncaught Error shown above. 

I think this is a beginner's mistake with something obvious being missed and would be grateful for any assistance in fixing it.

Link to comment
Share on other sites

This is the line where it crashes:

$transport_pages = $pages->find("template=aggregate-entry, aggregate_type.title='Transport'");

Do you mean it should be this instead?:

$transport_pages = wire('$pages')->find("template=aggregate-entry, aggregate_type.title='Transport'");

Link to comment
Share on other sites

The API vars, $pages, $log, $sanitizer, $fields, etc aren't automatically instantiated inside custom functions. You may want to start your function eg like:

$log = wire('log');
$pages = wire('pages');
...

then you can use them as normal

  • Like 1
Link to comment
Share on other sites

Just a quick heads-up: I'm moving this thread to the "Module/Plugin Development" subforum. The main Modules/Plugins area is intended for support threads of existing modules, while questions regarding module development belong to said subarea 🙂

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...