Class: TranscodingMachine::Server::Worker
- Inherits:
-
Object
- Object
- TranscodingMachine::Server::Worker
- Defined in:
- lib/transcoding_machine/server/worker.rb
Instance Method Summary collapse
- #find_media_players(media_player_ids) ⇒ Object
- #handle_analyze(transcoder, message_properties) ⇒ Object
- #handle_message(msg) ⇒ Object
- #handle_transcode(transcoder, message_properties) ⇒ Object
-
#initialize(log) ⇒ Worker
constructor
initialize queues from names.
- #message_loop ⇒ Object
- #run ⇒ Object
- #send_log_message(message) ⇒ Object
- #send_status_message(status) ⇒ Object
-
#set_state(new_state) ⇒ Object
Send status when state changes, when state becomes busy, or every minute (even if there is no state change).
- #shutdown ⇒ Object
Constructor Details
#initialize(log) ⇒ Worker
initialize queues from names
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 |
# File 'lib/transcoding_machine/server/worker.rb', line 10 def initialize(log) @log = log @shutdown = false @state = "none" @last_status_time = Time.now @sqs = RightAws::SqsGen2.new @s3 = RightAws::S3.new(nil, nil, :server => 's3.amazonaws.com', :port => 80, :protocol => 'http') begin @work_queue = @sqs.queue(Ec2Environment.work_queue_name) if @work_queue.nil? @log.puts "error #{$!} #{Ec2Environment.work_queue_name}" raise "no work queue" end @status_queue = @sqs.queue(Ec2Environment.status_queue_name) if @status_queue.nil? @log.puts "error #{$!} #{Ec2Environment.status_queue_name}" raise "no status queue" end rescue @log.puts "error #{$!}" raise "cannot list queues" end @media_players, @media_formats = TranscodingMachine.load_models_from_hash(Ec2Environment.transcoding_settings) Transcoder.[:storage] = S3Storage.new set_state("idle") end |
Instance Method Details
#find_media_players(media_player_ids) ⇒ Object
138 139 140 |
# File 'lib/transcoding_machine/server/worker.rb', line 138 def find_media_players(media_player_ids) @media_players.slice(*media_player_ids).values end |
#handle_analyze(transcoder, message_properties) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/transcoding_machine/server/worker.rb', line 110 def handle_analyze(transcoder, ) "Analyzing: #{[:bucket]}/#{[:key]}" selected_media_players = find_media_players([:media_players]) if selected_media_players.any? source_file_attributes, source_media_format, target_media_formats = transcoder.analyze_source_file(selected_media_players) .delete(:media_players) [:action] = :transcode [:file_attributes] = source_file_attributes target_media_formats.each do |media_format| [:media_format] = media_format @work_queue.push(.to_yaml) end else "No media players found #{[:media_players].inspect}" end "Analyzed: #{[:bucket]}/#{[:key]}" end |
#handle_message(msg) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/transcoding_machine/server/worker.rb', line 71 def (msg) #pp msg set_state("busy") start_time = Time.now = YAML.load(msg.body) puts "consuming message #{.inspect}" if bucket = @s3.bucket([:bucket].to_s) key = bucket.key([:key].to_s) if key.exists? transcoder = Transcoder.new(key.name, @media_formats, TranscodingEventListener.new(), :bucket => bucket.name) case [:action] when :transcode handle_transcode(transcoder, ) else handle_analyze(transcoder, ) end else "Input file not found (bucket: #{[:bucket]} key: #{[:key]})" end else "Input bucket not found (bucket: #{[:bucket]})" end end_time = Time.now if true #test if transcode was successful msg.delete end end |
#handle_transcode(transcoder, message_properties) ⇒ Object
132 133 134 135 136 |
# File 'lib/transcoding_machine/server/worker.rb', line 132 def handle_transcode(transcoder, ) "Transcoding: #{[:bucket]}/#{[:key]} to format #{[:media_format]}" transcoder.transcode([:file_attributes], [:media_format]) "Transcoded: #{[:bucket]}/#{[:key]} to format #{[:media_format]}" end |
#message_loop ⇒ Object
142 143 144 145 146 147 148 149 150 151 |
# File 'lib/transcoding_machine/server/worker.rb', line 142 def msg = @work_queue.pop if msg.nil? #@log.puts "no messages" set_state("idle") sleep 5 else (msg) end end |
#run ⇒ Object
157 158 159 160 161 |
# File 'lib/transcoding_machine/server/worker.rb', line 157 def run while ! @shutdown end end |
#send_log_message(message) ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'lib/transcoding_machine/server/worker.rb', line 51 def () @log.puts msg = { :type => 'log', :instance_id => Ec2Environment.instance_id, :message => , :timestamp => Time.now} @status_queue.push(msg.to_yaml) end |
#send_status_message(status) ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/transcoding_machine/server/worker.rb', line 40 def (status) now = Time.now msg = { :type => 'status', :instance_id => Ec2Environment.instance_id, :state => 'active', :load_estimate => status == 'busy' ? 1 : 0, :timestamp => now} @status_queue.push(msg.to_yaml) @last_status_time = now end |
#set_state(new_state) ⇒ Object
Send status when state changes, when state becomes busy, or every minute (even if there is no state change).
62 63 64 65 66 67 68 69 |
# File 'lib/transcoding_machine/server/worker.rb', line 62 def set_state(new_state) if new_state != @state || new_state == "busy" || @last_status_time + 60 < Time.now @state = new_state (new_state) end end |
#shutdown ⇒ Object
153 154 155 |
# File 'lib/transcoding_machine/server/worker.rb', line 153 def shutdown @shutdown = true end |