Skip to content

Commit

Permalink
Service & play fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
wisq committed Apr 11, 2012
1 parent 0ef3e60 commit aadd30c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
2 changes: 1 addition & 1 deletion bin/play.rb
@@ -1,4 +1,4 @@
#!/usr/bin/ruby #!/usr/bin/env ruby


require 'lib/manager' require 'lib/manager'


Expand Down
37 changes: 23 additions & 14 deletions bin/service.rb
Expand Up @@ -3,10 +3,8 @@
$LOAD_PATH << File.dirname(__FILE__) + '/..' $LOAD_PATH << File.dirname(__FILE__) + '/..'
require 'lib/manager' require 'lib/manager'


if Object.const_defined(:Encoding) $stdout.sync = true
Encoding.default_external = 'UTF-8' $stderr.sync = true
Encoding.default_internal = 'UTF-8'
end


# This is a daemonised version for running # This is a daemonised version for running
# as a service via e.g. runit. # as a service via e.g. runit.
Expand All @@ -27,7 +25,7 @@ def initialize(station_file)
end end


def run def run
Signal.trap('HUP') { tune_to_file } Signal.trap('HUP') { reload }
Signal.trap('INT') { abort_fetch } Signal.trap('INT') { abort_fetch }
Signal.trap('USR1') { show_status } Signal.trap('USR1') { show_status }


Expand All @@ -38,44 +36,55 @@ def run
loop { sleep } loop { sleep }
end end


def reload
@manager.load_banned
tune_to_file
end

def tune_to_file def tune_to_file
station = File.open(@station_file, &:readline).chomp station = File.open(@station_file, &:readline).chomp
if station != @station if station != @station
puts "Tuning to #{station.inspect} ..." puts "Tuning to #{station.inspect} ..."
@lastfm.tune_to(station) @lastfm.tune_to(station)
@manager.pending.clear @manager.pending.clear
puts "Tuned." puts "Tuned."
@station = station
else else
puts "Already tuned to #{station.inspect}." puts "Already tuned to #{station.inspect}."
end end
end end


def show_status def show_status
lists = [ lists = [
['<<', $m.playlist.last(10)], ['<<', @manager.playlist.last(10)],
['##', [$m.fetching]], ['##', [@manager.fetching]],
['>>', $m.pending], ['>>', @manager.pending],
] ]


for prefix, list in lists do for prefix, list in lists do
for e in list do for e in list do
next if e.nil? # $m.fetching nil next if e.nil? # @manager.fetching nil
percent = sprintf('%3d%%', e.percent_done) percent = sprintf('%3d%%', e.percent_done)
puts "#{prefix} #{percent}: #{e.track.artist} -- #{e.track.title}" puts "#{prefix} #{percent}: #{e.track.artist} -- #{e.track.title}"
end end
end end
puts "-- Time remaining: #{$p.time_remaining} seconds" puts "-- Time remaining: #{@player.time_remaining} seconds"
end end


def abort_fetch def abort_fetch
if $m.fetching then if @manager.fetching then
track = $m.fetching.track track = @manager.fetching.track
puts "Aborting download: #{track.artist} -- #{track.title}" puts "Aborting download: #{track.artist} -- #{track.title}"
$m.fetching.abort_fetch @manager.fetching.abort_fetch
else else
puts 'No download in progress.' puts 'No download in progress.'
end end
end end
end end


CachefmService.new(ARGV.first).run puts "Starting up ..."
begin
CachefmService.new(ARGV.first).run
ensure
puts "Shutting down."
end

0 comments on commit aadd30c

Please sign in to comment.