正在显示
1 个修改的文件
包含
36 行增加
和
25 行删除
| 1 | <?php | 1 | <?php |
| 2 | 2 | ||
| 3 | +//error_reporting(); | ||
| 4 | +include_once __DIR__."/../vendor/autoload.php"; | ||
| 3 | 5 | ||
| 4 | swoole_set_process_name('php-email-sync-list-my'); | 6 | swoole_set_process_name('php-email-sync-list-my'); |
| 5 | 7 | ||
| 6 | -$pm = new \Swoole\Process\Manager(); | 8 | +function stop(){ |
| 9 | + if(redis()->add('sync_my_pid_lock',getmypid(),10)){ | ||
| 10 | + $keys = redis()->keys('sync_my_pid:*'); | ||
| 7 | 11 | ||
| 12 | + foreach ($keys as $key){ | ||
| 13 | + list($k,$pid) = explode(':',$key); | ||
| 14 | + if(posix_kill($pid,0)){ | ||
| 15 | + $t = redis()->get($key); | ||
| 8 | 16 | ||
| 9 | -$pm->addBatch(30,function ($work_id){ | ||
| 10 | - //error_reporting(); | ||
| 11 | - include_once __DIR__."/../vendor/autoload.php"; | ||
| 12 | - | ||
| 13 | - $number = 0; | 17 | + if(time()-$t > 60){ |
| 18 | + _echo('向'.$pid.'发送终止信号'); | ||
| 19 | + if(posix_kill($pid,SIGTERM)){ | ||
| 20 | + redis()->delete($key); | ||
| 21 | + } | ||
| 22 | + } | ||
| 23 | + }else{ | ||
| 24 | + redis()->delete($key); | ||
| 25 | + } | ||
| 26 | + } | ||
| 14 | 27 | ||
| 15 | - while (1){ | ||
| 16 | - if($number>500){ | ||
| 17 | - break; | 28 | + redis()->delete('sync_my_pid_lock'); |
| 18 | } | 29 | } |
| 30 | +} | ||
| 31 | + | ||
| 32 | +while (1){ | ||
| 33 | + | ||
| 19 | $id = redis()->lPop('sync_email_lists_my'); | 34 | $id = redis()->lPop('sync_email_lists_my'); |
| 35 | + redis()->set('sync_my_pid:'.getmypid(),time(),86400); | ||
| 20 | 36 | ||
| 37 | + stop(); | ||
| 38 | + | ||
| 39 | + // _echo('读取到'.$id); | ||
| 21 | if($id && is_numeric($id)){ | 40 | if($id && is_numeric($id)){ |
| 22 | // 占用当前的id,占用2小时 | 41 | // 占用当前的id,占用2小时 |
| 23 | if(redis()->add('just_sync_'.$id,time(),600)){ | 42 | if(redis()->add('just_sync_'.$id,time(),600)){ |
| 24 | - $number++; | 43 | + |
| 25 | try{ | 44 | try{ |
| 26 | // 开始同步 | 45 | // 开始同步 |
| 27 | - $email = db()->cache(3600)->first(\Model\emailSql::first($id)); | ||
| 28 | - if($email){ | ||
| 29 | - (new \Service\SyncMail($email))->sync(); | ||
| 30 | - } | ||
| 31 | - | 46 | + (new \Service\SyncMail($id))->sync(); |
| 32 | }catch (Throwable $e){ | 47 | }catch (Throwable $e){ |
| 33 | - logs('sync : '.$e->getMessage()); | 48 | + _echo($e->getMessage()); |
| 34 | } | 49 | } |
| 35 | - | ||
| 36 | // 30秒后 消除占用 | 50 | // 30秒后 消除占用 |
| 37 | - redis()->expire('just_sync_'.$id,120); | 51 | + redis()->expire('just_sync_'.$id,30); |
| 52 | + | ||
| 53 | + \Lib\Log::getInstance()->write(); | ||
| 54 | + | ||
| 38 | } | 55 | } |
| 39 | }else{ | 56 | }else{ |
| 40 | sleep(1); | 57 | sleep(1); |
| 41 | } | 58 | } |
| 42 | - } | ||
| 43 | - | ||
| 44 | - _echo('子进程即将推出'); | ||
| 45 | - | ||
| 46 | -}); | ||
| 47 | - | ||
| 48 | -$pm->start(); | ||
| 49 | 59 | ||
| 60 | +} | ||
| 50 | 61 | ||
| 51 | 62 | ||
| 52 | 63 |
-
请 注册 或 登录 后发表评论