Table of Contents
What is Cron?
Implement console-based Cron command
New DaemonController.php
New crontab file
需要考虑的问题
下一步是什么
相关链接
Home Backend Development PHP Tutorial Yii2 Programming Guide: How to run Cron service

Yii2 Programming Guide: How to run Cron service

Sep 01, 2023 pm 11:21 PM
yii Programming Guide cron service

Yii2 Programming Guide: How to run Cron service

If you're asking "What is Yii?" check out my previous tutorial: Introduction to the Yii Framework, which reviews the benefits of Yii and provides an overview of the 2014-10 New features of Yii 2.0 released in March. Well>

In this Programming with Yii2 series, I will guide readers in using the Yii2 PHP framework. In today’s tutorial, I’ll share with you how to leverage Yii’s console functionality to run cron jobs.

In the past, I used wget — a web-accessible URL — in a cron job to run my background tasks. This raises security concerns and has some performance issues. While I discussed some ways to mitigate risks in our Boot Series security feature, I had hoped to transition to console-driven commands. With Yii2 this is fairly simple.

For today's example, I'll be demonstrating the console-based cron command on my Twixxr site, which I describe in this Twitter API episode. Due to rate limiting and performance management issues, the Twitter API relies heavily on efficient, reliable cron jobs. So this is a great example to share with you.

Before I begin, I want to reiterate: I always appreciate your ideas and feedback. If you have questions or topic suggestions, please post your thoughts in the comments below. You can also contact me directly on Twitter @reifman.

What is Cron?

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

To view existing cron jobs on the server, you would typically enter sudo crontab -l and see something like this:

# Edit this file to introduce tasks to be run by cron.
# 
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
# 
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').# 
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
# 
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# 
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
# 
# For more information see the manual pages of crontab(5) and cron(8)
# 
# m h  dom mon dow   command
*/3 * * * * wget -O /dev/null https://meetingplanner.io/daemon/frequent
*/15 * * * * wget -O /dev/null http://meetingplanner.io/daemon/quarter
0 * * * * wget -O /dev/null http://meetingplanner.io/daemon/hourly
15 1 * * * wget -O /dev/null http://meetingplanner.io/daemon/overnight
40 2 * * * /usr/sbin/automysqlbackup
15 3 * * 5 wget -O /dev/null http://meetingplanner.io/daemon/weekly
30 2 * * 1 /opt/letsencrypt/letsencrypt-auto renew >> /var/log/le-renew.log
Copy after login

The left side specifies to activate these tasks every 3 or 15 minutes or every day at midnight, etc., and the right side is the script to run. See alsoUsing Cron Jobs to Schedule Tasks (Envato Tuts).

Please note that the Let's Encrypt script is a unique console command. It is run from the command line on our server. However, all of my meeting schedule tasks above are run via wget. It's like a bot running requests on the web browser at a specific time against our web application that performs background tasks.

In addition to the overhead required for external web requests and timeout constraints for scripts on the server, you must also secure these access points. Here's an example of how meeting planners can do this:

// only cron jobs and admins can run this controller's actions
    public function beforeAction($action)
    {
      // your custom code here, if you want the code to run before action filters,
      // which are triggered on the [[EVENT_BEFORE_ACTION]] event, e.g. PageCache or AccessControl
      if (!parent::beforeAction($action)) {
          return false;
      }
      // other custom code here
      if (( $_SERVER['REMOTE_ADDR'] == $_SERVER['SERVER_ADDR'] ) ||
          (!\Yii::$app->user->isGuest && \common\models\User::findOne(Yii::$app->user->getId())->isAdmin()))
       {
         return true;
       }
      return false; // or false to not run the action
    }
Copy after login

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

Implement console-based Cron command

Alex Makarov is one of the main volunteers in the development of the Yii framework and he helps answer my questions when I write regularly about the Envato Tuts framework. After reading my security incident, he asked me why I didn't use Yii2's native console functionality to execute cron jobs. Basically I don't know.

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

I'm used to using the console to run database migrations (e.g. ./yii migrate/up 7), but that's about it. I'd love to try using it to perform background tasks.

As I wrote in a previous tutorial, my nascent website Twixxr required a large number of background processes to regularly rotate API calls to satisfy all user requests to socialize with female-owned influencer Twitter accounts.

The homepage is as follows:

Yii2 Programming Guide: How to run Cron service

So I thought Twixxr would be a good testing platform for running console based cron controllers.

New DaemonController.php

This is the core of my new console-based DaemonController.php:

<?php
namespace console\controllers;

use Yii;
use yii\helpers\Url;
use yii\console\Controller;
use frontend\models\Twixxr;

/**
 * Test controller
 */
class DaemonController extends Controller {

    public function actionIndex() {
        echo "Yes, cron service is running.";
    }

    public function actionFrequent() {
      // called every two minutes
      // */2 * * * * ~/sites/www/yii2/yii test
      $time_start = microtime(true);
      $x = new \frontend\models\Twixxr();
      $x->process($time_start);
      $time_end = microtime(true);
      echo 'Processing for '.($time_end-$time_start).' seconds';
    }

    public function actionQuarter() {
        // called every fifteen minutes
        $x = new \frontend\models\Twixxr();
        $x->loadProfiles();
      }

      public function actionHourly() {
        // every hour
        $current_hour = date('G');
        if ($current_hour%4) {
          // every four hours
        }
            if ($current_hour%6) {
            // every six hours
          }
      	}
Copy after login

Please note that it is very similar in structure to my front-end based controller, but it is not securely accessible over the network because it is located in the /console tree. The Apache web server site is not configured to browse this zone.

So, in the example above, actionFrequent() will be called every two to three minutes. It handles another set of Twixxr friendship requests. actionQuarter() on the other hand is called every 15 minutes and updates the browsing account's profile information. Let's see how scheduling works in a cron file.

New crontab file

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

You enter sudo crontab -e to edit, or -l to list its contents. Here is my Twixxr cron file:

$ sudo crontab -l
# m h  dom mon dow   command
*/3 * * * * /var/www/twixxr/yii daemon/frequent
*/15 * * * * /var/www/twixxr/yii daemon/quarter
0 * * * * /var/www/twixxr/yii daemon/hourly
15 1 * * * /var/www/twixxr/yii daemon/overnight
15 3 * * 5 /var/www/twixxr/yii daemon/weekly
#40 2 * * * /usr/sbin/automysqlbackup
30 2 * * 1 /usr/bin/letsencrypt renew >> /var/log/le-renew.log
Copy after login

这非常简单。 /var/www/twixxr/yii daemon/frequent 的左侧是 yii 解释器所在的路径,右侧是控制台控制器和调用的方法。 p>

切换时一切都很顺利。我还没有切换 Meeting Planner,因为我想做更多测试。当后台任务中断时,很难知道也很难调试它们(尽管 Sentry 错误日志记录有很大帮助)。

需要考虑的问题

我遇到的一个问题是控制台命名空间与前端命名空间不同,因此,例如,我在教程中设置的 SiteHelper.php 组件(该组件描述了从单个代码库运行多个网站)失败了当我调用它时。删除它是有效的,但我需要运行测试以确保底层后台代码仍然有效。不过,大部分情况下切换都很顺利。

与任何其他代码更改一样,彻底测试和监控。

下一步是什么

展望未来,我将探索在 Yii2 框架内构建 REST API,该框架恰好依赖于创建一个不同的子树,例如控制台树,但用于外部 API。当然,这会带来复杂的身份验证和安全问题……所以与您一起探索这些将会很有趣。我将从多个角度研究 API。我对此感到非常兴奋。

请观看我的“使用 Yii2 编程”系列中即将推出的教程,我将继续深入研究该框架的不同方面。另请浏览“使用 PHP 构建您的初创公司”系列,其中记录了构建 Simple Planner 和 Meeting Planner 的过程。

如果您想知道下一个 Yii2 教程何时发布,请在 Twitter 上关注我 @reifman 或查看我的讲师页面以获取更新。

相关链接

  • Yii2 Developer Exchange,我的 Yii2 资源站点
  • 使用 Cron 作业安排任务 (Envato Tuts+)

  • 如何在 Yii2 中实现 cron(Yii 文档)
  • Twixxr,其中提到的示例网络服务

The above is the detailed content of Yii2 Programming Guide: How to run Cron service. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use PHP framework Yii to develop a highly available cloud backup system How to use PHP framework Yii to develop a highly available cloud backup system Jun 27, 2023 am 09:04 AM

With the continuous development of cloud computing technology, data backup has become something that every enterprise must do. In this context, it is particularly important to develop a highly available cloud backup system. The PHP framework Yii is a powerful framework that can help developers quickly build high-performance web applications. The following will introduce how to use the Yii framework to develop a highly available cloud backup system. Designing the database model In the Yii framework, the database model is a very important part. Because the data backup system requires a lot of tables and relationships

Full-width and half-width switching shortcut key usage guide Full-width and half-width switching shortcut key usage guide Mar 26, 2024 am 09:30 AM

Full-width and half-width shortcut key usage guide Full-width and half-width are two states commonly used in Chinese input methods. In full-width state, one character occupies one character's position, while in half-width state, one character occupies half of a character's position. In daily word processing, it is often necessary to switch between full-width and half-width, and mastering shortcut keys can improve work efficiency. This article will introduce you to the shortcut key usage guide for switching between full-width and half-width. 1. Full-width and half-width switching under Windows system. To switch between full-width and half-width states under Windows system, usually use the following

Symfony vs Yii2: Which framework is better for developing large-scale web applications? Symfony vs Yii2: Which framework is better for developing large-scale web applications? Jun 19, 2023 am 10:57 AM

As the demand for web applications continues to grow, developers have more and more choices in choosing development frameworks. Symfony and Yii2 are two popular PHP frameworks. They both have powerful functions and performance, but when faced with the need to develop large-scale web applications, which framework is more suitable? Next we will conduct a comparative analysis of Symphony and Yii2 to help you make a better choice. Basic Overview Symphony is an open source web application framework written in PHP and is built on

Data query in Yii framework: access data efficiently Data query in Yii framework: access data efficiently Jun 21, 2023 am 11:22 AM

The Yii framework is an open source PHP Web application framework that provides numerous tools and components to simplify the process of Web application development, of which data query is one of the important components. In the Yii framework, we can use SQL-like syntax to access the database to query and manipulate data efficiently. The query builder of the Yii framework mainly includes the following types: ActiveRecord query, QueryBuilder query, command query and original SQL query

How to use Yii3 framework in php? How to use Yii3 framework in php? May 31, 2023 pm 10:42 PM

As the Internet continues to develop, the demand for web application development is also getting higher and higher. For developers, developing applications requires a stable, efficient, and powerful framework, which can improve development efficiency. Yii is a leading high-performance PHP framework that provides rich features and good performance. Yii3 is the next generation version of the Yii framework, which further optimizes performance and code quality based on Yii2. In this article, we will introduce how to use Yii3 framework to develop PHP applications.

Yii2 vs Phalcon: Which framework is better for developing graphics rendering applications? Yii2 vs Phalcon: Which framework is better for developing graphics rendering applications? Jun 19, 2023 am 08:09 AM

In the current information age, big data, artificial intelligence, cloud computing and other technologies have become the focus of major enterprises. Among these technologies, graphics card rendering technology, as a high-performance graphics processing technology, has received more and more attention. Graphics card rendering technology is widely used in game development, film and television special effects, engineering modeling and other fields. For developers, choosing a framework that suits their projects is a very important decision. Among current languages, PHP is a very dynamic language. Some excellent PHP frameworks such as Yii2, Ph

PHP Date Programming Guide: Discover how to determine the day of the week for a date using PHP PHP Date Programming Guide: Discover how to determine the day of the week for a date using PHP Mar 19, 2024 pm 06:09 PM

PHP Date Programming Guide: Discover how to use PHP to determine the day of the week for a certain date. In PHP programming, we often need to deal with date and time-related issues. One of the common needs is to determine the day of the week for a certain date. PHP provides a wealth of date and time processing functions that can easily implement this function. This article will introduce in detail how to determine the day of the week of a certain date in PHP and give specific code examples. 1. Use the date() function to get the day of the week. The date() function in PHP can be used for formatting.

A Beginner's Guide to Go Language Programming A Beginner's Guide to Go Language Programming Mar 25, 2024 am 09:30 AM

Go language (Golang) is a programming language developed by Google. Its design is simple, efficient, and has strong concurrency features, so it is favored by many developers. This article will provide you with a simple introductory guide to Go language programming, introduce basic concepts and syntax, and attach specific code examples to help you better understand and learn Go language programming. The first step in setting up and configuring the environment is to set up a development environment for the Go language. You can go to Go official website (https://golang.org/)

See all articles