Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How do I make a task for a cronjob
  • Two problems. I have some code as follows in the fuel\app\tasks\ folder named updates.php
    namespace Fuel\Tasks;
    
    class Updates {
    
        public function run() {
    
            echo "running";
            
            $now = time();
    
            $result = DB::select('*')
                    ->from('users')
                    ->where('time', '<=', $now)
                    ->execute();
    }
    }
    

    And I get a fatal error that it can't find the DB class PHP Fatal error: Class 'Fuel\Tasks\DB' not found in /var/www/project/fuel/app/tasks/updates.php on line 24
    Error - Class 'Fuel\Tasks\DB' not found in APPPATH/tasks/updates.php on line 24 Secondly I have no idea how to use this as a cron job. I usually set up my cron jobs through webmin and save a command like this /usr/bin/php -q /var/www/project/fuel/app/tasks/updates.php and run it under root. Is that correct?
  • I'm not using oil to run it. I guess the issue is I want to use fuel based php to write some scripts for cron to execute, but I dont know where I can put the files outside of the fuel file system and be able to leverage the fuel code if that makes sense. I mean I could make a "cron" controller but then it would be publicly accessable.
  • Hey there, I am having trouble running a task through cron because of the STDIN not being present, and it is throwing an error. It is simply not getting past the part where it checks for STDIN and STDERR etc... it worked just fine from the command line, but cron job didn't make it... any clues??? Thanks a bunch!
  • Through cron? Or through some kind of control panel that tries to fool you into believing it is cron? Because those tasks don't run in a commandline context at all, which is what oil is build for.
  • Well now that you say that... it must be a fool... I set it up through CPANEL, on a schedule... To be honest, that is always the way I did it... I am recently getting more into all the command line stuff, which is not easy. ;) What do you recommend on this one? Thanks so much.
  • fixed... had to do with the root user rather than the CPANEL user that was running the cron job. That and a different php path to use, which seemed a bit weird to me... like there are 2 of them, one for root, and one for user... all I can say is really pay attention to the users, permissions, and environment variables that would effect this. Fuel Rocks. ;0
  • I know this thread is old, but I think the solution still has not been presented. Yes, you need the \DB to make sure you can access that namespace. In order to run the task via crontab, you will indeed use oil, in particular the refine command. See as follows:
    php oil refine updates For more detail, read the docs. http://docs.fuelphp.com/general/tasks.html
  • And with reply to the original error: you can not call a FuelPHP php file directly. It needs the framework to be loaded and initialized, and that can only happen through the index.php file, or through the oil CLI utility. Having said that, there are lots of situations in which you need background processing, yet you don't have access to the commandline or there is no cron available. I implement these tasks using a message queue (as part of a workflow engine). The application inserts activities into the queue, and a polling process picks them up and processes them. In FuelPHP terms, an activity could be a task/method combination. This poller can be:
    - a task run via oil (through cron)
    - a specific URL in your application, which you can poll remotely using cron/wget on another server
    - a 1x1 image in the footer of your pages with an src that points to that specific URL This is a very scalable system, you can have 10 servers dedicated to these processes, all with DB access and access to the message queue, if your application whould require that.
  • You cant call cronjob file directly. You have to call it within oil:
    /var/www/project/fuel/oil refine updates See http://docs.fuelphp.com/general/tasks.html
  • For Tasks you have to use the Oil php file to execute Tasks... For task should be in php oil refine updates
  • For Tasks you have to use the Oil php file to execute Tasks... For task should be in php oil refine updates
  • Brian Perin wrote on Saturday 26th of November 2011:
    PHP Fatal error: Class 'Fuel\Tasks\DB' not found in /var/www/project/fuel/app/tasks/updates.php on line 24
    Error - Class 'Fuel\Tasks\DB' not found in APPPATH/tasks/updates.php on line 24

    You're in the Tasks namespace and there is no DB class (that class is in the Core namespace). To load the DB class from the Core namespace, just put a backslash before the class:
    \DB::method()
    
  • Peter Wiggers wrote on Saturday 26th of November 2011:
    Brian Perin wrote on Saturday 26th of November 2011:
    PHP Fatal error: Class 'Fuel\Tasks\DB' not found in /var/www/project/fuel/app/tasks/updates.php on line 24
    Error - Class 'Fuel\Tasks\DB' not found in APPPATH/tasks/updates.php on line 24

    You're in the Tasks namespace and there is no DB class (that class is in the Core namespace). To load the DB class from the Core namespace, just put a backslash before the class:
    \DB::method()
    

    Thanks that fixed that, but any idea how to automate tasks?
  • I can get cron to run a straight php file named test.php within the tasks folder but I get the same class not found error located in fuel/app/tasks/test.php
    <?php
    namespace Fuel\Tasks;
    
    echo "testing a task";
    \DB::select('*')
            ->from('users')
            ->execute();
    
    ?>
    

    PHP Fatal error: Class 'DB' not found in /var/www/project/fuel/app/tasks/test.php on line 4
  • Make sure you have declared correct paths in oil file.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion