Run Controller function as Cron job

Questions, Best Practices, HowTo's, ...
Post Reply
superMan
Posts: 76
Joined: Sun Sep 14, 2014 9:11 am

Run Controller function as Cron job

Post by superMan » Tue Jan 27, 2015 1:57 pm

Hi,
i have controller, with a function that i want to run every 10 min for this example.
when i run this function from terminal with :

Code: Select all

php /var/www/html/pimcore/modules/admin/controllers/MangeController.php cron


i get this :

Code: Select all

PHP Fatal error:  Class 'Pimcore\Controller\Action\Admin' not found in /var/www/html/pimcore/modules/admin/controllers/MangeController.php on line 11


i know why its happen, how can i get this to work?
because i use inside the function with Pimcore API functions. i must load this classes.

it can be done? :-\

User avatar
IgorBenko
Posts: 689
Joined: Thu Jul 21, 2011 4:33 pm
Location: Berlin
Contact:

Re: Run Controller function as Cron job

Post by IgorBenko » Tue Jan 27, 2015 2:29 pm

I don't think you can directly call the controller since there is no request to dispatch. I am sure this could be done somehow, but it is basically too much trouble I think. If I were you, I would move the functionality out to a separate class and create a CLI script that would call the class function.
But the easiest method would be to just use curl or wget and call URL directly:

Code: Select all

/usr/bin/curl -silent http://www.yoururl.com/?controller=controller-name&action=action-name > /dev/null

or

Code: Select all

/usr/bin/wget --quiet http://www.yoururl.com/?controller=controller-name&action=action-name > /dev/null


Cheers, Igor
basilicom GmbH - Official Pimcore Selected Company - http://basilicom.de/de/pimcore

superMan
Posts: 76
Joined: Sun Sep 14, 2014 9:11 am

Re: Run Controller function as Cron job

Post by superMan » Tue Feb 03, 2015 10:16 am

i have controller lets say that his name his :

controller : Ctr_test
action : start

my call will be inside shell script : test.sh
/usr/bin/curl -silent http://www.dev-pimcore.com/?controller= Ctr_test&action=start > /dev/nulll

and crontab will run this every 5 min.

but how can i get this to work? i need to login with user in order to get to this address,
when I'm not logged in to the site, the response from the server is redirect to login page.

i don't know how to get this work :-\ any idea?

User avatar
IgorBenko
Posts: 689
Joined: Thu Jul 21, 2011 4:33 pm
Location: Berlin
Contact:

Re: Run Controller function as Cron job

Post by IgorBenko » Tue Feb 03, 2015 12:10 pm

I think you really should be using a CLI PHP script in that case. Otherwise you will be complicating things for yourself a bit too much. See CLI scripts in /pimcore/cli and try to help yourself with that.

Cheers, Igor
basilicom GmbH - Official Pimcore Selected Company - http://basilicom.de/de/pimcore

superMan
Posts: 76
Joined: Sun Sep 14, 2014 9:11 am

Re: Run Controller function as Cron job

Post by superMan » Mon Feb 09, 2015 1:35 pm

Sorry for my delay,
can you please guide me how to do this?

User avatar
IgorBenko
Posts: 689
Joined: Thu Jul 21, 2011 4:33 pm
Location: Berlin
Contact:

Re: Run Controller function as Cron job

Post by IgorBenko » Mon Feb 09, 2015 7:55 pm

Hi,

create a file MyCliScript.php in /website/var/cli (or any other folder you want - but make sure it is not publicly accessible).

Code: Select all

<?php
chdir(__DIR__);
// Startup file is inside /pimcore/cli folder
include_once("../../../pimcore/startup.php");

// Add here any code you want to run - for example
$newObject = Pimcore\Model\Object\Myclassname::create(array(
    'name'        => 'New Name',
    'description' => 'Some description'
));
$newObject->setKey(\Pimcore\File::getValidFilename('New Name'));
$newObject->setParentId(123);
$newObject->save();


Hope this helps!

Cheers, Igor
basilicom GmbH - Official Pimcore Selected Company - http://basilicom.de/de/pimcore

TPETb
Posts: 9
Joined: Wed Jun 26, 2013 3:57 pm

Re: Run Controller function as Cron job

Post by TPETb » Tue Jul 07, 2015 1:32 pm

I believe the right way is to actually put your desired code in a plugin (model?) and then in /path/to/your/plugin/lib/Plugin_Name/Plugin.php use the following:

Code: Select all

    
    public function init()
    {
        Pimcore::getEventManager()->attach("system.maintenance", array($this, "onMaintenance"));
    }


Be also aware that Pimcore (at least latest) puts max_execution_time 240 for HTTP env requests (CLI requests have no limit) — see https://github.com/pimcore/pimcore/blob ... e.php#L458


Yet again, for me CLI is not very friendly for debug purpose so I would really be glad if there was such possibility to use controller action from regular CLI maintenance. Does anyone have an idea if I can exec an action from anywhere in the code (I'm actually thinking of launching action execution from Plugin.php init method)

lnicoletti
Posts: 3
Joined: Wed Dec 03, 2014 1:09 pm
Contact:

Re: Run Controller function as Cron job

Post by lnicoletti » Thu Jul 23, 2015 3:23 pm

Yes! I use this way with Plugin

Code: Select all

    
    public function init()
    {
        Pimcore::getEventManager()->attach("system.maintenance", array($this, "onMaintenance"));
    }

Post Reply

Return to “Design & Templates”



Who is online

Users browsing this forum: No registered users and 1 guest