Yii Framework Forum: Sharing application and global states to console app - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Sharing application and global states to console app Determine how to share the global and app state of web app w/ console Rate Topic: -----

#1 User is offline   drp666c 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 30-November 09

Posted 27 May 2010 - 11:01 AM

Greetings,
I am wondering what is the best way to use the Yii framework to write middleware (back-end code) that doesn't get called by web requests but rather by cron or shell php scripts. Additionally, if this is possible under Yii, can the global and app state data be shared across the middleware (back-end code) and the web instance. I am currently storing database connection and other information in the global state of the web app and would like to make it available to the backend code.

Thanks
0

#2 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 28 May 2010 - 01:57 AM

There's a section in the guide about console applications and yiic commands. They are perfect for cron and you can access mostly all application data (depends a little on configuration).

http://www.yiiframew.../topics.console
0

#3 User is offline   drp666c 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 30-November 09

Posted 28 May 2010 - 10:38 AM

View PostMike, on 28 May 2010 - 01:57 AM, said:

There's a section in the guide about console applications and yiic commands. They are perfect for cron and you can access mostly all application data (depends a little on configuration).

http://www.yiiframew.../topics.console


Mike,
Thanks for the reply. I had already read that section and if I understand it correctly it says you give it its own config file; which is fine but what I am trying to figure out is when I run something from the command line will that process be able to access the data stored in Yii::app and Yii::app()->getGlobalState by the web applications.

Additionally, I am trying to write middleware code which can be run either via cron or by interactive request from a web page. An example would be sending reminder emails using some business logic via cron or allow a user to use a webpage to send an alert but use the same business logic code.

Hope that made sense.

Regards.
0

#4 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 29 May 2010 - 05:10 AM

You can configure the same DB parameters for you console application or even think of mechanisms how to merge parameters shared by both, web and console applications (see here for inspiration).

You won't feel much difference in your console application. E.g. you can access your database with DAO and AR just like you would in your webapp. Also global state should be available (which i didn't test yet, though - give it a try?). You also can access all configured components, except for those who only make sense in a web request like session. So if you create a "mailer" component (or check out the extension repo for something), it will also be available in both applications.

Just like you described i use console apps for maintenance (cron-) jobs a lot.
0

#5 User is offline   drp666c 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 30-November 09

Posted 09 July 2010 - 12:35 AM

View PostMike, on 29 May 2010 - 05:10 AM, said:

You can configure the same DB parameters for you console application or even think of mechanisms how to merge parameters shared by both, web and console applications (see here for inspiration).

You won't feel much difference in your console application. E.g. you can access your database with DAO and AR just like you would in your webapp. Also global state should be available (which i didn't test yet, though - give it a try?). You also can access all configured components, except for those who only make sense in a web request like session. So if you create a "mailer" component (or check out the extension repo for something), it will also be available in both applications.

Just like you described i use console apps for maintenance (cron-) jobs a lot.


I'm having a bit of trouble getting a simple console command to run. I copied my fully functional config file from the web application and replaced the $config to point to the new config in my entry script

Yii::createConsoleApplication($config)->run();

But when I run the entry script I get the "No available commands" output.


php myconsole.php Email yiirocks@gmail.com 
No available commands. 
Please define them under the following directory:         
              /var/www/yii/mywebapp/protected/commands

0

#6 User is offline   drp666c 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 30-November 09

Posted 09 July 2010 - 12:40 AM

View Postdrp666c, on 09 July 2010 - 12:35 AM, said:

I'm having a bit of trouble getting a simple console command to run. I copied my fully functional config file from the web application and replaced the $config to point to the new config in my entry script

Yii::createConsoleApplication($config)->run();

But when I run the entry script I get the "No available commands" output.


php myconsole.php Email yiirocks@gmail.com 
No available commands. 
Please define them under the following directory:         
              /var/www/yii/mywebapp/protected/commands



Here's my php code for the email command class:


<?php
class EmailCommand extends CConsoleCommand
{
    public function run($args)
    {
        $receiver=$args[0];
        // send email to $receiver
        echo "We want to send email to $receiver";
    }
}

?>
 

0

#7 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 09 July 2010 - 02:23 AM

Why did you change the entry script? You should use protected/yiic to call your commands. It will use protected/config/console.php as default config file.
0

#8 User is offline   drp666c 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 30-November 09

Posted 09 July 2010 - 03:16 PM

View PostMike, on 09 July 2010 - 02:23 AM, said:

Why did you change the entry script? You should use protected/yiic to call your commands. It will use protected/config/console.php as default config file.


Are you saying this is what my shell command would look like?

php /var/www/yii/myapp/protected/yiic shell /var/www/yii/myapp/myconsole.php /var/www/yii/myapp/protected/config/console.php email yiirocks@gmail.com

php yiic shell <entry-script> <config-file> <command> <param0 param1 ... paramN>

Thanks for the reply.
0

#9 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 10 July 2010 - 02:13 AM

No no, much easier. Don't confuse the "yiic shell" (which is like a maintenance shell for your application) with a "yiic command". The latter is a command you have to put into protected/commands (e.g. the EmailCommand.php from guide example) and call like:

/path/to/protected/yiic email <arg0> <arg1> ...


By default it will use protected/config/console.php as configuration. The yiic file should be executable, so no need to add "php" in front.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users