<?php

namespace App\Modules\Network\Commands;

class PrefixCommunities extends \App\Console\Commands\BaseCommand
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'prefix:communities {--thread-pool=}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Associate known prefixes against known communities and paths.';

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

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $max = $this->option('thread-pool');

        $pipe = [];

        for ($i = 0; $i < $max; ++$i) {
            $pipe[$i] = popen("php /var/www/html/creatia/artisan prefix:communities-thread --as-max=$max --as-mod=$i", 'w');
        }
        
        for ($i = 0; $i < $max; ++$i) {
            pclose($pipe[$i]);
        }
    
        return 0;
    }
}
