swoft efficient cronjobs

We’ll explore in this article implementing efficient Cronjobs with Swoft, the first new generation of PHP high-performance coroutine full stack PHP framework based on Swoole native coroutine, with built-in coroutine web server and common coroutine client, resident memory, independent of traditional PHP-FPM, full asynchronous non-blocking IO implementation It uses asynchronous client-side writing to implement asynchronous client.

The framework provides similar Go language operations, similar to the Spring Cloud framework flexible annotations, powerful global dependency injection container, comprehensive service governance, flexible and powerful AOP, standard PSR specification implementation and so on.

Swoft can be used to to build high-performance Web systems, APIs, middleware, basic services, and more. There is no complicated asynchronous callback, no complicated yield, it comes with perfect service governance, and flexibility. We wanted to give you a basic overview on some features in this framework.

Getting started with Swoft

If you are familiar with Swoole, installation should be easy and simple. Otherwise you need to get first Swoole extension installed. Swoft require at least version >4.3.0 of the Swoole extension installed and it’s recommended to use the latest stable version. So let’s install the latest

sudo pecl install swoole

I have enabled in my installation sockets, openssl, mysqlnd, json and curl

Swoole extension configuration
Build setting for Swoole extension

Swoole itself is based on other PHP extensions and libraries like MySQLnd, so if you choose it make sure it’s already installed on your system. Then enable Swoole in your php.ini

Swoole in the phpinfo()
Swoole enabled in PHP

Now let’s install Swoft :

composer create-project swoft/swoft swoft

Before starting Swoft, make sure you don’t have xdebug installed and if you do you can remove it or simply disable it. Now we have Swoft up and running we can show the cli command help, or simply start the http server then you can browse to localhost:18306 :

php bin/swoft -h
php bin/swoft http:start
Swoft framework up and running.

Efficient Cronjobs with Swoft

I’m not very comfortable with Frameworks where I have to modify the code itself of the framework to add any functionnalities; but anyway adding cronjobs in Swoft is very easy using Timed Tasks. So you don’t have to write new code, but you know where to modify. So you will have to look at the code under app/Task/Crontab/CronTask.php and add in the secondTask() method a simple printf command that will be executed every seconds, which is the six stars cron in the crontab format * * * * * * :

<?php declare(strict_types=1);
namespace App\Crontab;
use Swoft\Crontab\Annotaion\Mapping\Cron;
use Swoft\Crontab\Annotaion\Mapping\Scheduled;
 * Class CronTask
 * @since 2.0
 * @Scheduled()
class CronTask
     * @Cron("* * * * * *")
    public function secondTask()
        printf("second task run: %s ", date('Y-m-d H:i:s', time()));

If you are not familiar with Cronjobs, it’s very easy to configure the time frequency you want your task be executed, so you can add your custom jobs :

Swoft cronjobs

To be able to run the cronjob you need to enable it in your application, or run it manually. To enable it, you need to add it to the app/bean.php add to the process section the code below :

 return [
    'httpServer'     => [
            // ...
            'process' => [
                'crontab' => bean(Swoft\Crontab\Process\CrontabProcess::class)
            // ...

Now if we start the server the job will run every second with $ php bin/swoft http:start; you will see the cronjob running every seconds

Efficient Cronjobs with Swoft

So hope you got a small idea how to install and play with the Swoft framework, and adding efficient cronjobs. If you have any suggestion or question feel free to ask in the comments !


Please enter your comment!
Please enter your name here