How to Program With Yii2: Running Cron Services

Final product image
What You'll Be Creating

If you're asking, "What's Yii?" check out my earlier tutorial: Introduction to the Yii Framework, which reviews the benefits of Yii and includes an overview of what's new in Yii 2.0, released in October 2014.

In this Programming With Yii2 series, I'm guiding readers in use of the Yii2 Framework for PHP. In today's tutorial, I'll share with you how to take advantage of Yii's console capacity to run cron jobs.

In the past, I've used wget in my cron jobs—a web accessible URL would run my background tasks. This raised security issues and has some performance problems. While I addressed some ways to mitigate risks in our startup series' episodes on security, I had hoped to transition to console-driven commands. And with Yii2 it's fairly straightforward.

For today's example, I'll demonstrate console-based cron commands on my Twixxr site which I described in this Twitter API episode. Due to rate limits and performance management issues, the Twitter API is very dependent on efficient, reliable cron tasks. So it's a great example to share with you.

Before I get started, I'll reiterate: I am always appreciative of your ideas and feedback. If you have a question or topic suggestion, please post your thoughts in the comments below. You can also reach me on Twitter @reifman directly.

What Is Cron?

Wikipedia describes cron as "a time-based job scheduler in Unix-like computer operating systems." And that's pretty accurate. Basically, cron runs all of the background tasks we need to run web services, from log management and backups to API requests to database cleanup.

To see your existing cron jobs on a server, you usually type sudo crontab -l and see something like this:

The left-hand side specifies to activate these tasks every 3 or 15 minutes or daily at midnight, etc., and the right-hand side is the script to run. See also Scheduling Tasks with Cron Jobs (Envato Tuts+).

Notice how the Let's Encrypt script is a unique console command. It runs from the command line on our server. However, all my Meeting Planner tasks above run via wget. That acts as if a robot was at a web browser at a specific time running requests against our web application that perform background tasks.

In addition to the overhead that an external web request requires and timeout limitations on scripts on servers, you have to secure these access points. Here's an example of how Meeting Planner does it:

It verifies that the user is either logged in as an administrator or running locally on the server at an identical Internet IP address.

Implementing Console-Based Cron Commands

Alex Makarov, one of the lead volunteers behind the Yii Framework's development, has been helpful answering questions for me as I regularly write about the framework for Envato Tuts+. After reading my security episode, he asked why I wasn't using Yii2's inherent console capability for cron jobs. Basically, I didn't know about it.

Just as I had a /frontend/controllers/DaemonController.php, I created a /console/controllers/DaemonController.php. For this tutorial, I'll do this for the smaller, simpler Twixxr web service.

I'm used to using the console for running database migrations (e.g. ./yii migrate/up 7), but that's all. I was eager to try using it for background tasks.

As I wrote about in an earlier tutorial, my newborn site Twixxr requires extensive background processes to regularly rotate API calls for all of the user requests to befriend influential Twitter accounts held by women. 

Here's what the home page looks like:

How to Program with Yii2 - Console-Based Cron - Twixxr Home Page Example Website

So I thought Twixxr would make a great testbed for running a console-based cron controller.

The New DaemonController.php

Here's the core of my new console-based DaemonController.php:

Notice that it's pretty identical to the structure of my front-end based controller, but it's securely inaccessible to the web because it's in the /console tree. No Apache web server site is configured to browse this area.

So, in the example above, actionFrequent() will be called every two to three minutes. It processes another set of Twixxr friendship requests. On the other hand, actionQuarter() is called every 15 minutes and updates the profile information for browsing accounts. Let's look at how the scheduling works in the cron file.

The New Crontab File

Essentially, in my crontab file, I replace wget with a direct Linux script as shown above for Let's Encrypt renewals.

You type sudo crontab -e to edit or -l to list its contents. Here's my Twixxr cron file:

It's pretty simple. The left-hand side of /var/www/twixxr/yii daemon/frequent is the path where the yii interpreter lives, and the right-hand side is the console controller and method to call.

Everything worked pretty well switching over. I haven't switched Meeting Planner over just yet as I want to do more testing. When background tasks break, it's difficult to know and difficult to debug them (although Sentry error logging is helping a lot).

Issues to Consider

One element I ran into is that the console namespace is distinct from the front-end namespace—so, for example, the SiteHelper.php component I set up in my tutorial, which described running multiple sites from a single codebase, failed when I invoked it. Removing it worked, but I needed to run tests to make sure the underlying background code still functioned. However, mostly the switchover went smoothly.

As with any other code change, test and monitor thoroughly.

What's Next

Looking ahead, I will explore building REST APIs within the Yii2 Framework, which relies coincidentally on creating a distinct sub-tree like the console tree but for external APIs. Of course, this brings up complex authentication and security issues... so it will be fun to explore these with you. I'll be looking at APIs from several angles. I'm actually pretty excited about this. 

Watch for upcoming tutorials in my Programming With Yii2 series as I continue diving into different aspects of the framework. Please also explore the Building Your Startup With PHP series, which documents the process of building Simple Planner and Meeting Planner.

If you'd like to know when the next Yii2 tutorial arrives, follow me @reifman on Twitter or check my instructor page for updates. 

Related Links



Related Articles