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_message(msg) ⇒ 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
110 111 112 |
# File 'lib/transcoding_machine/server/worker.rb', line 110 def find_media_players(media_player_ids) @media_players.slice(*media_player_ids).values 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 "Transcoding: BLA BLA BLA" = YAML.load(msg.body) puts "consuming message #{.inspect}" selected_media_players = find_media_players([:media_players]) if selected_media_players.any? if bucket = @s3.bucket([:bucket].to_s) key = bucket.key([:key].to_s) if key.exists? transcoder = Transcoder.new(key.name, selected_media_players, @media_formats, TranscodingEventListener.new(), :bucket => bucket.name) transcoder.start else "Input file not found (bucket: #{[:bucket]} key: #{[:key]})" end else "Input bucket not found (bucket: #{[:bucket]})" end else "No media players found #{[:media_players].inspect}" end end_time = Time.now if true #test if transcode was successful msg.delete "Transcoded: BLA BLA BLA" end end |
#message_loop ⇒ Object
114 115 116 117 118 119 120 121 122 123 |
# File 'lib/transcoding_machine/server/worker.rb', line 114 def msg = @work_queue.pop if msg.nil? #@log.puts "no messages" set_state("idle") sleep 5 else (msg) end end |
#run ⇒ Object
129 130 131 132 133 |
# File 'lib/transcoding_machine/server/worker.rb', line 129 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
125 126 127 |
# File 'lib/transcoding_machine/server/worker.rb', line 125 def shutdown @shutdown = true end |