ElasticBeanstalk worker cron 每分钟运行多次

Posted

技术标签:

【中文标题】ElasticBeanstalk worker cron 每分钟运行多次【英文标题】:ElasticBeanstalk worker cron running multiple times per minute 【发布时间】:2019-07-20 15:01:51 【问题描述】:

我有一个运行 Laravel 的 EB 工作环境(单实例),用于运行计划和分派的作业。

我面临的问题是每分钟多次调用 cron,导致计划的作业加倍。这可以在worker的访问日志中看到;

-------------------------------------
/var/log/httpd/access_log
-------------------------------------
127.0.0.1 (-) - - [27/Feb/2019:00:07:01 +0000] "POST /worker/schedule HTTP/1.1" 302 217 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:07:01 +0000] "POST /worker/schedule HTTP/1.1" 302 217 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:07:01 +0000] "POST /worker/schedule HTTP/1.1" 302 217 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:07:01 +0000] "POST /worker/schedule HTTP/1.1" 302 217 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:07:01 +0000] "POST /worker/schedule HTTP/1.1" 302 217 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:07:07 +0000] "POST /scheduled.php HTTP/1.1" 302 215 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:07:07 +0000] "POST /scheduled.php HTTP/1.1" 302 215 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:07:08 +0000] "POST /scheduled.php HTTP/1.1" 302 215 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:07:09 +0000] "POST /scheduled.php HTTP/1.1" 302 215 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:07:09 +0000] "POST /scheduled.php HTTP/1.1" 302 215 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:07:09 +0000] "POST /scheduled.php HTTP/1.1" 302 215 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:07:10 +0000] "POST /scheduled.php HTTP/1.1" 302 215 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:07:11 +0000] "POST /scheduled.php HTTP/1.1" 302 215 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:07:11 +0000] "POST /scheduled.php HTTP/1.1" 302 215 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:08:00 +0000] "POST /worker/schedule HTTP/1.1" 302 217 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:08:00 +0000] "POST /worker/schedule HTTP/1.1" 302 217 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:08:00 +0000] "POST /worker/schedule HTTP/1.1" 302 217 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:08:00 +0000] "POST /worker/schedule HTTP/1.1" 302 217 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:08:00 +0000] "POST /worker/schedule HTTP/1.1" 302 217 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:08:00 +0000] "POST /worker/schedule HTTP/1.1" 302 217 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:08:00 +0000] "POST /worker/schedule HTTP/1.1" 302 217 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:08:00 +0000] "POST /worker/schedule HTTP/1.1" 302 217 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:08:00 +0000] "POST /worker/schedule HTTP/1.1" 302 217 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:08:00 +0000] "POST /worker/schedule HTTP/1.1" 302 217 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:08:00 +0000] "POST /worker/schedule HTTP/1.1" 302 217 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:08:00 +0000] "POST /worker/schedule HTTP/1.1" 302 217 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:08:00 +0000] "POST /worker/schedule HTTP/1.1" 302 217 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:08:15 +0000] "POST /worker/schedule HTTP/1.1" 302 217 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:08:15 +0000] "POST /worker/schedule HTTP/1.1" 302 217 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:08:15 +0000] "POST /worker/schedule HTTP/1.1" 302 217 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:08:15 +0000] "POST /worker/schedule HTTP/1.1" 302 217 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:08:15 +0000] "POST /worker/schedule HTTP/1.1" 302 217 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:08:15 +0000] "POST /worker/schedule HTTP/1.1" 302 217 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:08:15 +0000] "POST /worker/schedule HTTP/1.1" 302 217 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:08:15 +0000] "POST /worker/schedule HTTP/1.1" 302 217 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:08:15 +0000] "POST /worker/schedule HTTP/1.1" 302 217 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:08:15 +0000] "POST /worker/schedule HTTP/1.1" 302 217 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:08:15 +0000] "POST /worker/schedule HTTP/1.1" 302 217 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:08:15 +0000] "POST /worker/schedule HTTP/1.1" 302 217 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:08:15 +0000] "POST /worker/schedule HTTP/1.1" 302 217 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:08:15 +0000] "POST /worker/schedule HTTP/1.1" 302 217 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:08:16 +0000] "POST /worker/schedule HTTP/1.1" 302 217 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:08:16 +0000] "POST /worker/schedule HTTP/1.1" 302 217 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:08:16 +0000] "POST /scheduled.php HTTP/1.1" 302 215 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:08:16 +0000] "POST /worker/schedule HTTP/1.1" 302 217 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:08:16 +0000] "POST /worker/schedule HTTP/1.1" 302 217 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:08:16 +0000] "POST /worker/schedule HTTP/1.1" 302 217 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:08:16 +0000] "POST /worker/schedule HTTP/1.1" 302 217 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:08:16 +0000] "POST /worker/schedule HTTP/1.1" 302 217 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:08:16 +0000] "POST /worker/schedule HTTP/1.1" 302 217 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:08:16 +0000] "POST /scheduled.php HTTP/1.1" 302 215 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:08:16 +0000] "POST /worker/schedule HTTP/1.1" 302 217 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:08:16 +0000] "POST /worker/schedule HTTP/1.1" 302 217 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:08:16 +0000] "POST /worker/schedule HTTP/1.1" 302 217 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:08:16 +0000] "POST /worker/schedule HTTP/1.1" 302 217 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:08:16 +0000] "POST /worker/schedule HTTP/1.1" 302 217 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:08:48 +0000] "POST /scheduled.php HTTP/1.1" 302 215 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:09:00 +0000] "POST /worker/schedule HTTP/1.1" 302 217 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:09:00 +0000] "POST /worker/schedule HTTP/1.1" 302 217 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:09:00 +0000] "POST /worker/schedule HTTP/1.1" 302 217 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:09:00 +0000] "POST /worker/schedule HTTP/1.1" 302 217 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:09:00 +0000] "POST /worker/schedule HTTP/1.1" 302 217 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:09:00 +0000] "POST /worker/schedule HTTP/1.1" 302 217 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:09:00 +0000] "POST /worker/schedule HTTP/1.1" 302 217 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:09:00 +0000] "POST /worker/schedule HTTP/1.1" 302 217 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:09:00 +0000] "POST /worker/schedule HTTP/1.1" 302 217 "-" "aws-sqsd/2.4"
127.0.0.1 (-) - - [27/Feb/2019:00:09:00 +0000] "POST /worker/schedule HTTP/1.1" 302 217 "-" "aws-sqsd/2.4"

导致我的 CloudWatch 日志中出现类似的重复;

20:40:00  INFO: Processing /2019/20190225.csv [] []
20:40:00  INFO: Processing /2019/20190225.csv [] []
20:40:00  INFO: Processing /2019/20190225.csv [] []
20:40:01  INFO: Processing /2019/20190225.csv [] []
20:40:01  INFO: Processing /2019/20190225.csv [] []
20:40:02  INFO: Processing /2019/20190225.csv [] []
20:40:02  INFO: Processing /2019/20190225.csv [] []
20:40:03  INFO: Processing /2019/20190225.csv [] []
20:40:03  INFO: Processing /2019/20190225.csv [] []
20:40:04  INFO: Processing /2019/20190225.csv [] []
20:40:04  INFO: Processing /2019/20190225.csv [] []
20:40:06  INFO: Processing /2019/20190225.csv [] []
20:40:06  INFO: Processing /2019/20190225.csv [] []
20:40:06  INFO: Processing /2019/20190225.csv [] []
20:40:06  INFO: Processing /2019/20190225.csv [] []
20:40:06  INFO: Processing /2019/20190225.csv [] []
20:40:06  INFO: Processing /2019/20190225.csv [] []

我正在使用dusterio/laravel-aws-worker 与 SQS 队列交互以处理分派的作业。

工人在cron.yaml中有以下内容;

version: 1
cron:
    - name: "schedule"
      url: "/worker/schedule"
      schedule: "* * * * *"

我的工作人员的控制台内核正在安排这样的每小时作业;

protected function schedule(Schedule $schedule)

    $schedule->job(new ImportData())
        ->hourly()
        ->timezone(config('app.timezone')); // Pacific/Auckland

这里有什么我遗漏的吗?

【问题讨论】:

【参考方案1】:

对于那些偶然发现这个问题的人来说,问题是 HTTPS 重定向发生在我的应用程序上,并且同一个应用程序同时部署到 Web 层和工作层。 302 响应会导致 SQS 重试到您的最大重试次数限制,但每个调用仍在执行作业。

一种解决方案是从该重定向中排除 SQS 代理;

files:
    /etc/httpd/conf.d/http-redirect.conf:
        mode: "000644"
        owner: root
        group: root
        content: |
            RewriteEngine On
            RewriteCond %HTTP:X-Forwarded-Proto !https
            RewriteCond %HTTP_USER_AGENT !ELB-HealthChecker
            RewriteCond %HTTP_USER_AGENT !aws-sqsd
            RewriteRule (.*) https://%HTTP_HOST%REQUEST_URI

另一种方法是防止将此脚本部署到工作环境。

【讨论】:

以上是关于ElasticBeanstalk worker cron 每分钟运行多次的主要内容,如果未能解决你的问题,请参考以下文章

Aws Elasticbeanstalk cron.yaml 工作人员问题

在 Elastic Beanstalk 上启动 SQS celery worker

AWS Elastic Beanstalk Worker 队列行为

AWS 运行 Rails 应用程序的架构指南(Web/Worker 设置)

使用 crontab 在弹性 beanstalk worker 实例上运行 django 管理命令

AWS Elastic Beanstalk Worker - 根据可用队列消息的数量进行扩展