How to run a capistrano task within a whenever task?

161 views Asked by At

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?

2

There are 2 answers

0
Ruslan K On BEST ANSWER

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).

0
will_in_wi On

I'd suggest your latter option, moving the logic to a rake task and executing it from both whenever and Capistrano. It'll be vastly easier and cleaner to do.