轮询数据库php后调度任务

I am building web app in PHP which is used to schedule tasks in the future. In the background I am supposed to Poll the database every 2 min to see if there is any upcoming task. How do I implement polling. Javascript solution is not helpful as the user can be on any of the pages on the webapp, but the system should keep pooling the DB.

Cron is one way but still I have to poll the database to create a cron job. What is the best way to implement it?

Thanks

Create a cron job that is executed once every X minutes and make that job check the DB. If there is a new upcoming task, just make the job launch it.

Create an 'execute:tasks' artisan command, so you can poll your database whenever you need to execute your tasks. You should be able to run it this way:

php artisan execute:tasks

Your command will call some controller action (or a class method) to poll the database and find if there are tasks available to be executed.

Then you just need to create a cron job that will execute that artisan command every 2 minutes:

*/2 * * * * php artisan execute:tasks

This is an example of Artisan command:

<?php

use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;

class ExecuteTasksCommand extends Command {

    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'execute:tasks';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Find and execute available tasks.';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return void
     */
    public function fire()
    {
        (new ExecuteTasksClass)->findAndExecute();
    }

}

You just have to name this file something like app/commands/ExecuteTasksCommand.php

And add this to app\start\artisan.php:

Artisan::add(new ExecuteTasksCommand);