-
Notifications
You must be signed in to change notification settings - Fork 8
/
start_and_wait.rb
50 lines (40 loc) · 2.06 KB
/
start_and_wait.rb
1
2
3
4
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
####> This code and all components © 2015 – 2019 Wowza Media Systems, LLC. All rights reserved.
####> This code is licensed pursuant to the BSD 3-Clause License.
require "wsc_sdk"
require_relative "../client" # Get our client
require_relative "../helpers" # Include some helpers to make the code more direct
# Ensure the args passed in are present
arguments = ask_for_arguments(__FILE__, transcoder_id: nil)
# Extract some data into convenience variables
transcoders = $client.transcoders
transcoder_id = arguments[0]
# Request the transcoder object
transcoder = transcoders.find(transcoder_id)
# Handle an API error (in the helpers.rb file)
handle_api_error(transcoder, "There was an error finding the transcoder") unless transcoder.success?
# Get the result of starting the transcoder. We add a code block, which
# instructs the SDK to enter a wait loop, and periodically polls the API
# for the transcoder state until either the `started` state is returned or
# the timeout period is reached.
state = transcoder.start do |wait_state, transcoder_state|
# This code will execute each time the state of the transcoder is polled
# during the wait loop. You can check the wait_state to determine what the
# outcomes of each request are.
if wait_state == :waiting
# We're still waiting for the started state to be returned
puts "Waiting..."
elsif wait_state == :complete
# We've successfully started the transcoder, so output the completed state
# Defined in helpers.rb
output_model_attributes(transcoder_state, "Completed Transcoder State:")
elsif wait_state == :timeout
# A timeout occurs if the start state isn't reached within the timeout
# limit, which defaults to 30 seconds.
puts "TIMEOUT: Could not start the transcoder within the timeout period."
end
end
# Handle an API error. (In the helpers.rb file)
handle_api_error(state, "There was an error starting the transcoder") unless state.success?
# Output the transcoder state
# Defined in helpers.rb
output_model_attributes(state, "Final Transcoder State:")