正在显示
2 个修改的文件
包含
87 行增加
和
2 行删除
cmd/sync_my.php
0 → 100644
1 | +<?php | ||
2 | + | ||
3 | +//error_reporting(); | ||
4 | +include_once __DIR__."/../vendor/autoload.php"; | ||
5 | + | ||
6 | +@file_put_contents('sync_run_my.log',"进程启动 ".getmypid(),FILE_APPEND); | ||
7 | + | ||
8 | +swoole_set_process_name('php-email-sync-list-my'); | ||
9 | +//TODO:: 不知道为什么,隔断时间mysql会连不上 | ||
10 | +\Co\run(function (){ | ||
11 | + $start_time = time(); | ||
12 | + $goNum = 0; | ||
13 | +// 循环阻塞 | ||
14 | + while (true){ | ||
15 | + | ||
16 | + if (time() - $start_time > 43200){ | ||
17 | + @file_put_contents('sync_run_my.log',"进程停止 ".system("kill ".getmypid()),FILE_APPEND); | ||
18 | + break; | ||
19 | + } | ||
20 | + | ||
21 | + if($goNum > 50){ | ||
22 | + co::sleep(0.5); | ||
23 | + continue; | ||
24 | + } | ||
25 | + | ||
26 | + // 需要同步的id | ||
27 | + $id = redis()->lPop('sync_email_lists_my'); | ||
28 | + | ||
29 | + if($id && is_numeric($id)){ | ||
30 | + // 占用当前的id,占用2小时 | ||
31 | + 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 | + (new \Service\SyncMail($email))->sync(); | ||
40 | + | ||
41 | + } | ||
42 | + | ||
43 | + }catch (Throwable $e){ | ||
44 | + logs('sync : '.$e->getMessage()); | ||
45 | + } | ||
46 | + | ||
47 | + | ||
48 | + // 协程完成后执行的函数 | ||
49 | + co::defer(function () use ($id,&$goNum){ | ||
50 | + $goNum--; | ||
51 | + // 30秒后 消除占用 | ||
52 | + redis()->expire('just_sync_'.$id,120); | ||
53 | + // 写入日志 | ||
54 | + \Lib\Log::getInstance()->write(); | ||
55 | + }); | ||
56 | + | ||
57 | + }); | ||
58 | + } | ||
59 | + } | ||
60 | + | ||
61 | + //每次都暂停1秒,防止同一时间启动太多的任务 | ||
62 | + co::sleep(0.1); | ||
63 | + | ||
64 | + | ||
65 | + } | ||
66 | + | ||
67 | +// while ($goNum > 0){ | ||
68 | +// if (time() - $start_time > 24060){ break; } | ||
69 | +// co::sleep(1); | ||
70 | +// } | ||
71 | + | ||
72 | + | ||
73 | +}); | ||
74 | + | ||
75 | + | ||
76 | + | ||
77 | + | ||
78 | + | ||
79 | + | ||
80 | + | ||
81 | + | ||
82 | + | ||
83 | + | ||
84 | + | ||
85 | + |
@@ -571,8 +571,8 @@ class Home extends Base { | @@ -571,8 +571,8 @@ class Home extends Base { | ||
571 | } | 571 | } |
572 | 572 | ||
573 | } | 573 | } |
574 | - | ||
575 | - redis()->rPush('sync_email_lists', $v['id']); | 574 | + $source = app()->request('source'); |
575 | + redis()->rPush('sync_email_lists'.($source==1?'_my':''), $v['id']); | ||
576 | } | 576 | } |
577 | $datas[$k]['have_new'] = redis()->getDel('have_new_mail_'.$v['id']); | 577 | $datas[$k]['have_new'] = redis()->getDel('have_new_mail_'.$v['id']); |
578 | 578 |
-
请 注册 或 登录 后发表评论