1、参考网址:https://www.shuijingwanwq.com/2021/01/22/4698/ 。queue/run 命令替换 queue/listen 命令。在本地环境中,查看 queue/run 命令队列的状态。
PS E:/wwwroot/channel-pub-api> ./yii copy-asset-queue/info --color=0 Jobs - waiting: 20 - delayed: 0 - reserved: 0 - done: 0 PS E:/wwwroot/channel-pub-api> ./yii upload-asset-queue/info --color=0 Jobs - waiting: 0 - delayed: 0 - reserved: 0 - done: 0 PS E:/wwwroot/channel-pub-api> ./yii pub-article-queue/info --color=0 Jobs - waiting: 0 - delayed: 0 - reserved: 0 - done: 0 PS E:/wwwroot/channel-pub-api> ./yii source-callback-queue/info --color=0 Jobs - waiting: 0 - delayed: 0 - reserved: 0 - done: 0
2、查看 Redis 中的队列数据,cpa:queue:copy:asset.messages。数据量为 20 条。如图1
3、打开2个命令行窗口,在窗口1中运行队列后,然后在窗口2中运行同样的队列。以确认当同样的队列同时运行时(集群部署时),同一个队列作业是否会重复执行。在执行一段时间后,窗口1已经完成了2个队列作业,窗口2已经完成了3个队列作业。如图2
4、在执行一段时间后,查看 Redis 中的队列数据,cpa:queue:copy:asset.messages。数据量为 15 条。如图3
5、又执行一段时间后,在窗口3中运行同样的队列。查看队列状态,已经确定队列作业中等待执行的为10个,进行中的为2个,完成的为8个。如图4
6、最后分别查看3个窗口中的运行情况。窗口1执行了8个队列作业。窗口2执行了12个队列作业。窗口3执行了0个队列作业,原因应该在于执行第1个队列作业时,超时失败了。
窗口1
PS E:/wwwroot/channel-pub-api> ./yii copy-asset-queue/run --verbose=1 --isolate=1 --color=0 2021-08-31 17:37:31 [pid: 64508] - Worker is started 2021-08-31 17:37:32 [1] common/jobs/CopyAssetJob (attempt: 1, pid: 64508) - Started 2021-08-31 17:42:35 [1] common/jobs/CopyAssetJob (attempt: 1, pid: 64508) - Done (303.486 s) 2021-08-31 17:42:36 [3] common/jobs/CopyAssetJob (attempt: 1, pid: 64508) - Started 2021-08-31 17:49:45 [3] common/jobs/CopyAssetJob (attempt: 1, pid: 64508) - Done (429.834 s) 2021-08-31 17:49:46 [7] common/jobs/CopyAssetJob (attempt: 1, pid: 64508) - Started 2021-08-31 17:57:23 [7] common/jobs/CopyAssetJob (attempt: 1, pid: 64508) - Done (457.296 s) 2021-08-31 17:57:24 [10] common/jobs/CopyAssetJob (attempt: 1, pid: 64508) - Started 2021-08-31 18:06:16 [10] common/jobs/CopyAssetJob (attempt: 1, pid: 64508) - Done (531.560 s) 2021-08-31 18:06:16 [14] common/jobs/CopyAssetJob (attempt: 1, pid: 64508) - Started 2021-08-31 18:14:02 [14] common/jobs/CopyAssetJob (attempt: 1, pid: 64508) - Done (465.649 s) 2021-08-31 18:14:02 [15] common/jobs/CopyAssetJob (attempt: 1, pid: 64508) - Started 2021-08-31 18:21:18 [15] common/jobs/CopyAssetJob (attempt: 1, pid: 64508) - Done (436.172 s) 2021-08-31 18:21:19 [18] common/jobs/CopyAssetJob (attempt: 1, pid: 64508) - Started 2021-08-31 18:27:51 [18] common/jobs/CopyAssetJob (attempt: 1, pid: 64508) - Done (392.171 s) 2021-08-31 18:27:52 [20] common/jobs/CopyAssetJob (attempt: 1, pid: 64508) - Started 2021-08-31 18:27:54 [20] common/jobs/CopyAssetJob (attempt: 1, pid: 64508) - Done (2.273 s) 2021-08-31 18:27:54 [pid: 64508] - Worker is stopped (0:50:23) PS E:/wwwroot/channel-pub-api> ./yii copy-asset-queue/info --color=0 Jobs - waiting: 0 - delayed: 0 - reserved: 0 - done: 20
窗口2
PS E:/wwwroot/channel-pub-api> ./yii copy-asset-queue/run --verbose=1 --isolate=1 --color=0 2021-08-31 17:37:37 [pid: 63068] - Worker is started 2021-08-31 17:37:37 [2] common/jobs/CopyAssetJob (attempt: 1, pid: 63068) - Started 2021-08-31 17:44:18 [2] common/jobs/CopyAssetJob (attempt: 1, pid: 63068) - Done (400.251 s) 2021-08-31 17:44:18 [4] common/jobs/CopyAssetJob (attempt: 1, pid: 63068) - Started 2021-08-31 17:44:20 [4] common/jobs/CopyAssetJob (attempt: 1, pid: 63068) - Done (1.969 s) 2021-08-31 17:44:21 [5] common/jobs/CopyAssetJob (attempt: 1, pid: 63068) - Started 2021-08-31 17:49:35 [5] common/jobs/CopyAssetJob (attempt: 1, pid: 63068) - Done (314.755 s) 2021-08-31 17:49:36 [6] common/jobs/CopyAssetJob (attempt: 1, pid: 63068) - Started 2021-08-31 17:56:27 [6] common/jobs/CopyAssetJob (attempt: 1, pid: 63068) - Done (411.015 s) 2021-08-31 17:56:27 [8] common/jobs/CopyAssetJob (attempt: 1, pid: 63068) - Started 2021-08-31 17:56:29 [8] common/jobs/CopyAssetJob (attempt: 1, pid: 63068) - Done (1.854 s) 2021-08-31 17:56:30 [9] common/jobs/CopyAssetJob (attempt: 1, pid: 63068) - Started 2021-08-31 18:02:47 [9] common/jobs/CopyAssetJob (attempt: 1, pid: 63068) - Done (377.192 s) 2021-08-31 18:02:48 [12] common/jobs/CopyAssetJob (attempt: 1, pid: 63068) - Started 2021-08-31 18:02:50 [12] common/jobs/CopyAssetJob (attempt: 1, pid: 63068) - Done (2.375 s) 2021-08-31 18:02:51 [13] common/jobs/CopyAssetJob (attempt: 1, pid: 63068) - Started 2021-08-31 18:09:42 [13] common/jobs/CopyAssetJob (attempt: 1, pid: 63068) - Done (411.228 s) 2021-08-31 18:09:42 [11] common/jobs/CopyAssetJob (attempt: 2, pid: 63068) - Started 2021-08-31 18:17:07 [11] common/jobs/CopyAssetJob (attempt: 2, pid: 63068) - Done (445.086 s) 2021-08-31 18:17:08 [16] common/jobs/CopyAssetJob (attempt: 1, pid: 63068) - Started 2021-08-31 18:17:11 [16] common/jobs/CopyAssetJob (attempt: 1, pid: 63068) - Done (3.012 s) 2021-08-31 18:17:12 [17] common/jobs/CopyAssetJob (attempt: 1, pid: 63068) - Started 2021-08-31 18:22:13 [17] common/jobs/CopyAssetJob (attempt: 1, pid: 63068) - Done (300.874 s) 2021-08-31 18:22:13 [19] common/jobs/CopyAssetJob (attempt: 1, pid: 63068) - Started 2021-08-31 18:28:40 [19] common/jobs/CopyAssetJob (attempt: 1, pid: 63068) - Done (386.913 s) 2021-08-31 18:28:40 [pid: 63068] - Worker is stopped (0:51:03)
窗口3
PS E:/wwwroot/channel-pub-api> ./yii copy-asset-queue/run --verbose=1 --isolate=1 --color=0 2021-08-31 17:59:31 [pid: 65344] - Worker is started 2021-08-31 17:59:31 [11] common/jobs/CopyAssetJob (attempt: 1, pid: 65344) - Started 2021-08-31 18:09:32 [11] common/jobs/CopyAssetJob (attempt: 1, pid: 65344) - Error > Symfony/Component/Process/Exception/ProcessTimedOutException: The process "C:/php-7.4.6/php.exe E:/wwwroot/channel-pub-api/yii "copy-asset-queue/exec" 11 600 1 65344 --verbose=1 --color=0" exceeded the timeout of 600 seconds. 2021-08-31 19:07:22 [pid: 65344] - Worker is stopped (1:07:51)
7、查看队列作业20条的操作结果。relative_path 即下载后文件的相对路径,队列作业全部执行成功。因为相对路径都存在。但是搜索应该下载的视频文件,其数量应该等于 30 。但是在下载目录中的实际视频文件数量为 32 。现在的问题是,不太清楚多出的 2 个文件是如何产生的(其中一个文件应该是早就存在的,仅多出了1个文件)。如图5
8、已经确定窗口3中的队列作业超时失败,影响到了2条队列作业。虽然失败了,但是文件已经下载成功。如图6
PS E:/wwwroot/channel-pub-api> ./yii source-callback-queue/info --color=0 Jobs - waiting: 2 - delayed: 0 - reserved: 0 - done: 0
9、窗口3中的队列作业为何超时,原因应该在于本地环境的下行带宽是有限的。窗口1与窗口2已经占用了大量的带宽资源比例,导致窗口3没有带宽资源可用,因此超时。再次重复以上步骤。决定将超时时间从 600 秒调整为 1800 秒。3个窗口的队列作业全部执行成功,其中最长的执行时间为:708.338 s,难怪在之前会超时失败。因为相对路径都存在。但是搜索应该下载的视频文件,其数量应该等于 30 。但是在下载目录中的实际视频文件数量为 31 (应该在于程序内部的原因,有待于后续分析)。如图7
窗口1
PS E:/wwwroot/channel-pub-api> ./yii copy-asset-queue/run --verbose=1 --isolate=1 --color=0 2021-09-01 10:37:09 [pid: 73336] - Worker is started 2021-09-01 10:37:09 [1] common/jobs/CopyAssetJob (attempt: 1, pid: 73336) - Started 2021-09-01 10:43:23 [1] common/jobs/CopyAssetJob (attempt: 1, pid: 73336) - Done (374.069 s) 2021-09-01 10:43:24 [6] common/jobs/CopyAssetJob (attempt: 1, pid: 73336) - Started 2021-09-01 10:52:59 [6] common/jobs/CopyAssetJob (attempt: 1, pid: 73336) - Done (575.304 s) 2021-09-01 10:53:02 [10] common/jobs/CopyAssetJob (attempt: 1, pid: 73336) - Started 2021-09-01 11:03:11 [10] common/jobs/CopyAssetJob (attempt: 1, pid: 73336) - Done (609.431 s) 2021-09-01 11:03:12 [14] common/jobs/CopyAssetJob (attempt: 1, pid: 73336) - Started 2021-09-01 11:13:11 [14] common/jobs/CopyAssetJob (attempt: 1, pid: 73336) - Done (598.621 s) 2021-09-01 11:13:12 [16] common/jobs/CopyAssetJob (attempt: 1, pid: 73336) - Started 2021-09-01 11:13:14 [16] common/jobs/CopyAssetJob (attempt: 1, pid: 73336) - Done (2.588 s) 2021-09-01 11:13:15 [17] common/jobs/CopyAssetJob (attempt: 1, pid: 73336) - Started 2021-09-01 11:21:02 [17] common/jobs/CopyAssetJob (attempt: 1, pid: 73336) - Done (466.632 s) 2021-09-01 11:21:03 [20] common/jobs/CopyAssetJob (attempt: 1, pid: 73336) - Started 2021-09-01 11:21:05 [20] common/jobs/CopyAssetJob (attempt: 1, pid: 73336) - Done (2.528 s) 2021-09-01 11:21:05 [pid: 73336] - Worker is stopped (0:43:56) PS E:/wwwroot/channel-pub-api> ./yii copy-asset-queue/info --color=0 Jobs - waiting: 0 - delayed: 0 - reserved: 0 - done: 20 PS E:/wwwroot/channel-pub-api> ./yii source-callback-queue/info --color=0 Jobs - waiting: 0 - delayed: 0 - reserved: 0 - done: 0 PS E:/wwwroot/channel-pub-api>
窗口2
PS E:/wwwroot/channel-pub-api> ./yii copy-asset-queue/run --verbose=1 --isolate=1 --color=0 2021-09-01 10:38:02 [pid: 67976] - Worker is started 2021-09-01 10:38:03 [3] common/jobs/CopyAssetJob (attempt: 1, pid: 67976) - Started 2021-09-01 10:49:51 [3] common/jobs/CopyAssetJob (attempt: 1, pid: 67976) - Done (708.338 s) 2021-09-01 10:49:52 [8] common/jobs/CopyAssetJob (attempt: 1, pid: 67976) - Started 2021-09-01 10:49:55 [8] common/jobs/CopyAssetJob (attempt: 1, pid: 67976) - Done (3.721 s) 2021-09-01 10:49:56 [9] common/jobs/CopyAssetJob (attempt: 1, pid: 67976) - Started 2021-09-01 10:57:06 [9] common/jobs/CopyAssetJob (attempt: 1, pid: 67976) - Done (429.860 s) 2021-09-01 10:57:06 [11] common/jobs/CopyAssetJob (attempt: 1, pid: 67976) - Started 2021-09-01 11:08:16 [11] common/jobs/CopyAssetJob (attempt: 1, pid: 67976) - Done (669.532 s) 2021-09-01 11:08:17 [2] common/jobs/CopyAssetJob (attempt: 2, pid: 67976) - Started 2021-09-01 11:18:33 [2] common/jobs/CopyAssetJob (attempt: 2, pid: 67976) - Done (616.247 s) 2021-09-01 11:18:35 [19] common/jobs/CopyAssetJob (attempt: 1, pid: 67976) - Started 2021-09-01 11:25:39 [19] common/jobs/CopyAssetJob (attempt: 1, pid: 67976) - Done (424.108 s) 2021-09-01 11:25:40 [pid: 67976] - Worker is stopped (0:47:38)
窗口3
PS E:/wwwroot/channel-pub-api> ./yii copy-asset-queue/run --verbose=1 --isolate=1 --color=0 2021-09-01 10:38:29 [pid: 75656] - Worker is started 2021-09-01 10:38:30 [4] common/jobs/CopyAssetJob (attempt: 1, pid: 75656) - Started 2021-09-01 10:38:32 [4] common/jobs/CopyAssetJob (attempt: 1, pid: 75656) - Done (2.338 s) 2021-09-01 10:38:33 [5] common/jobs/CopyAssetJob (attempt: 1, pid: 75656) - Started 2021-09-01 10:46:21 [5] common/jobs/CopyAssetJob (attempt: 1, pid: 75656) - Done (468.368 s) 2021-09-01 10:46:22 [7] common/jobs/CopyAssetJob (attempt: 1, pid: 75656) - Started 2021-09-01 10:58:04 [7] common/jobs/CopyAssetJob (attempt: 1, pid: 75656) - Done (702.171 s) 2021-09-01 10:58:05 [12] common/jobs/CopyAssetJob (attempt: 1, pid: 75656) - Started 2021-09-01 10:58:08 [12] common/jobs/CopyAssetJob (attempt: 1, pid: 75656) - Done (2.769 s) 2021-09-01 10:58:09 [13] common/jobs/CopyAssetJob (attempt: 1, pid: 75656) - Started 2021-09-01 11:06:11 [13] common/jobs/CopyAssetJob (attempt: 1, pid: 75656) - Done (482.736 s) 2021-09-01 11:06:12 [15] common/jobs/CopyAssetJob (attempt: 1, pid: 75656) - Started 2021-09-01 11:16:43 [15] common/jobs/CopyAssetJob (attempt: 1, pid: 75656) - Done (631.170 s) 2021-09-01 11:16:44 [18] common/jobs/CopyAssetJob (attempt: 1, pid: 75656) - Started 2021-09-01 11:24:50 [18] common/jobs/CopyAssetJob (attempt: 1, pid: 75656) - Done (485.917 s) 2021-09-01 11:24:55 [pid: 75656] - Worker is stopped (0:46:27)
10、得出结论,Yii2 队列扩展 是支持并行运行的,即使在集群中同时运行,也是没有问题的。
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/250610.html