Jump to:

22972 Posts in 11596 Topics by 2824 members

General Questions

SilverStripe Forums » General Questions » Class not found when run through Cron job

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 796 Views
  • jayslippy
    Avatar
    Community Member
    10 Posts

    Class not found when run through Cron job Link to this post

    I'm trying to set up regular tasks for my website using the Cron job feature from my web provider. The problem I have is that I have an extension class (code copied below) which works perfectly when browsing the website, but produces a class not found error when code is run by the cron job command line. I've spent a long time doing basic checks, and I have another extension class in the same folder which is added in the same config file and has no problems. I used the class_exists function to output to an e-mail through the cron job and to one of my web pages, and confirmed that it returns true to the web, but false to my e-mail!

    Is there any difference in the way the code is built when run through command line compared to being accessed through a browser? Any reason why the code for this particular class would be ignored by my cron job even through the website finds it, builds it and uses the function it contains? Any suggestions on further tests I could run?

    In the code below the two php files are in the same folder. When I remove the first add_extension everything works fine, so it is building the VerificationExtension class fine, but FormFieldExtension causes the fatal error.

    In config.php

    Object::add_extension('FormField', 'FormFieldExtension');
    Object::add_extension('Page_Controller', 'VerificationExtension');

    In FormFieldExtension.php

    <?php

    class FormFieldExtension extends Extension {
       
       public function hasNoLabelClass() {
          if($self = $this->owner) {
             if($self->hasClass("nolabel")) {
                return true;
             }
          }
          return false;
       }
       
    }

    In VerificationExtension.php

    <?php

    class VerificationExtension extends Extension {

       public function Verified() {
          if($member = Member::CurrentMember()){
             if($member->RegistrationStatus > 1) {
                return true;
             }
          }
          return false;
       }

    }

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: Class not found when run through Cron job Link to this post

    Is there any difference in the way the code is built when run through command line compared to being accessed through a browser?

    Depends on how you are running it through through command line. Silverstripe uses URL rewriting and send all php requests to sapphire/main.php with the url as a parameter.

  • jayslippy
    Avatar
    Community Member
    10 Posts

    Re: Class not found when run through Cron job Link to this post

    Thanks for the reply, I still don't quite know where to go with this though! Is there a way for me to debug the manifest builder and output the data somehow to see why some of my classes aren't being built? Does anybody know any reason why my extension class might be ignored sometimes and not others? I've run the /deb/build so it's not that.

    I have now output the manifest file contents through the e-mail and the website, the relevant sections are copied below. What are the possible explanations for the manifest builder ignoring the FormFieldExtension.php file and the class it contains when run through a cron job, when it works fine through the browser?

    via e-mail/cron job/command line:

    'regularemailtask' => '/nfs/c09/h02/mnt/129055/domains/mro-247.com/html/mysite/tasks/RegularEmailTask.php',
    'editprofilepage' => '/nfs/c09/h02/mnt/129055/domains/mro-247.com/html/registration/code/EditProfilePage.php',
    'editprofilepage_controller' => '/nfs/c09/h02/mnt/129055/domains/mro-247.com/html/registration/code/EditProfilePage.php',
    'memberdecorator' => '/nfs/c09/h02/mnt/129055/domains/mro-247.com/html/registration/code/MemberDecorator.php',
    'registrationpage' => '/nfs/c09/h02/mnt/129055/domains/mro-247.com/html/registration/code/RegistrationPage.php',
    'registrationpage_controller' => '/nfs/c09/h02/mnt/129055/domains/mro-247.com/html/registration/code/RegistrationPage.php',
    'verificationextension' => '/nfs/c09/h02/mnt/129055/domains/mro-247.com/html/registration/code/VerificationExtension.php',
    'dataformatter' => '/nfs/c09/h02/mnt/129055/domains/mro-247.com/html/sapphire/api/DataFormatter.php',

    via website:

    'regularemailtask' => '/nfs/c09/h02/mnt/129055/domains/mro-247.com/html/mysite/tasks/RegularEmailTask.php',
    'editprofilepage' => '/nfs/c09/h02/mnt/129055/domains/mro-247.com/html/registration/code/EditProfilePage.php',
    'editprofilepage_controller' => '/nfs/c09/h02/mnt/129055/domains/mro-247.com/html/registration/code/EditProfilePage.php',
    'formfieldextension' => '/nfs/c09/h02/mnt/129055/domains/mro-247.com/html/registration/code/FormFieldExtension.php',
    'memberdecorator' => '/nfs/c09/h02/mnt/129055/domains/mro-247.com/html/registration/code/MemberDecorator.php',
    'registrationpage' => '/nfs/c09/h02/mnt/129055/domains/mro-247.com/html/registration/code/RegistrationPage.php',
    'registrationpage_controller' => '/nfs/c09/h02/mnt/129055/domains/mro-247.com/html/registration/code/RegistrationPage.php',
    'verificationextension' => '/nfs/c09/h02/mnt/129055/domains/mro-247.com/html/registration/code/VerificationExtension.php',
    'dataformatter' => '/nfs/c09/h02/mnt/129055/domains/mro-247.com/html/sapphire/api/DataFormatter.php',

  • jayslippy
    Avatar
    Community Member
    10 Posts

    Re: Class not found when run through Cron job Link to this post

    Resolved. In case anybody else is interested, I realised that there is a separate manifest for main (ie web access) and cli-script, which is what the cron job was using. You can check the different builds of the manifest in the silverstripe-cache folder, which confirmed to me that my cli-script version was a few days old. To force a refresh just add "flush=all" to the end of your command.

    "sapphire/cli-script.php /RegularEmailTask flush=all"

    796 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.