Cakephp cron task to invoke controller action

I started using CakePHP (1.2) a few months ago to add small features to an enterprise application, and I'm not very familiar with it.

We then test locally on the development server before merging with the production server.

I want the controller action to be called every hour from the fact that I suggested that this is the best way to do this through my research, the cron task.


Attempt 1

After reading this data

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 could implement something without errors, but the action fails.

Based on these examples, I added the cron_dispatcher.php file to the application directory (not app / webroot), and then ran this command from the dir application

php cron_dispatcher.php / controller / action / param

Nothing still happens, but it works fine when I call it through a URL.


Attempt 2

I tried to create a shell (email.php) that would trigger an action in / app / vendors / shells /.

<?php class EmailShell extends Shell { public function main() { $this->out('Test'); } } ?> 

This successfully displays Test in the console using

cake email

but then I can not find how to trigger a controller action. I tried

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

I also tried to make a call from a function other than the main one in the shell.

I tried to include the controller in the $ uses variable, as with the model, but this did not work (and it does not make sense, I think)

I donโ€™t think creating a task is a solution, because I donโ€™t want to duplicate the sendEmails function, so Iโ€™m looking for a way to just call the controller action from the shell or something else!

There is probably some theory I'm missing, thanks


Decision

I moved some methods from the controller to the model, and I was able to call them from the 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 some of the information and added a solution

+6
source share
2 answers

This is a fairly common problem in fact, it also got into it.

The controller decides how to process the request and run this task. In this case, there is no need for a controller, since you have a shell task, the task is already clear.

Knowing that it makes no sense to call the controller method.

So rethink your options, and yes, it's pretty complicated. For example, you may decide that sending email is a step in business logic, so it should be in the model. Another option is to completely separate it (which we like best).

In this case, you will have to create a queue in which you put all the emails to send. This is a good design, since you know that the amount of logic in the controller goes down and it is separated. This way you get the email service.

For example, you can ask the service to send mail to a "new user". Then you add a User object to it, and it should process itself. Thus, you can even scale, since your service can be, for example, outsourced, you can expand several servers in the service, etc.

Edit:

Good questions.

Actions:

  • Concentrate the email sending process first. Therefore, choose one place where it can be placed. You can decide: Add to send an email to the queue or call the service directly. For example, you can add a shell task to send email.

  • Call Shell: Now you need to call the shell. In general, you do not want this. Why not? Because the shell (task) can work for a long time. Therefore, we use queues between them. Thus, you can request a queue or inform a queue of a message that something has been done. For example, think of a mail server that is down. You must try again, etc. This should not be in the web request because the user is waiting for a response.

  • The third step is to call the shell from your cron, now itโ€™s easy, since you are already on the command line to use standard calls.

In any case, there are options for direct calling from the controller, but you should not. This post provides very interesting information: CakePHP: Starting a shell job from a controller

Edit 31/08 / '13: See also CakePHP event system for some examples too: http://book.cakephp.org/2.0/en/core-libraries/events.html

+3
source

Depending on what needs to be done, I often keep these methods in my controller actions. At the top of the action, I check $ _SERVER ['REMOTE_ADDR'] == $ _SERVER ['SERVER_ADDR'] to ensure that only the site can trigger the action. Then in cron I would curl or wget this address.

It has its advantages - it is easier to run locally during development (just enter the URL in your browser), and there are also some differences between the php version and apache version of the cli version, as well as the request variables (for example, cake can 'get domain / the address of the site through cli, how you can work as an apache module, so absolute links to the site using the html helper do not work).

0
source

Source: https://habr.com/ru/post/919349/


All Articles