<?php

namespace Lib;


/**
 * 这个是协程封装
 * @author:dc
 * @time 2025/3/17 11:22
 * Class SwGo
 * @package Lib
 */
class SwGo {

    /**
     * 运行状态
     * @var array
     */
    public static $runStatus = [];

    /**
     * 已经运行的go数量
     * @var int
     */
    public static $runNumber = 0;


    /**
     *
     * @param \Closure $run
     * @param \Closure $end
     * @author:dc
     * @time 2025/3/17 11:35
     */
    public static function start(\Closure $run,...$param): bool
    {

        go(function (\Closure $run,...$param){
            self::$runNumber++;
            $end = null;
            if(!empty($param[0]) && $param[0] instanceof \Closure){
                $end = $param[0];
                unset($param[0]);
                $param = array_values($param);
            }

            $run(...$param);

            if($end) $end(...$param);

            // 写入日志
            \Lib\Log::getInstance()->write();
            // 释放 mysql
            db()->close();
            // 释放redis
            redis()->close();

            self::$runNumber--;

        },$run,...$param);

        return true;
    }



}