Execute function in console from code

I’m creating an website that will send push notifications to an iphone app, the function that will send this push notifications sometimes can take a while.

To prevent timeouts, and unresponsive pages I want to send the push in background.

So in my controller I want to a call a function that will run on CLI.

For example, from a regular controller how can I call a function like the "actionIndex" that is within the HelloController (commands/HelloController.php).

I know how to call the Hello controler from the console I don’t know how to call it from within my php code.

Perhaps it will help if you read up on creating console applications: https://github.com/yiisoft/yii2/blob/master/docs/guide/tutorial-console.md

I already read that, but I don’t find any example. I know how to execute the code from console, I don’t know how to do the same but from php code.

If someone can show me an example it will be great. I am really stuck in this.

I would have a console application acting as a service in the background, which listens for commands that can be given by your frontend application. For instance, the service might run as a cron every 1 minute, which monitors a table in the database and, if it finds an entry earmarked as being incomplete (i.e. status = 0), it executes it and sends the push notification (and afterwards may mark status as 1, or delete the entry).

I don’t send mail out locally on most of my applications - I use a 3rd party via SMTP. It hangs for about 1 second to send a single email, so if my frontend application is trying to send out a bulk message, the application hangs for a long time. To combat this, I have an email service running in the background (not on a cron, it just runs 24/7 in the background intermittently checking for emails to send, and a cron restarts it if it fails), which monitors an email table in the database. When a new entry is added to the table, the service processes it. No hang, no worries.

An approach like this also has the added benefit of being able to share the load throughout a range of resources. Services are typically useful for processes that can be quite intensive. For instance, if I had an email service that was processing 2,000 emails/minute, I might want to move that off of my web server and on to its own box. It would be accomplished by simply migrating the email service, and would have no carry over effects on to the web application itself.

I like your approach. Do you create the console application with Yii? I’m new with linux and I have some difficult to understand what is a console application.

In my understanding I can check every 10s (or is too much? ) for entries with status=0.

For a service run 20/7 you have something like this:






How can I do a cron job to restarts it if it fails?

What I use largely depends on what I’m trying to accomplish. For instance, If I’m just reading and writing to one table and sending emails, there’s absolutely no onus on me to use Yii (and hence PHP). If the service is aiming to do something which would benefit more from Yii’s rich feature set (i.e. use of shared resources - AR models, components, etc.), I would probably build it in Yii.

Yes, I loop and then sleep prior to the next iteration. I have a service which monitors all of my services. I can stop/start specific services (so instead of while(1) I would have something like while(SERVICE_IS_ACTIVE)), and instead of sleep(10) I would have sleep(SERVICE_DELAY)). The delay depends again on what you’re trying to achieve. If you need to send push notifications out almost instantly, a shorter delay would be more appropriate.

Each service updates when it last runs (i.e. a simple UPDATE services SET last_run = CURRENT_TIMESTAMP WHERE service_id = ‘email’). If the last_run exceeds a certain threshold, it’s failed. I also catch exceptions in my services and try to fail/restart gracefully.