lib/Video/PlaybackMachine/Scheduler.pm untested

filter: Statementstmt 0.0% 0 / 148 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 30 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 237 CC 14 SCAR 20.3
CC
14
Cov
0%
finished
30.0
get_time_to_next
12.0
update
12.0
CRAP 7.6
1 package Video::PlaybackMachine::Scheduler;
2  
3 our $VERSION = '0.09'; # VERSION
4  
5 ####
6 #### Video::PlaybackMachine::Scheduler
7 ####
8 #### $Revision$
9 ####
10 #### Plays movies in the ScheduleTable at the appropriate times.
11 ####
12  
13 use Moo;
14  
15 use POE;
16 use POE::Session;
17 use Log::Log4perl;
18 use POSIX 'INT_MAX';
19  
20 use Video::PlaybackMachine::Player qw(PLAYER_STATUS_PLAY);
21 use Video::PlaybackMachine::Config;
22  
23 use Time::Duration;
24  
25 use Carp;
26  
27 ############################# Class Constants #############################
28  
29 use constant DEFAULT_SKIP_TOLERANCE => 30;
30  
31 use constant DEFAULT_IDLE_TOLERANCE => 15;
32  
33 our $Minimum_Fill = 7;
34  
35 ## Modes of operation
36  
37 # Starting up, haven't played anything yet
38 use constant START_MODE => 0;
39  
40 # Idle mode -- dead air
41 use constant IDLE_MODE => 1;
42  
43 # Between scheduled content
44 use constant FILL_MODE => 2;
45  
46 # Playing scheduled content
47 use constant PLAY_MODE => 3;
48  
49 ############################## Attributes ##############################
50  
51 has 'schedule_table' => ( 'is' => 'ro' );
52  
53 has 'player' => ( is => 'ro', builder => 1 );
54  
55 has 'filler' => ( is => 'ro', required => 1 ) ;
56  
57 has 'terminate_on_finish' => ( is => 'ro', 'default' => 1 );
58  
59 has 'run_forever' => ( is => 'ro', 'default' => 0 );
60  
61 has 'mode' => ( is => 'rw', default => START_MODE );
62  
63 has 'offset' => ( is => 'ro', default => 0 );
64  
65 has 'minimum_fill' => ( is => 'ro', default => 7 );
66  
67 has 'logger' => ( is => 'ro', default =>
68 sub {
69 Log::Log4perl->get_logger('Video::Playback::Scheduler');
70 }
71 );
72  
73 ############################# Object Methods ##############################
74  
75 sub _build_player {
76 my $self = shift;
77
78 return Video::PlaybackMachine::Player->new();
79 }
80  
81 sub spawn {
82 my $self = shift;
83  
84 POE::Session->create(
85  
86 object_states => [
87 $self => [
88 qw(_start finished update play_scheduled warning_scheduled schedule_next shutdown wait_for_scheduled query_next_scheduled)
89 ]
90 ],
91 );
92 }
93  
94 # The schedule time.
95 sub time {
96 my $self = shift;
97
98 return CORE::time() + $self->offset();
99 }
100  
101 ##
102 ## should_be_playing()
103 ##
104 ## Returns:
105 ##
106 ## Video::PlaybackMachine::ScheduleEntry
107 ##
108 ## Returns the movie, if any, which should be playing right
109 ## now.
110 ##
111 sub should_be_playing {
112 my $self = shift;
113  
114 my $current =
115 scalar $self->schedule_table()->get_entry_during( $self->time() );
116  
117 return $current;
118 }
119  
120 sub get_next_entry {
121 my $self = shift;
122  
123 return
124 scalar $self->schedule_table()->get_entries_after( $self->time(), 1 );
125  
126 }
127  
128 sub get_time_to_next {
129 my $self = shift;
130
131 my $next = $self->get_next_entry();
132  
133 # TODO The 'run_forever' business should probably be in the
134 # calling function, not down here
135 if ( defined($next) ) {
136 return $next->start_time() - $self->time();
137 }
138 elsif ( $self->run_forever() ) {
139 return INT_MAX;
140 }
141 else {
142 return;
143 }
144 }
145  
146 ############################# Session Methods #############################
147  
148 ##
149 ## _start()
150 ##
151 ## POE startup state.
152 ##
153 ## Called when the session begins. Spawns off a player and filler session
154 ## so that they can do whatever prep work they need to do, identifies
155 ## this session as a scheduler, and checks the database for things
156 ## that should be played.
157 ##
158 sub _start {
159 my ( $self, $kernel, $heap ) = @_[ OBJECT, KERNEL, HEAP ];
160  
161 # Hang out a shingle
162 $kernel->alias_set('Scheduler');
163  
164 # Set up our Player and our Filler
165 $heap->{player_session} = $self->player->spawn();
166 $heap->{filler_session} = $self->filler->spawn();
167
168 $self->logger->info('Scheduler started');
169  
170 # Check the database for things that need playing
171 $kernel->yield('update');
172  
173 }
174  
175 ##
176 ## query_next_scheduled()
177 ##
178 ## Designed to be called and return the next item on the schedule.
179 ## Although this is a POE event handler, it's useful only when called
180 ## with the call() command.
181 ##
182 sub query_next_scheduled {
183 my ($self, $num) = @_;
184
185 $num //= 1;
186  
187 return
188 $self->schedule_table()->get_entries_after( $self->time(), $num );
189  
190 }
191  
192 ##
193 ## update()
194 ##
195 ## POE state.
196 ##
197 ## Called whenever there's a change to the schedule
198 ## and we need to make sure that the Scheduler's state
199 ## matches what's in the database. Does NOT interrupt
200 ## a running movie.
201 ##
202 sub update {
203 my ( $self, $kernel, $heap ) = @_[ OBJECT, KERNEL, HEAP ];
204  
205 # Clear all schedule alarms
206 $kernel->alarm('play_scheduled');
207 $kernel->alarm('warning_scheduled');
208  
209 # If we're not playing
210 if ( $self->mode() != PLAY_MODE ) {
211  
212 # If there's something supposed to be playing
213 if ( my $entry = $self->should_be_playing() ) {
214  
215 $self->logger->debug("Time to play $entry");
216  
217 # Play it
218 $kernel->yield( 'play_scheduled', $entry, $self->get_seek($entry) );
219 return;
220  
221 } # End if supposed to be playing
222  
223 # Otherwise, fill gap until next scheduled item
224 else {
225 $kernel->yield('wait_for_scheduled');
226 }
227  
228 } # End if we're not playing
229  
230 # Set alarm to play next scheduled item
231 $kernel->delay( 'schedule_next', 5 );
232 }
233  
234 ##
235 ## finished()
236 ##
237 ## POE state.
238 ##
239 ## Called whenever playback is finished. It checks to see if there is anything
240 ## waiting for immediate play (i.e. was double-scheduled earlier) and plays it.
241 ## Otherwise, sends us to fill mode.
242 ##
243 ## Until we enter fill or play mode, this method puts us into idle mode.
244 ##
245 sub finished {
246 my ( $self, $kernel, $request, $response ) =
247 @_[ OBJECT, KERNEL, ARG0, ARG1 ];
248
249 my $now = CORE::time();
250  
251 # If we've been running longer than the restart interval, restart the system
252 my $config = Video::PlaybackMachine::Config->config();
253 if ( $config->restart_interval() > 0 ) {
254 if ( ( $now - $^T ) > $config->restart_interval() ) {
255 $self->{'logger'}->info("Shutting down for restart");
256 exit(0);
257 }
258 }
259  
260 # We're in idle mode now
261 $self->{mode} = IDLE_MODE;
262  
263 # Log the item that finished playing
264 $self->logger()->info("Movie played: ", $response->[0]);
265  
266 # If there's something else scheduled
267 if ( defined $self->get_next_entry($now) ) {
268  
269 # If there's enough time to start filling
270 if ( $self->get_time_to_next($now) > $self->{minimum_fill} ) {
271  
272 # Fill until next scheduled entry
273 $kernel->yield('wait_for_scheduled');
274  
275 } # End if enough time
276  
277 # Otherwise, go into idle mode till next
278 else {
279  
280 $self->logger()->debug( "Not filling: "
281 . $self->get_time_to_next($now)
282 . " too short for fill (minimum $self->{'minimum_fill'})\n"
283 );
284  
285 $self->mode(IDLE_MODE);
286  
287 }
288  
289 } # End if something else scheduled
290  
291 # Otherwise, nothing scheduled; shut down.
292 else {
293  
294 $kernel->yield('shutdown');
295  
296 }
297  
298  
299 }
300  
301 sub warning_scheduled {
302 my ( $self, $kernel ) = @_[ OBJECT, KERNEL ];
303  
304 # If we're in fill mode
305 if ( $self->mode() == FILL_MODE ) {
306  
307 # Send a warning message to the Filler
308 $kernel->post( 'Filler', 'warning', $self->get_time_to_next() );
309  
310 } # End if we're in fill mode
311  
312 # Otherwise, do nothing; we do not interrupt scheduled content.
313  
314 }
315  
316 sub play_scheduled {
317 my ( $self, $kernel, $session, $entry, $seek ) = @_[ OBJECT, KERNEL, SESSION, ARG0, ARG1 ];
318  
319 # If we're playing something scheduled
320 if ( ( $self->mode() == PLAY_MODE )
321 && ( $self->player()->get_status() == PLAYER_STATUS_PLAY ) )
322 {
323 $self->logger()->warn("Skipped playing " . $entry->mrl() . " since already playing");
324
325 return;
326  
327 } # End if we're playing something scheduled
328  
329 # Otherwise, we're ready to play
330 else {
331  
332 # Tell the Filler to stop filling
333 $kernel->post( 'Filler', 'stop' );
334  
335 # Mark that we're in play mode now
336 $self->mode(PLAY_MODE);
337  
338 # Start playing the movie
339 $kernel->post( 'Player', 'play',
340 $session->postback( 'finished', $self, CORE::time() ),
341 0, $entry->mrl );
342  
343 # Schedule the next item from the schedule table
344 $kernel->delay( 'schedule_next', 3 );
345  
346 } # End otherwise
347  
348 }
349  
350 sub wait_for_scheduled {
351 my ( $self, $kernel ) = @_[ OBJECT, KERNEL ];
352  
353 defined $self->get_time_to_next()
354 or $self->{'logger'}->logdie(
355 "Called wait_for_scheduled with nothing to wait for; schedule time is "
356 . scalar localtime( $self->time() ) );
357  
358 # If there's enough time before the next item to bother with fill
359 if ( $self->get_time_to_next() > $self->{minimum_fill} ) {
360  
361 # Mark that we're in Fill mode
362 $self->mode( FILL_MODE );
363  
364 # Tell our Filler to get to work
365 $kernel->post( 'Filler', 'start_fill', $self );
366  
367 } # End if enough time
368  
369 # Else not enough time
370 else {
371  
372 # Go to Idle mode
373 $self->mode( IDLE_MODE );
374  
375 }
376  
377 }
378  
379 sub schedule_next {
380 my ( $self, $kernel, $heap ) = @_[ OBJECT, KERNEL, HEAP ];
381  
382 # If there's something left in the schedule
383 if ( my $entry = $self->get_next_entry() ) {
384  
385 # Set an alarm to play it
386 my $alarm_offset = $entry->start_time() - $self->offset();
387 my $in_time = $alarm_offset - CORE::time();
388  
389 ( $in_time >= 0 )
390 or $self->logger()->logdie(
391 "Attempt to schedule '",
392 $entry->mrl(),
393 "' in the past ($in_time) at ",
394 scalar localtime $alarm_offset
395 );
396  
397 $self->logger()->info( "scheduling: ", $entry->mrl(), " at ",
398 scalar localtime($alarm_offset),
399 " in ", duration($in_time) );
400 $kernel->alarm( 'play_scheduled', $alarm_offset, $entry,
401 0 );
402  
403 } # End if there's something left
404  
405 }
406  
407 sub shutdown {
408 my ( $self, $kernel, $heap ) = @_[ OBJECT, KERNEL, HEAP ];
409  
410 # If we're supposed to quit
411 if ( $self->terminate_on_finish() ) {
412  
413 # Pull in the shingle
414 $kernel->alias_remove('Scheduler');
415  
416 # Terminate Player and Filler
417 $kernel->post( $heap->{player_session}, 'shutdown' );
418 $kernel->post( $heap->{filler_session}, 'shutdown' );
419  
420 # Stop watching for 'finished' events
421 $kernel->state('finished');
422  
423 delete $heap->{$_} foreach keys %$heap;
424  
425 $kernel->alarm_remove_all();
426  
427 return;
428  
429 } # End if we're supposed to quit
430  
431 # Otherwise we're supposed to put up a standby screen
432 else {
433  
434 # Put up the standby screen
435 warn "Putting up standby screen unimplemented...";
436  
437 } # End otherwise
438  
439 }
440  
441 1;
442  
443 =head1 NAME
444  
445 Video::PlaybackMachine::Scheduler - Plays movies at the appropriate times