正在显示
1 个修改的文件
包含
47 行增加
和
72 行删除
| 1 | <?php | 1 | <?php | 
| 2 | 2 | ||
| 3 | -//error_reporting(); | ||
| 4 | -include_once __DIR__."/../vendor/autoload.php"; | ||
| 5 | - | ||
| 6 | -@file_put_contents('sync_run.log',"进程启动 ".getmypid(),FILE_APPEND); | ||
| 7 | 3 | ||
| 8 | swoole_set_process_name('php-email-sync-list'); | 4 | swoole_set_process_name('php-email-sync-list'); | 
| 9 | -//TODO:: 不知道为什么,隔断时间mysql会连不上 | ||
| 10 | -\Co\run(function (){ | ||
| 11 | - $start_time = time(); | ||
| 12 | - $goNum = 0; | ||
| 13 | -// 循环阻塞 | ||
| 14 | - while (true){ | ||
| 15 | 5 | ||
| 16 | - if (time() - $start_time > 43200){ | ||
| 17 | - @file_put_contents('sync_run.log',"进程停止 ".system("kill ".getmypid()),FILE_APPEND); | ||
| 18 | - break; | ||
| 19 | - } | 6 | +$pm = new \Swoole\Process\Manager(); | 
| 20 | 7 | ||
| 21 | - if($goNum > 50){ | ||
| 22 | - co::sleep(0.5); | ||
| 23 | - continue; | ||
| 24 | - } | 8 | + | 
| 9 | +$pm->addBatch(60,function ($work_id) { | ||
| 10 | + | ||
| 11 | + include_once __DIR__."/../vendor/autoload.php"; | ||
| 12 | + | ||
| 13 | + $number = 0; | ||
| 14 | + while (true){ | ||
| 15 | + if($number > 500){ break; } | ||
| 25 | 16 | ||
| 26 | // 需要同步的id | 17 | // 需要同步的id | 
| 27 | $id = redis()->lPop('sync_email_lists'); | 18 | $id = redis()->lPop('sync_email_lists'); | 
| @@ -29,76 +20,60 @@ swoole_set_process_name('php-email-sync-list'); | @@ -29,76 +20,60 @@ swoole_set_process_name('php-email-sync-list'); | ||
| 29 | if($id && is_numeric($id)){ | 20 | if($id && is_numeric($id)){ | 
| 30 | // 占用当前的id,占用2小时 | 21 | // 占用当前的id,占用2小时 | 
| 31 | if(redis()->add('just_sync_'.$id,time(),600)){ | 22 | if(redis()->add('just_sync_'.$id,time(),600)){ | 
| 32 | - // 启动一个协程 | ||
| 33 | - go(function () use ($id,&$goNum){ | ||
| 34 | - $goNum++; | ||
| 35 | - try{ | ||
| 36 | - // 开始同步 | ||
| 37 | - $email = db()->cache(3600)->first(\Model\emailSql::first($id)); | ||
| 38 | - if($email){ | ||
| 39 | - $sync = new \Service\SyncMail($email); | ||
| 40 | - $search = new \Lib\Imap\ImapSearch(); | ||
| 41 | - // 第一次同步 只同步当天的 | ||
| 42 | - if(!db()->cache(600)->count(\Model\listsSql::first('`id` > 0'))){ | ||
| 43 | - $sync->search($search->dateGt($email['created_at'])); | 23 | + $number++; | 
| 24 | + | ||
| 25 | + try{ | ||
| 26 | + // 开始同步 | ||
| 27 | + $email = db()->cache(3600)->first(\Model\emailSql::first($id)); | ||
| 28 | + if($email){ | ||
| 29 | + $sync = new \Service\SyncMail($email); | ||
| 30 | + $search = new \Lib\Imap\ImapSearch(); | ||
| 31 | + // 第一次同步 只同步当天的 | ||
| 32 | + if(!db()->cache(600)->count(\Model\listsSql::first('`id` > 0'))){ | ||
| 33 | + $sync->search($search->dateGt($email['created_at'])); | ||
| 34 | + }else{ | ||
| 35 | + | ||
| 36 | + if(strtotime("-2 day") > strtotime($email['created_at'])){ | ||
| 37 | + $sync->search( | ||
| 38 | + $search->dateGt( | ||
| 39 | + date('Y-m-d', | ||
| 40 | + strtotime("-2 day") | ||
| 41 | + ) | ||
| 42 | + ) | ||
| 43 | + ); | ||
| 44 | }else{ | 44 | }else{ | 
| 45 | - // 是否是ai邮箱 | ||
| 46 | - if(db()->count("select count(*) from `hot_mail` where `email` = '{$email['email']}'")){ | ||
| 47 | - // ai邮件只同步2天内的 | ||
| 48 | - if(strtotime("-2 day") > strtotime($email['created_at'])){ | ||
| 49 | - $sync->search( | ||
| 50 | - $search->dateGt( | ||
| 51 | - date('Y-m-d', | ||
| 52 | - strtotime("-2 day") | ||
| 53 | - ) | ||
| 54 | - ) | ||
| 55 | - ); | ||
| 56 | - }else{ | ||
| 57 | - $sync->search( | ||
| 58 | - $search->dateGt($email['created_at']) | ||
| 59 | - ); | ||
| 60 | - } | ||
| 61 | - | ||
| 62 | - } | 45 | + $sync->search( | 
| 46 | + $search->dateGt($email['created_at']) | ||
| 47 | + ); | ||
| 63 | } | 48 | } | 
| 64 | 49 | ||
| 65 | - $sync->sync(); | ||
| 66 | - | ||
| 67 | - $sync = null; | ||
| 68 | - unset($sync); | ||
| 69 | } | 50 | } | 
| 70 | 51 | ||
| 71 | - }catch (Throwable $e){ | ||
| 72 | - logs('sync : '.$e->getMessage()); | 52 | + $sync->sync(); | 
| 53 | + | ||
| 54 | + $sync = null; | ||
| 55 | + unset($sync); | ||
| 73 | } | 56 | } | 
| 74 | 57 | ||
| 58 | + }catch (Throwable $e){ | ||
| 59 | + logs('sync : '.$e->getMessage()); | ||
| 60 | + } | ||
| 75 | 61 | ||
| 76 | - // 协程完成后执行的函数 | ||
| 77 | - co::defer(function () use ($id,&$goNum){ | ||
| 78 | - $goNum--; | ||
| 79 | - // 30秒后 消除占用 | ||
| 80 | - redis()->expire('just_sync_'.$id,120); | ||
| 81 | - // 写入日志 | ||
| 82 | - \Lib\Log::getInstance()->write(); | ||
| 83 | - }); | 62 | + // 30秒后 消除占用 | 
| 63 | + redis()->expire('just_sync_'.$id,120); | ||
| 84 | 64 | ||
| 85 | - }); | ||
| 86 | } | 65 | } | 
| 87 | } | 66 | } | 
| 88 | - | ||
| 89 | - //每次都暂停1秒,防止同一时间启动太多的任务 | ||
| 90 | - co::sleep(0.1); | ||
| 91 | - | 67 | + else{ | 
| 68 | + sleep(1); | ||
| 69 | + } | ||
| 92 | 70 | ||
| 93 | } | 71 | } | 
| 72 | +}); | ||
| 94 | 73 | ||
| 95 | -// while ($goNum > 0){ | ||
| 96 | -// if (time() - $start_time > 24060){ break; } | ||
| 97 | -// co::sleep(1); | ||
| 98 | -// } | ||
| 99 | 74 | ||
| 75 | +$pm->start(); | ||
| 100 | 76 | ||
| 101 | -}); | ||
| 102 | 77 | ||
| 103 | 78 | ||
| 104 | 79 | 
- 
请 注册 或 登录 后发表评论