@@ -159,7 +159,7 @@ public static function getPlaylistActiveStreamsCount($playlist): int
159159
160160 try {
161161 $ endpoint = $ service ->apiBaseUrl .'/streams/by-metadata ' ;
162- $ response = Http::timeout (3 )->acceptJson ()
162+ $ response = Http::timeout (10 )->acceptJson ()
163163 ->withHeaders ($ service ->apiToken ? [
164164 'X-API-Token ' => $ service ->apiToken ,
165165 ] : [])
@@ -187,41 +187,69 @@ public static function getPlaylistActiveStreamsCount($playlist): int
187187
188188 /**
189189 * Get active streams for a specific playlist using metadata filtering
190+ * Returns null on failure to distinguish from legitimately empty results
190191 */
191- public static function getPlaylistActiveStreams ($ playlist ): array
192+ public static function getPlaylistActiveStreams ($ playlist, int $ retries = 2 ): ? array
192193 {
193194 $ service = new self ;
194195
195196 if (empty ($ service ->apiBaseUrl )) {
196- return [];
197+ Log::warning ('Cannot fetch playlist streams: m3u-proxy API URL not configured ' );
198+
199+ return null ;
197200 }
198201
199- try {
200- $ endpoint = $ service ->apiBaseUrl .'/streams/by-metadata ' ;
201- $ response = Http::timeout (3 )->acceptJson ()
202- ->withHeaders ($ service ->apiToken ? [
203- 'X-API-Token ' => $ service ->apiToken ,
204- ] : [])
205- ->get ($ endpoint , [
206- 'field ' => 'playlist_uuid ' ,
207- 'value ' => $ playlist ->uuid ,
208- 'active_only ' => true ,
209- ]);
202+ $ endpoint = $ service ->apiBaseUrl .'/streams/by-metadata ' ;
203+ $ attempt = 0 ;
210204
211- if ($ response ->successful ()) {
212- $ data = $ response ->json ();
205+ while ($ attempt < $ retries ) {
206+ try {
207+ $ response = Http::timeout (10 )->acceptJson ()
208+ ->withHeaders ($ service ->apiToken ? [
209+ 'X-API-Token ' => $ service ->apiToken ,
210+ ] : [])
211+ ->get ($ endpoint , [
212+ 'field ' => 'playlist_uuid ' ,
213+ 'value ' => $ playlist ->uuid ,
214+ 'active_only ' => true ,
215+ ]);
213216
214- return $ data [ ' matching_streams ' ] ?? [];
215- }
217+ if ( $ response -> successful ()) {
218+ $ data = $ response -> json ();
216219
217- Log::warning ('Failed to fetch playlist streams from m3u-proxy: HTTP ' .$ response ->status ());
220+ return $ data ['matching_streams ' ] ?? [];
221+ }
218222
219- return [];
220- } catch (Exception $ e ) {
221- Log::warning ('Failed to fetch playlist streams from m3u-proxy: ' .$ e ->getMessage ());
223+ Log::warning ('Failed to fetch playlist streams from m3u-proxy: HTTP ' .$ response ->status (), [
224+ 'attempt ' => $ attempt + 1 ,
225+ 'max_attempts ' => $ retries ,
226+ ]);
227+
228+ $ attempt ++;
229+ if ($ attempt < $ retries ) {
230+ sleep (1 ); // Wait 1 second before retry
231+ }
222232
223- return [];
233+ } catch (Exception $ e ) {
234+ Log::warning ('Failed to fetch playlist streams from m3u-proxy: ' .$ e ->getMessage (), [
235+ 'attempt ' => $ attempt + 1 ,
236+ 'max_attempts ' => $ retries ,
237+ ]);
238+
239+ $ attempt ++;
240+ if ($ attempt < $ retries ) {
241+ sleep (1 ); // Wait 1 second before retry
242+ }
243+ }
224244 }
245+
246+ // All retries failed
247+ Log::error ('All attempts to fetch playlist streams from m3u-proxy failed ' , [
248+ 'playlist_uuid ' => $ playlist ->uuid ,
249+ 'attempts ' => $ retries ,
250+ ]);
251+
252+ return null ;
225253 }
226254
227255 /**
@@ -237,7 +265,7 @@ public static function isChannelActive(Channel $channel): bool
237265
238266 try {
239267 $ endpoint = $ service ->apiBaseUrl .'/streams/by-metadata ' ;
240- $ response = Http::timeout (2 )->acceptJson ()
268+ $ response = Http::timeout (10 )->acceptJson ()
241269 ->withHeaders ($ service ->apiToken ? [
242270 'X-API-Token ' => $ service ->apiToken ,
243271 ] : [])
@@ -283,7 +311,7 @@ public static function getActiveStreamsCountByMetadata(string $field, string $va
283311
284312 try {
285313 $ endpoint = $ service ->apiBaseUrl .'/streams/by-metadata ' ;
286- $ response = Http::timeout (3 )->acceptJson ()
314+ $ response = Http::timeout (10 )->acceptJson ()
287315 ->withHeaders ($ service ->apiToken ? [
288316 'X-API-Token ' => $ service ->apiToken ,
289317 ] : [])
0 commit comments