capistranocapistrano3wheneverwhenever-capistrano

How to run a capistrano task within a whenever task?


I have whenever gem setup properly. How can I run that capistrano from my whenever schedule.rb?

my schedule.rb

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

My capistrano task:

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. And should I run that rake task within whenever and capistrano?


Solution

  • Jan, you may find documentation very useful 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. 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).