prep for release
[catagits/Catalyst-Plugin-Scheduler.git] / lib / Catalyst / Plugin / Scheduler.pm
index 6a200f1..e7a3974 100644 (file)
@@ -7,12 +7,11 @@ use DateTime;
 use DateTime::Event::Cron;
 use DateTime::TimeZone;
 use File::stat;
-use NEXT;
 use Set::Scalar;
 use Storable qw/lock_store lock_retrieve/;
-use YAML;
+use MRO::Compat;
 
-our $VERSION = '0.06';
+our $VERSION = '0.10';
 
 __PACKAGE__->mk_classdata( '_events' => [] );
 __PACKAGE__->mk_accessors('_event_state');
@@ -59,7 +58,7 @@ sub schedule {
 sub dispatch {
     my $c = shift;
 
-    $c->NEXT::dispatch(@_);
+    $c->maybe::next::method();
 
     $c->_get_event_state();
 
@@ -158,19 +157,33 @@ sub setup {
     $c->config->{scheduler}->{state_file}  ||= $c->path_to('scheduler.state');
     $c->config->{scheduler}->{hosts_allow} ||= '127.0.0.1';
     $c->config->{scheduler}->{yaml_file}   ||= $c->path_to('scheduler.yml');
+    
+    # Always start with a clean state
+    if ( -e $c->config->{scheduler}->{state_file} ) {
+        $c->log->debug( 
+            'Scheduler: Removing old state file ' .
+            $c->config->{scheduler}->{state_file}
+        ) if $c->config->{scheduler}->{logging};
+        
+        unlink $c->config->{scheduler}->{state_file}
+            or Catalyst::Exception->throw(
+                message => 'Scheduler: Unable to remove old state file '
+                    . $c->config->{scheduler}->{state_file} . " ($!)"
+            );
+    }
 
-    $c->NEXT::setup(@_);
+    $c->maybe::next::method(@_);
 }
 
 sub dump_these {
     my $c = shift;
     
-    return ( $c->NEXT::dump_these(@_) ) unless @{ $c->_events };
-    
+    return ( $c->maybe::next::method(@_) ) unless @{ $c->_events };
+        
     # for debugging, we dump out a list of all events with their next
     # scheduled run time
     return ( 
-        $c->NEXT::dump_these(@_),
+        $c->maybe::next::method(@_),
         [ 'Scheduled Events', $c->scheduler_state ],
     );
 }
@@ -259,7 +272,20 @@ sub _check_yaml {
             # wipe out all current events and reload from YAML
             $c->_events( [] );
 
-            my $yaml = YAML::LoadFile( $c->config->{scheduler}->{yaml_file} );
+            my $file = $c->config->{scheduler}->{yaml_file};
+            my $yaml;
+
+            eval { require YAML::Syck; };
+            if( $@ ) {
+                require YAML;
+                $yaml = YAML::LoadFile( "$file" );
+            }
+            else {
+                open( my $fh, $file ) or die $!;
+                my $content = do { local $/; <$fh> };
+                close $fh;
+                $yaml = YAML::Syck::Load( $content );
+            }
             
             foreach my $event ( @{$yaml} ) {
                 $c->schedule( %{$event} );
@@ -618,7 +644,7 @@ configuration option L</hosts_allow>.
 
 As an alternative to using the schedule() method, you may define scheduled
 events in an external YAML file.  By default, the plugin looks for the
-existence of a file called C<schedule.yml> in your application's home
+existence of a file called C<scheduler.yml> in your application's home
 directory.  You can change the filename using the configuration option
 L</yaml_file>.
 
@@ -654,8 +680,8 @@ maintenance.  Plugin authors, B<be sure to inform your users> if you do this!
 Events should be registered from a plugin's C<setup> method.
 
     sub setup {
-        my $c = shift;
-        $c->NEXT::setup(@_);
+        my $c = shift;        
+        $c->maybe::next::method(@_);
         
         if ( $c->can('schedule') ) {
             $c->schedule(