1
1
import cloneDeep from 'lodash.clonedeep' ;
2
2
import sha256 from 'crypto-js/sha256' ;
3
3
import Md5 from 'crypto-js/md5' ;
4
- import RRule from 'rrule' ;
4
+ import { RRule , datetime } from 'rrule' ;
5
5
import Base64 from 'crypto-js/enc-base64' ;
6
6
import isPublished from '../util/isPublished' ;
7
7
import Logger from '../logger/logger' ;
8
8
import ConfigLoader from '../config-loader' ;
9
+ import ScheduleUtils from "../schedule" ;
9
10
10
11
/**
11
12
* ScheduleService.
@@ -174,49 +175,36 @@ class ScheduleService {
174
175
static findScheduledSlides ( playlists , regionId ) {
175
176
const slides = [ ] ;
176
177
177
- const now = new Date ( ) ;
178
- const startOfDay = new Date ( ) ;
179
- startOfDay . setUTCHours ( 0 , 0 , 0 , 0 ) ;
180
-
181
178
playlists . forEach ( ( playlist ) => {
182
179
const { schedules } = playlist ;
183
180
184
181
if ( ! isPublished ( playlist ?. published ) ) {
185
182
return ;
186
183
}
187
184
188
- let occurs = true ;
185
+ let active = true ;
189
186
190
187
// If schedules are set for the playlist, do not show playlist unless a schedule is active.
191
188
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 ;
216
204
} ) ;
217
205
}
218
206
219
- if ( occurs ) {
207
+ if ( active ) {
220
208
playlist ?. slidesData ?. forEach ( ( slide ) => {
221
209
if ( ! isPublished ( slide . published ) ) {
222
210
return ;
@@ -229,6 +217,8 @@ class ScheduleService {
229
217
newSlide . executionId = `EXE-ID-${ executionId } ` ;
230
218
slides . push ( newSlide ) ;
231
219
} ) ;
220
+ } else {
221
+ Logger . log ( 'info' , `Playlist ${ playlist [ '@id' ] } not scheduled for now` ) ;
232
222
}
233
223
} ) ;
234
224
0 commit comments