简体   繁体   English

如何在任何时候任务中运行 capistrano 任务?

[英]How to run a capistrano task within a whenever task?

I have whenever gem setup properly.我有whenever gem 设置正确。 How can I run that capistrano from my whenever schedule.rb ?我怎样才能从我的whenever schedule.rb运行那个capistrano

my schedule.rb我的日程表.rb

every 1.minute, roles: [:app] do
   # how to run here a capistrano task
   # invoke 'my_app:test'
end

My capistrano task:我的 capistrano 任务:

namespace :my_app do

  desc 'test'

  task :test do
    on roles(:web) do
      puts "the task runs"
    end
  end
end

Or should I move that task into a rake task.或者我应该将该任务移至rake任务。 And should I run that rake task within whenever and capistrano ?我应该在whenevercapistrano内运行那个 rake 任务吗?

I'd suggest your latter option, moving the logic to a rake task and executing it from both whenever and Capistrano.我建议您使用后一种选择,将逻辑移至 rake 任务并从 when 和 Capistrano 执行它。 It'll be vastly easier and cleaner to do.这样做会更容易和更干净。

Jan, you may find documentation very useful https://github.com/javan/whenever . Jan,您可能会发现文档非常有用https://github.com/javan/whenever The code example below - I just copied and edited it from the doc.下面的代码示例 - 我只是从文档中复制和编辑它。

schedule.rb

# run this task only on servers with the :app role in Capistrano
# see Capistrano roles section below
every :day, at: '12:20am', roles: [:web] do
  rake "app_server:task"
end

lib/tasks/test_task.rb

namespace :my_app do
  desc 'test'
  task :test do
    puts "the task runs"
  end
end

I believe it's easier to create Rake task and run it via Whenever.我相信创建 Rake 任务并通过 When 运行它更容易。 You can choose the Role, so basically you don't need to use Capistrano task (I believe you want to use it just because of Role).你可以选择角色,所以基本上你不需要使用 Capistrano 任务(我相信你想使用它只是因为角色)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM