2013-01-15 123 views
3

我已經寫了使用gem "mailman"放在「腳本/ mailman_server」如何在使用capistrano的rails服務器上運行腳本?

#!/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 

它獲取所有郵件從我的帳戶,然後我做他們的處理名爲mailman_server腳本。

我有我的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, "[email protected]: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啓動或停止此腳本。

+0

如何手動停止腳本?只需在處理時按Ctrl + C?如果是這種情況,最好添加另一個腳本來終止由'mailman_server'啓動的進程,並從'deploy'回調之前調用它。 –

+0

@Prakash是的,我這樣做。因爲我需要該腳本在服務器上運行,所以我使用了屏幕。 –

回答

0

你可以嘗試的東西保存進程的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,它提供了方便的方法在開發中運行作業,並支持將作業導出爲新手或inid.d腳本以供生產,因此您只需啓動或停止相應的服務即可。

run "sudo /etc/init.d/mailman_server start" 
run "sudo /etc/init.d/mailman_server stop" 
相關問題