php - Cakephp cron job to call a controller's action -


i started use cakephp (1.2) few months ago add small features company's application , i'm not familiar it.

we test locally on development server before merging production server.

i want controller action called every hour assumed best way through researches, cron job.


attempt 1

after reading these,

http://bakery.cakephp.org/articles/mathew_attlee/2006/12/05/calling-controller-actions-from-cron-and-the-command-line

http://book.cakephp.org/1.2/en/view/110/creating-shells-tasks

i implement without errors, action not executed.

based on these examples, added file named cron_dispatcher.php in app directory (not app/webroot) , did command app dir

php cron_dispatcher.php /controller/action/param

still nothing happened works perfect when call through url.


attempt 2

i tried creating shell (email.php) call action in /app/vendors/shells/.

<?php  class emailshell extends shell {      public function main() {         $this->out('test');     }  } ?>  

this outputs test in console using

cake email main

but cannot find how call controller's action. have tried

$this->requestaction('/controller/action');

i have tried make call different function main in shell.

i have tried include controller in $uses variable model didn't work (and doesn't make sense think)

i don't think creating task solution either don't want duplicate sendemails function hence why i'm looking way call controller's action shell or whatever!

there theory i'm missing, thanks


solution

i moved methods controller model , able call them shell.

app::import('component', 'email');  class sendmemosshell extends shell {      var $uses = array(         'memo',     );      public function main() {      }      public function sendemails () {         $this->email =& new emailcomponent(null);         $memolist = $this->memo->getmemos();         //...     } } 

this link helped http://book.cakephp.org/2.0/en/console-and-shells/cron-jobs.html


edit : clarified of information , added solution

it quite common issue actually, ran also.

a controller deciding how handle request , starting task. in case there no need controller since have shell task, task clear.

knowing that, not make sense call controller method.

so rethink options, , yes quite difficult one. example might decide sending e-mail business logic step should in model. option separate totally (that's most).

in case have create queue put in e-mails send. design since know amount of logic in controller goes down , separated. way e-mail service.

for example ask service send "new user" mail. add user object , should handle itself. way can scale since service example outsourced, expand multiple servers on service etc.

edit:

good questions.

steps take:

  1. centralize "sending e-mail" process first. choose 1 location put it. can decide: add send e-mail queue or call service directly. example add shell task sending e-mails.

  2. call shell: have problem call shell. in general don't want to. why not? because shell (a task) run long time. that's why use queues in between. can ask queue or let queue message done. example think mail server down. have retry etc. should not in web request because user waiting response.

  3. third step call shell cron, that's easy since on command line use standard calls.

anyhow, there options direct call controller should not. post gives interesting insights: cakephp: run shell job controller

edit 31/08/'13: see events system of cakephp examples: http://book.cakephp.org/2.0/en/core-libraries/events.html


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -