Administrative processes & worker processes

After half a year, I have improved the last part of the process management in the last chapter. The last part of the process management is very wonderful!

This one picks up where we left off in Chapter 19, Mastering the Horizon, after we started the process,

View the current running processes in the system

Worker process source code

Focus on the Horizon: Work process, as this is the main working process, here is the source codeCopy the code
<? php namespace Laravel\Horizon\Console; use Illuminate\Queue\Console\WorkCommand as BaseWorkCommand; class WorkCommand extends BaseWorkCommand { /** * The consolecommand name.
      *
      * @var string
      */
     protected $signature = 'horizon:work {connection? : The name of the queue connection to work} {--delay=0 : Amount of time to delay failed jobs} {--daemon : Run the worker in daemon mode (Deprecated)} {--force : Force the worker to run even in maintenance mode} {--memory=128 : The memory limit in megabytes} {--once : Only process the next job on the queue} {--stop-when-empty : Stop when the queue is empty} {--queue= : The names of the queues to work} {--sleep=3 : Number of seconds to sleep when no job is available} {--supervisor= : The name of the supervisor the worker belongs to} {--timeout=60 : The number of seconds a child process can run} {--tries=0 : Number of times to attempt a job before logging it failed}';
 
     /**
      * Indicates whether the command should be shown in the Artisan command list.
      *
      * @var bool
      */
     protected $hidden = true;
 
     /**
      * Execute the console command.
      *
      * @return void
      */
     public function handle()
     {
         if (config('horizon.fast_termination')) {
             ignore_user_abort(true); } parent::handle(); }}Copy the code

Worker process parent source code

parent::handle();

Parent :: Handle () actually executes Laravel's WorkCommand constructor

Here we can see that Horizon actually uses Laravel’s Queue: Work code

Notice a few differences here, because instead of calling PHP artisan Queue :work,

That’s why the name we see in the process does not contain Queue :work but Horizon :work, but does it implement what Laravel provides

Queue: Work is the logic of the Horizon queue: Work

That’s the end of the story, but let’s take an extra look at how Laravel implements queues.

WorkCommand executes logic

    /**
     * Execute the console command.
     *
     * @return void
     */
     
    public function handle()
    {
        if ($this->downForMaintenance() && $this->option('once')) {
            return $this->worker->sleep($this->option('sleep'));
        }

        // Well listen to the processed and failed events so we can write information
        // to the console as jobs are processed, which will let the developer watch
        // which jobs are coming through a queue and be informed on its progress.
        $this->listenForEvents();

        $connection = $this->argument('connection')
                        ?: $this->laravel['config'] ['queue.default'];

        // We need to get the right queue for the connection which is set in the queue
        // configuration file for the application. We will pull it based on the set
        // connection being run for the queue operation currently being executed.
        $queue = $this->getQueue($connection);


        // 'Nothing, just go to the topic, pass in the connection and queue name, execute'
        $this->runWorker(
            $connection.$queue
        );
    }
    
Copy the code

accident

Just when I was ready to do a big, simple analysis of queue:work source code, suddenly inspiration struck me, AS if I had written an article, exclaim, hold the grass! Almost wrote heavy ~

Chapter 11, Job& queue consumer end implementation

conclusion

This chapter is interesting, the core is still horizon:work using queue:work source code.

But Horizon provides more powerful Worker process management, dynamic zoom process, friendly UI,

And simple maintenance commands and so on. The only drawback is that Redis clustering is not supported.