gpt4 book ai didi

deployment - 如何使用 capistrano 在 rails 服务器上运行脚本?

转载 作者:行者123 更新时间:2023-12-03 09:31:56 25 4
gpt4 key购买 nike

我使用放置在“script/mailman_server”中的 gem "mailman" 编写了一个名为 mailman_server 的脚本

    #!/usr/bin/env ruby
require "rubygems"
require "bundler/setup"
require "mailman"

#Mailman.config.logger = Logger.new("log/mailman.log")
Mailman.config.poll_interval = 3
Mailman.config.pop3 = {
server: 'server', port: 110,
username: "loginid",
password: "password"
}

Mailman::Application.run do
default do
p "Found a new message"
# 'perform some action here'
end
end

它从我的帐户中获取所有电子邮件,然后我对它们进行处理。

我有我的deploy.rb 文件

set :stages, %w(production)     #various environments
load "deploy/assets" #precompile all the css, js and images... before deployment..
require "bundler/capistrano" # install all the new missing plugins...
require 'delayed/recipes' # load this for delayed job..
require 'capistrano/ext/multistage' # deploy on all the servers..
require "rvm/capistrano" # if you are using rvm on your server..
require './config/boot'
require 'airbrake/capistrano' # using airbrake in your application for crash notifications..

set :delayed_job_args, "-n 2" # number of delayed job workers


before "deploy:assets:symlink", "deploy:copy_database_file"
before "deploy:update_code", "delayed_job:stop" # stop the previous deployed job workers...
after "deploy:start", "delayed_job:start" #start the delayed job
after "deploy:restart", "delayed_job:restart" # restart it..
after "deploy:update", "deploy:cleanup" #clean up temp files etc.
set :rvm_ruby_string, '1.9.3' # ruby version you are using...
set :rvm_type, :user

server "my_server_ip", :app, :web, :db, :primary => true

set(:application) { "my_application_name" }
set (:deploy_to) { "/home/user/#{application}/#{stage}" }
set :user, 'user'
set :keep_releases, 3
set :repository, "git@bitbucket.org:my_random_git_repo_url"
set :use_sudo, false
set :scm, :git
default_run_options[:pty] = true
ssh_options[:forward_agent] = true
set :deploy_via, :remote_cache
set :git_shallow_clone, 1
set :git_enable_submodules, 1

namespace :deploy do
task :start do ; end
task :stop do ; end
task :restart, :roles => :app, :except => { :no_release => true } do
run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
end
task :copy_database_file do
run "ln -sf #{shared_path}/database.yml #{release_path}/config/database.yml"
end
end

我想在每次部署到服务器时都执行这个脚本。此外,每当我部署代码时,我都需要停止此脚本。

我不知道如何在服务器上使用 Capistrano 启动或停止此脚本。

最佳答案

你可以尝试像这样在启动时保存进程的 pid

run "cd #{deploy_to}/current; ./script/mailman_server &; echo &! > /var/run/mailman_server.pid" #untested

并用

停止
run "kill `cat /var/run/mailman_server.pid`;  rm /var/run/mailman_server.pid"

但我认为你应该查看Foreman ,它提供了在开发中运行作业的便捷方式,并支持将作业导出到 upstart 或 inid.d 脚本以进行生产,因此您只需要使用

启动或停止相应的服务
run "sudo /etc/init.d/mailman_server start"
run "sudo /etc/init.d/mailman_server stop"

关于deployment - 如何使用 capistrano 在 rails 服务器上运行脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14336908/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com