Module: Capistrano::Jukebox

Defined in:
lib/capistrano/jukebox.rb

Class Method Summary collapse

Class Method Details

.load_into(configuration) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/capistrano/jukebox.rb', line 5

def self.load_into(configuration)

  configuration.load do

    # Start playing music before :deploy
    on :before, :only => :deploy do
      file = configuration[:file]
      file = jeopardy_song if file.nil?
      start_playing(file)
    end

    # Stop playing music after :deploy
    on :after, :only => :deploy do
      stop_playing
    end

    # Jukebox start/stop cap tasks
    namespace :jukebox do
      desc 'Start playing music on the jukebox'
      task :play, :roles => :app, :except => {:no_release => true} do
        file = configuration[:file]
        file = jeopardy_song if file.nil?
        start_playing(file)
      end

      desc 'Stop the jukebox from playing music'
      task :stop, :roles => :app, :except => {:no_release => true} do
        puts "You have forced the jukebox to stop the music."
        stop_playing
      end
    end

    # Starting mplayer playing the file in the background
    def start_playing(file)
      puts "Jukebox is starting to play '#{file}'"
      @pid = fork do
        exec("mplayer #{file} -really-quiet -framedrop -cache 16384 -cache-min 20/100")
      end
    end

    # Stops mplayer by killing it by pid
    # If no pid is known, all running player processes are killed
    def stop_playing
      unless @pid.nil?
        ::Process.kill("TERM", @pid)
      else
        exec("killall mplayer")
      end
    end

    def jeopardy_song
      File.expand_path('../../../music/jeopardy_thinking.mp3', __FILE__)
    end

  end

end