每当 aws opsworks 上的 gem

Posted

技术标签:

【中文标题】每当 aws opsworks 上的 gem【英文标题】:Whenever gem on aws opsworks 【发布时间】:2014-02-01 18:50:45 【问题描述】:

有没有人有使用aws opsworks 上的when 宝石的经验/成功?有好的食谱吗?我可以将该配方放在单独的层上并将一个实例与该附加层相关联吗?或者有更好的方法吗?谢谢!!!

编辑:

我们最终做的有点不同......

代码:

无法真正贴出真实的代码,不过是这样的:

在 deploy/before_migrate.rb:

[:schedule].each do |config_name|
  Chef::Log.info("Processing config for #config_name")
  begin
    template "#release_path/config/#config_name.rb" do |_config_file|
      variables(
        config_name => node[:deploy][:APP_NAME][:config_files][config_name]
      )
      local true
      source "#release_path/config/#config_name.rb.erb"
    end
  rescue => e
    Chef::Log.error e
    raise "Error processing config for #config_name: #e"
  end
end

在部署/after_restart.rb 中:

execute 'start whenever' do
  cwd release_path
  user node[:deploy][:APP_NAME][:user] || 'deploy'
  command 'bundle exec whenever -i APP_NAME'
end

在 config/schedule.rb.erb 中:

 <% schedule = @schedule ||  %>

set :job_template, "bash -l -c 'export PATH=/usr/local/bin:$PATH && :job'"
job_type :runner,  'cd :path && bundle exec rails runner -e :environment ":task" :output'
job_type :five_runner, 'cd :path && timeout 300 bundle exec rails runner -e :environment ":task" :output'
set :output, 'log/five_minute_job.log' 
every 5.minutes, at: <%= schedule[:five_minute_job_minute] || 0 %> do
  five_runner 'Model.method'
end

【问题讨论】:

我们最终处理如下: 在堆栈 json 中添加一个值以指定运行时 cron 的主机,默认为 rails_app1 在 rails 项目中有一个部署挂钩,仅在时运行那个主人。这会将 cron 放在第一台主机或我们指定的任何主机上,并且非常容易配置和管理。我们还有一个 erb 文件,它被评估为 schedule.rb 以便我们可以设置各种参数,例如每天运行的时间或每小时运行的分钟。该 erb 文件也在部署挂钩中进行评估。 如果可以的话,可以分享一下代码吗? 是的,请放心,一些更详细的建议会很棒。 抱歉耽搁了。在我们的堆栈 json 中,我们有 "deploy" : "my_app" : "job_hosts" : "whenever" : "rails-app1"...。在部署钩子 after_restart.rb 中,我们有类似 if node[:deploy][:my_app][:job_hosts][:whenever] == node[:opsworks][:instance][:hostname] execute 'start whenever' do cwd release_path user node[:deploy][:yardhound][:user] || 'deploy' command 'bundle exec whenever -i my_app' environment 'RAILS_ENV' =&gt; rails_env end end 的内容 【参考方案1】:

我们的仓库中有一本随时可用的食谱,非常欢迎您在这里使用:https://github.com/freerunningtech/frt-opsworks-cookbooks。我假设您熟悉将自定义说明书添加到您的 opsworks 堆栈。

我们通常在它自己的层上运行它,该层还包括应用程序部署所需的 Rails 食谱(而不是应用程序服务器):

配置:rails::configure 部署:无论何时部署::rails 取消部署:deploy::rails-undeploy

但是,我们通常也会将此实例部署为应用服务器,这意味着我们最终也会随时为我们使用的盒子提供请求。

有一个“陷阱”,您必须在 schedule.rb 顶部的 env 中设置路径,如下所示:

env :PATH, ENV['PATH']

【讨论】:

如何设置仅在一个层的实例上运行配方?我认为您使用--roles 选项,但是可以吗?在 Rails 应用程序中,layersrails-app 跨实例相同。

以上是关于每当 aws opsworks 上的 gem的主要内容,如果未能解决你的问题,请参考以下文章

AWS opsworks 上的 chef 12 脚本无法安装 jenkins

通过 AWS OpsWorks Nginx/Unicorn 服务器上的直接 IP 地址禁用 Web 访问

AWS Opsworks 未能添加 EC2 实例

aws opsworks 默认部署到哪里?

AWS OpsWorks 上 node.js 的应用程序日志在哪里

AWS+OpsWorks:如何使用 OpsWorks 启用 EC2 实例的详细监控