Rails中的AWS / EBS后台作业

时间:2016-07-29 12:32:13

标签: ruby-on-rails amazon-web-services amazon-rds amazon-ebs

我在AWS上使用Rails编写的Elastic Beanstalk / RDS / EC2 /等应用程序。

该应用程序有一些数据库条目需要在一段时间后删除,并且正在考虑使用gem来创建后台任务。

然而,我看到的所有宝石都只是以编程方式创建cron任务的方式。使用Elastic Beanstalk,正在使用的服务器实例将进行扩展/更改,因此cron任务将丢失。还有另一种在Rails中设置后台作业的方法吗?

1 个答案:

答案 0 :(得分:0)

我通常使用您可以在应用中的container_commands文件夹内的.config文件中配置的.ebextensions来解决此问题。 The idea is that the code is executed on so-called 'leader' instance in your webservers pool. It is not considered as a bulletproof solution but does the job and mostly works (it will fail when autoscaling comes into play though). 使用分布式互斥锁(redis-mutex或数据库支持)来阻止除获得锁定之外的所有进程。

#create config file, use numbers/names that suit your needs:
#.ebextensions/02_container_commands.config 
container_commands:
  01_cron_tasks:
    command: "cat .ebextensions/cron_tasks.txt > /etc/cron.d/yourapp && chmod 644 /etc/cron.d/yourapp"
    leader_only: true

cron会选择这个,只需将crontab定义放在.ebextensions/cron_tasks.txt文件中,并将此一个添加到您的Git仓库中。

0 5 * * * root bash -l -c "su -m webapp; cd /var/app/current && rake your_rake_task"

令人遗憾的是,似乎亚马逊AWS团队仍未提出适当,方便的解决方案,以便在Elastic Beanstalk环境中使用Rails安排后台作业。他们的Worker Tier要求您在应用程序中添加一个专用端点,用于侦听来自其自己的cron实现的消息。这很糟糕,因为它会强制您向核心应用程序逻辑(路由,控制器)添加特定于托管的代码,并且需要运行更多服务器。

与Heroku等平台相比,AWS的另一个缺点是缺少对Redki支持的排队系统的一键支持,如Sidekiq和可以通过类似cron的调度程序生成的按需工作人员。

相关问题