Scheduling Tasks to run in a Yii environment

Hello,

                          I have been lucky enough to have been  given an opportunity to work with Yii and PHP using a SQL Server  backend. I have developed a number of applications with simple workflow  and so far so good. I now require to have programs run that read through  the records created by the application to send out reminders, update  status information etc.


                          I was wondering if someone could please  give me some direction on how I might go about setting this up. The Yii,  PHP environment runs on a Windows 2008 server so I am guessing I can  use Windows Scheduler - I have never used this before. Is it just a  matter of writing an action in the Controller I wish to run and then  pointing a Windows Scheduled task to that URL eg [url="http://......./index.php/myapp/myAppsAction?"]http://......./index.php/myapp/myAppsAction?[/url]


                          My search on the Web directed me to running Cron jobs and even that seemed confusing.


                          


                          Kind regards


                          Chris

hey, not a big deal.

A bit tricky but easy solution provided from Yii is to have a console command, for understanding may you call command = controller and action in it.

for details may follow

http://www.yiiframework.com/doc/guide/1.1/en/topics.console

and

http://www.yiiframework.com/wiki/226/run-yiic-directly-from-your-app-without-a-shell/

let me now if you have trouble in implementation.

once a command is ready you may set a schedule job or just call it from script.

Hi,

You have create a separete commands to set sechedule.

1)Add setup to console.php for your database connection, which is inside Config folder

<?php

return array(

'basePath'=&gt;dirname(__FILE__).DIRECTORY_SEPARATOR.'..',


'name'=&gt;'My Console Application',


 


    'import'=&gt;array(


    'application.models.*',


    'application.components.*',


),  


    'components'=&gt;array( 


     'db'=&gt;array(


		'connectionString' =&gt; 'mysql:host=localhost;dbname=yourdatabasename',


		'emulatePrepare' =&gt; true,


		'username' =&gt; '',


		'password' =&gt; '',


		'charset' =&gt; 'utf8',


		'tablePrefix' =&gt; 'tbl_',


                'enableProfiling'=&gt;true,


                'enableParamLogging'=&gt;true,


	),   


    )    

);

2)Create folder ‘commands’ inside protected

3)inside folder create your command file as bellow MaintainCommand.php

<?php

Yii::import(‘application.models.*’);

class MaintainCommand extends CConsoleCommand

{

    public function actionrunnow(&#036;id){

// You can do your action here

                                   }

}

?>

  1. Create the schedule job for this command function it will work fine.

or you can use below code to execute

$commandPath = Yii::app()->getBasePath() . DIRECTORY_SEPARATOR . ‘commands’;

&#036;runner = new CConsoleCommandRunner();


&#036;runner-&gt;addCommands(&#036;commandPath);


&#036;commandPath = Yii::getFrameworkPath() . DIRECTORY_SEPARATOR . 'cli' . DIRECTORY_SEPARATOR . 'commands';


&#036;runner-&gt;addCommands(&#036;commandPath);


&#036;args = array('yiic', 'Maintain', 'runnow','--id=10');//--id is the argument we are passing we can send multiple argument 


ob_start();


&#036;runner-&gt;run(&#036;args);


echo htmlentities(ob_get_clean(), null, Yii::app()-&gt;charset);