Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lgtm
Browse files Browse the repository at this point in the history
axsuul committed Nov 11, 2023
1 parent fafd9df commit 45257fa
Showing 3 changed files with 23 additions and 9 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -19,6 +19,8 @@ These environment variables can be passed into the container (defaults are in pa
- Plex Media Server token
* `PLEX_TIMEOUT` (`10`)
- How long to wait for Plex Media Server to respond
* `PLEX_RETRIES_COUNT` (`0`)
- How many times to retry Plex Media Server requests if they fail
* `METRICS_PREFIX` (`plex`)
- What to prefix metric names with
* `METRICS_MEDIA_COLLECTING_INTERVAL_SECONDS` (`300`)
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ services:
# Can be set in .env
- PLEX_ADDR=${PLEX_ADDR}
- PLEX_TOKEN=${PLEX_TOKEN}
- PLEX_RETRIES_COUNT=${PLEX_RETRIES_COUNT}
- METRICS_MEDIA_COLLECTING_INTERVAL_SECONDS=300
volumes:
- .:/srv:delegated
29 changes: 20 additions & 9 deletions lib/middleware/collector.rb
Original file line number Diff line number Diff line change
@@ -9,7 +9,9 @@ def initialize(app)

# Plex configs
@plex_addr = ENV["PLEX_ADDR"] || "http://localhost:32400"
@plex_token = ENV["PLEX_TOKEN"]
@plex_timeout = ENV["PLEX_TIMEOUT"]&.to_i || 10
@plex_retries_count = ENV["PLEX_RETRIES_COUNT"]&.to_i || 0

# Metrics configs
@metrics_prefix = ENV["METRICS_PREFIX"] || "plex"
@@ -203,15 +205,24 @@ def fetch_media_section_count(key:, params: {}, **options)
end

def send_plex_api_request(method:, endpoint:, **options)
response = HTTP
.timeout(@plex_timeout)
.headers(
"X-Plex-Token" => ENV["PLEX_TOKEN"],
"Accept" => "application/json",
)
.public_send(method, "#{@plex_addr}#{endpoint}", **options)

JSON.parse(response)
count = 0

# Keep trying request if it fails until number of retries have been exhausted
loop do
count += 1
response = HTTP
.timeout(@plex_timeout)
.headers(
"X-Plex-Token" => @plex_token,
"Accept" => "application/json",
)
.public_send(method, "#{@plex_addr}#{endpoint}", **options)

return JSON.parse(response)

rescue HTTP::Error => e
raise(e) unless @plex_retries_count <= count
end
end

# Set metric values and reset all other labels that werenn't passed in

0 comments on commit 45257fa

Please sign in to comment.