11import cloneDeep from 'lodash.clonedeep' ;
22import sha256 from 'crypto-js/sha256' ;
33import Md5 from 'crypto-js/md5' ;
4- import RRule from 'rrule' ;
4+ import { RRule , datetime } from 'rrule' ;
55import Base64 from 'crypto-js/enc-base64' ;
66import isPublished from '../util/isPublished' ;
77import Logger from '../logger/logger' ;
88import ConfigLoader from '../config-loader' ;
9+ import ScheduleUtils from "../schedule" ;
910
1011/**
1112 * ScheduleService.
@@ -174,49 +175,36 @@ class ScheduleService {
174175 static findScheduledSlides ( playlists , regionId ) {
175176 const slides = [ ] ;
176177
177- const now = new Date ( ) ;
178- const startOfDay = new Date ( ) ;
179- startOfDay . setUTCHours ( 0 , 0 , 0 , 0 ) ;
180-
181178 playlists . forEach ( ( playlist ) => {
182179 const { schedules } = playlist ;
183180
184181 if ( ! isPublished ( playlist ?. published ) ) {
185182 return ;
186183 }
187184
188- let occurs = true ;
185+ let active = true ;
189186
190187 // If schedules are set for the playlist, do not show playlist unless a schedule is active.
191188 if ( schedules . length > 0 ) {
192- occurs = false ;
193-
194- schedules . forEach ( ( schedule ) => {
195- const rrule = RRule . fromString ( schedule . rrule . replace ( '\\n' , '\n' ) ) ;
196- rrule . between (
197- // Subtract duration from now to make sure all relevant occurrences are considered.
198- new Date (
199- now . getTime ( ) - ( schedule . duration ? schedule . duration * 1000 : 0 )
200- ) ,
201- now ,
202- true ,
203- function iterator ( occurrenceDate ) {
204- const occurrenceEnd = new Date (
205- occurrenceDate . getTime ( ) + schedule . duration * 1000
206- ) ;
207-
208- if ( now >= occurrenceDate && now <= occurrenceEnd ) {
209- occurs = true ;
210- // Break the iteration.
211- return false ;
212- }
213- return true ;
214- }
215- ) ;
189+ active = false ;
190+
191+ // Run through all schedule item and see if it occurs now. If one or more occur now, the playlist is active.
192+ schedules . every ( ( schedule ) => {
193+ const scheduleOccurs = ScheduleUtils . occursNow ( schedule . rrule , schedule . duration ) ;
194+
195+ if ( scheduleOccurs ) {
196+ active = true ;
197+
198+ // Break iteration.
199+ return false ;
200+ }
201+
202+ // Continue iteration.
203+ return true ;
216204 } ) ;
217205 }
218206
219- if ( occurs ) {
207+ if ( active ) {
220208 playlist ?. slidesData ?. forEach ( ( slide ) => {
221209 if ( ! isPublished ( slide . published ) ) {
222210 return ;
@@ -229,6 +217,8 @@ class ScheduleService {
229217 newSlide . executionId = `EXE-ID-${ executionId } ` ;
230218 slides . push ( newSlide ) ;
231219 } ) ;
220+ } else {
221+ Logger . log ( 'info' , `Playlist ${ playlist [ '@id' ] } not scheduled for now` ) ;
232222 }
233223 } ) ;
234224
0 commit comments