hot_mail_2.php 2.6 KB
<?php

use Model\listsSql;


/**
 * 处理预热邮箱
 * @author:dc
 * @time 2024/9/4 11:02
 * Class HotMail
 */
class HotMail {

    public function __construct(){
        $this->db = db();
        $this->start();
    }

    /**
     * shopk那边的预热邮箱
     * @var array
     */
    private $hotEmail = [];

    /**
     * @var \Lib\Db|\Lib\DbPool
     */
    private $db;


    /**
     * @author:dc
     * @time 2024/7/18 14:04
     */
    private function start(){
        _echo('启动预热邮件处理 '.getmypid());

        if(redis()->add('hot_mail_sync',1,60)){

            $maxId = $this->db->value("select `id` from `lists` order by `id` desc limit 1");
            $id = 0;
            while (1){
                $ids = [];
                for ($i=0;$i<1000;$i++){
                    $ids[] = $i+$id;
                }
                $id = end($ids);

                redis()->rPush('hot_check_ids',implode(',',$ids));

                if($id>$maxId){
                    break;
                }
            }

        }


        while (1){
            $ids = redis()->lPop('hot_check_ids');
            if($ids){
                $ids = explode(',',$ids);
                $this->run($ids);
            }else{
                echo '等待'.PHP_EOL;
                co::sleep(2);
            }
        }

    }


    private $folder = [];

    private function run($id){
        $list = $this->db->all(\Model\listsSql::all(dbWhere(['id'=>$id,'is_hots'=>0]),'`id`,`from`,`to`,`folder_id`'));
        foreach ($list as $item){
            if(empty($this->folder[$item['folder_id']])){
                $this->folder[$item['folder_id']] = folderAlias($this->db->value(\Model\folderSql::first($item['folder_id'],'folder')));
            }

            // 是否是发件箱
            if($this->folder[$item['folder_id']] == '发件箱'){
                $w = ['email' => explode(',',$item['to'])];
            }else{
                $w = ['email' =>$item['from']];
            }
            // 是否在 预热邮箱中
            if($this->db->cache(300)->count('select count(*) from `hot_mail` where '.dbWhere($w))){

                $ret = $this->db->update(listsSql::$table,['is_hots'=>1],dbWhere(['id'=>$item['id']]));
                echo date('d H:i:s').' ==》 '.$item['id'].':'.$ret."\n";
            }
        }
    }


}


swoole_set_process_name('hot-email-run-man');

$pm = new Swoole\Process\Manager();

$pm->addBatch(10,function (){

    swoole_set_process_name('hot-email-run');

    include_once "../vendor/autoload.php";

    new HotMail();

    exit();
},true);

$pm->start();