在Elastic Beanstalk中运行cron作业 [英] Running a cron job in Elastic Beanstalk

查看:100
本文介绍了在Elastic Beanstalk中运行cron作业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在Django Elastic Beanstalk应用程序中具有一个功能,如下所示:

So I have a functionality in a Django Elastic Beanstalk app that works like so:

  • 下载文件
  • 解析文件,使用文件中的数据运行对API的一些调用
  • 使用新数据更新EB实例的数据库

在测试实例中,我仅设置了本地cron作业.我只是在Django应用程序的特定URL上调用了wget,它将运行命令.

In testing instances where I just set up a local cron job. I just called wget on a specific URL of my Django application and it will run the command.

我的问题是如何在多实例的Elastic Beanstalk应用程序中处理此问题.我的EB应用程序只有一个实例可以运行此命令.我想避免数据库出现争用情况,以及避免从多个实例对外部API的冗余调用.也就是说,只有一个实例可以写入数据.

My problem is how to handle this in a multi-instanced Elastic Beanstalk application. Only one instance of my EB application should run this command. I want to avoid race conditions on the database and redundant calls to external API's from multiple instances. i.e. only one instance should be writing to the databe.

但是,谷歌搜索显示设置cron任务很尴尬,特别是如果您像我一样是EB的新手.听起来最有前途的方法似乎是cron.yaml方法,但据我所见,似乎没有一个在网络上的任何地方设置cron worker环境的示例.

However, Googling around shows setting up cron jobs is awkward, particularly if your new to EB like I am. The most promising sounding method seems to be the cron.yaml method, but there does not seem to be an example of setting up a cron worker environment anywhere on the web from what I can see.

我的理解是:

  • 您在EB项目的根目录中包含一个cron.yaml文件.
  • 部署项目
  • cron作业是在工作人员环境(?)中自动设置的.
  • 您定义的命令在指定的时间运行.

我的问题是如何确保只有一个实例将运行此命令?我对cron.yaml的工作方式是否有正确的想法?我是否缺少

My question is how do you make sure that only one instance will run this command? Do I have the right idea on how cron.yaml works or is there something I'm missing

推荐答案

只有一个实例可以运行该命令,因为cron作业本身实际上并没有在cron守护进程中运行.

Only one instance will run the command because the cron job does not actually run in a cron daemon per-se.

几乎没有什么概念可以帮助您快速了解亚马逊的Elastic Beanstalk心态.

There are few concepts that might help you quickly grok amazon's Elastic Beanstalk mindset.

  • 一个有弹性的beantalk环境必须选择一个领导实例,而该领导实例必须永远只有一个(而且必须是一个健康的实例,等等).
  • 工作环境通过SQS(简单队列服务)队列分配工作.
  • 一旦从队列中读取了一条消息,它将被视为进行中",直到工作程序返回200或请求超时/失败为止.在第一种情况下,该消息被删除,在后一种情况下,它重新进入队列. (重新驱动策略可以确定一条消息在发送到死信队列"之前可能失败多少次)
  • 飞行中的消息无法再次读取(除非返回).

队列中的消息一次仅由工作环境中的一个实例的一个接收一次.

A message in the queue is picked up only once by one of the instances in the worker environment at a time.

现在,cron.yaml文件实际上只是告诉领导者在计划中指定的时间在队列中创建具有特殊属性的消息.然后,当它找到此消息时,仅作为对指定URL的POST请求发送给一个实例.

Now the cron.yaml file actually just tells the leader to create a message in the queue with special attributes, at the times specified in the schedule. When it then finds this message, it's dispatched to one instance only as a POST request to the specified URL.

当我在工作人员环境中使用Django时,我将创建一个cron应用程序,其视图映射到我想要的操作.例如,如果我想定期轮询Facebook端点,则可能具有路径/cron/facebook/poll/,该路径在views.py

When I use Django in a worker environment I create a cron app with views that map to the action I want. For example if I wanted to periodically poll a Facebook endpoint I might have a path /cron/facebook/poll/ which calls a poll_facebook() function in views.py

这样,如果我有一个cron.yaml,它将每小时对Facebook进行一次轮询:

That way if I have a cron.yaml as follows, it'll poll Facebook once every hour:

version: 1
cron:
 - name: "pollfacebook"
   url: "/cron/facebook/poll/"
   schedule: "0 * * * *"

这篇关于在Elastic Beanstalk中运行cron作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆