Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何通过Capistrano3运行自定义rake任务?_Ruby On Rails_Ruby_Rake_Capistrano_Capistrano3 - Fatal编程技术网

Ruby on rails 如何通过Capistrano3运行自定义rake任务?

Ruby on rails 如何通过Capistrano3运行自定义rake任务?,ruby-on-rails,ruby,rake,capistrano,capistrano3,Ruby On Rails,Ruby,Rake,Capistrano,Capistrano3,通过这种方式,我可以在远程服务器上通过capistrano运行rake命令 例如,我有一个带有一些方法的lib/task/repasse.rake desc "it's take csv file, makes some changes and fill db with this info" task :example1 => :environment do require 'csv' rows_to_insert = [] # some actions # ... end

通过这种方式,我可以在远程服务器上通过capistrano运行rake命令

例如,我有一个带有一些方法的
lib/task/repasse.rake

desc "it's take csv file, makes some changes and fill db with this info"
task :example1 => :environment do
  require 'csv'
  rows_to_insert = []
  # some actions
  # ...
end
在本地服务器上一切正常-我只运行了
rake重分析:example1
这就是工作(正确填充数据库)。 所以问题是-在部署之后,如何在真实主机上运行此命令

我正在使用Rails4.1+Capistrano3

网站上的例子对我不适用

如果我尝试
cap production rake:invoke task=reparse:land
它失败于:

cap aborted!
Don't know how to build task 'rake:invoke'

一些修复

namespace :somenamespace do
  task :runrake do  
    on roles(:all), in: :sequence, wait: 5 do      
      within release_path do
        execute :rake, ENV['task'], "RAILS_ENV=production"
      end 
    end
  end
end
通过这种方式,它开始通过

cap production somenamespace:runrake task=custom_task_file:custom_method1

您需要在Capistrano配置中加载自定义rake任务:

# config/deploy.rb || config/deploy/production.rb
load 'lib/task/reparse.rake'

基于
capistrano/rails
gem,在控制台
cap-T
中检查新任务:


您可以创建相应的capistrano任务来运行特定的rake任务,如下所示:

namespace :guests do
  desc 'Remove guest users older than 7 days'
  task :clean do
    on roles(:app) do
      within release_path do
        with rails_env: fetch(:rails_env) do
          execute :rake, 'guests:delete_old_guest_users'
        end
      end
    end
  end
end

所以我一直在做这个。这很好用。但是,您需要一个格式化程序来真正利用代码

如果不想使用格式化程序,只需将日志级别设置为调试模式

SSHKit.config.output_verbosity = Logger::DEBUG
帽子材料 这是我为处理上述代码而构建的格式化程序。它基于sshkit中内置的:textsimple,但调用自定义任务并不是一种坏方法。哦,这许多不适用于最新版本的sshkit gem。我知道它适用于1.7.1。我这样说是因为主分支更改了可用的SSHKit::Command方法

module SSHKit
  module Formatter
    class SuperSimple < SSHKit::Formatter::Abstract
      def write(obj)
        case obj
        when SSHKit::Command    then write_command(obj)
        when SSHKit::LogMessage then write_log_message(obj)
        end
      end
      alias :<< :write

      private

      def write_command(command)
        unless command.started? && SSHKit.config.output_verbosity == Logger::DEBUG
          original_output << "Running #{String(command)} #{command.host.user ? "as #{command.host.user}@" : "on "}#{command.host}\n"
          if SSHKit.config.output_verbosity == Logger::DEBUG
            original_output << "Command: #{command.to_command}" + "\n"
          end
        end

        unless command.stdout.empty?
          command.stdout.lines.each do |line|
            original_output << line
            original_output << "\n" unless line[-1] == "\n"
          end
        end

        unless command.stderr.empty?
          command.stderr.lines.each do |line|
            original_output << line
            original_output << "\n" unless line[-1] == "\n"
          end
        end

      end

      def write_log_message(log_message)
        original_output << log_message.to_s + "\n"
      end
    end
  end
end
模块SSHKit
模块格式化程序
类SuperSimple
$ cap production invoke:rake TASK=some:rake_task

很抱歉,我没有仔细阅读回复:P-您是否尝试过添加capistrano任务
execute:rake,ENV['task']
?@UriAgassi thx请发表评论,我做了一些修复并将其添加到问题中)可能重复@UriAgassi是的,这是可行的,但唯一的方法,就像我上面描述的那样,不适用于capistrano的3.5.0。他们访问了airbrusshMore info In deploy.rb set:format,:airbrusshThank!您可以添加一个示例命令行来显示如何运行该命令行吗?
module SSHKit
  module Formatter
    class SuperSimple < SSHKit::Formatter::Abstract
      def write(obj)
        case obj
        when SSHKit::Command    then write_command(obj)
        when SSHKit::LogMessage then write_log_message(obj)
        end
      end
      alias :<< :write

      private

      def write_command(command)
        unless command.started? && SSHKit.config.output_verbosity == Logger::DEBUG
          original_output << "Running #{String(command)} #{command.host.user ? "as #{command.host.user}@" : "on "}#{command.host}\n"
          if SSHKit.config.output_verbosity == Logger::DEBUG
            original_output << "Command: #{command.to_command}" + "\n"
          end
        end

        unless command.stdout.empty?
          command.stdout.lines.each do |line|
            original_output << line
            original_output << "\n" unless line[-1] == "\n"
          end
        end

        unless command.stderr.empty?
          command.stderr.lines.each do |line|
            original_output << line
            original_output << "\n" unless line[-1] == "\n"
          end
        end

      end

      def write_log_message(log_message)
        original_output << log_message.to_s + "\n"
      end
    end
  end
end
$ cap production invoke:rake TASK=some:rake_task