Changeset 2924


Ignore:
Timestamp:
08/21/10 22:56:15 (18 months ago)
Author:
karpet
Message:

add use_native_signals feature; release 0.05

Location:
Catalyst-Plugin-Alarm/trunk
Files:
1 added
8 edited

Legend:

Unmodified
Added
Removed
  • Catalyst-Plugin-Alarm/trunk/Alarm.pm

    r2920 r2924  
    1515# Sys::SigAction doesn't help on Win32 systems 
    1616# because Win32 doesn't use POSIX signals 
    17 our $WIN32 = 1; 
    18 unless ( $^O eq 'MSWin32' ) { 
    19     require Sys::SigAction; 
    20     $WIN32 = 0; 
     17our $USE_NATIVE_SIGNALS = 0; 
     18if ( $^O eq 'MSWin32' ) { 
     19    $USE_NATIVE_SIGNALS = 1; 
    2120} 
    2221 
     
    3231} 
    3332 
     33sub setup_finalize { 
     34    my $c   = shift; 
     35    my $ret = $c->next::method(@_); 
     36 
     37    my %conf = %{ $c->config->{alarm} }; 
     38 
     39    if ( $conf{use_native_signals} ) { 
     40        $USE_NATIVE_SIGNALS = 1; 
     41    } 
     42    else { 
     43        require Sys::SigAction;    # defer till runtime 
     44    } 
     45 
     46    return $ret; 
     47} 
     48 
     49# must call on every request 
    3450sub prepare { 
    3551    my $class = shift; 
     
    105121        }; 
    106122 
    107         if ($WIN32) { 
     123        if ($USE_NATIVE_SIGNALS) { 
    108124            $SIG{ALRM} = $alarm_handler; 
    109125        } 
     
    239255    eval { 
    240256        my $h = $SIG{ALRM}; 
    241         if ($WIN32) { 
     257        if ($USE_NATIVE_SIGNALS) { 
    242258            $SIG{ALRM} = $alarm_handler; 
    243259        } 
     
    288304        CORE::alarm($prev_alarm); 
    289305 
    290         if ($WIN32) { 
     306        if ($USE_NATIVE_SIGNALS) { 
    291307            $SIG{ALRM} = $h; 
    292308        } 
     
    352368 package MyApp; 
    353369 use Catalyst qw( Alarm ); 
    354  MyApp->config->{alarm} = { 
     370 MyApp->config( alarm => { 
    355371    timeout => 60, 
    356372    global  => 120, 
    357373    handler => sub { # do something if alarm sounds } 
    358     }; 
     374 }); 
    359375  
    360  sub default : Private 
    361  { 
     376 sub default : Private { 
    362377     my ($self,$c) = @_; 
    363      unless( $c->timeout('foo') ) 
    364      { 
     378     unless( $c->timeout('foo') ) { 
    365379        $c->stash->{error} = "Sorry to keep you waiting. There was a problem."; 
    366380        return; 
     
    368382 } 
    369383  
    370  sub foo : Private 
    371  { 
     384 sub foo : Private { 
    372385    my ($self,$c) = @_; 
    373386    sleep 61;   
     
    443456  __PACKAGE__->config( alarm => { 
    444457    handler => sub { 
    445         if (ref $_[1]) 
    446         { 
     458        if (ref $_[1]) { 
    447459            $_[0]->log->error(" .... local alarm went off!!"); 
    448460            $_[1]->[0] = 'some return value'; 
    449461            $_[0]->alarm->on(0);    # turn 'off' the alarm flag 
    450462        } 
    451         else 
    452         { 
     463        else { 
    453464            $_[0]->log->error(" .... global alarm went off"); 
    454465            $_[0]->alarm->on(1); 
     
    466477Example: 
    467478 
    468   __PACKAGE__->config( alarm => { 
    469          override => { 
    470             re=> qr{/ajax/},  
    471             timeout=> 3 
    472            } 
    473          }); 
     479 __PACKAGE__->config( alarm => { 
     480    override => { 
     481        re=> qr{/ajax/},  
     482        timeout=> 3 
     483    } 
     484 }); 
    474485  
    475486Will set the global timeout value to 3 if the request->path matches C</ajax>. 
     
    486497    forward => 1,  
    487498    timeout => 10  
    488    }); 
     499 }); 
    489500  
    490501Will automatically call timeout() with a default value of 10 seconds, wherever your 
     
    494505feature. 
    495506 
     507=item use_native_signals 
     508 
     509Default value is false. If set to a true value, Sys::SigAction will not be used 
     510and instead the built-in %SIG handlers will be used. This is necessary for the plugin 
     511to work under Win32 systems and in some cases with FCGI. 
    496512 
    497513=back 
     
    536552 
    537553 
     554=head2 setup_finalize 
     555 
     556Overridden internally. 
     557 
    538558=head2 prepare 
    539559 
     
    565585 
    566586 __PACKAGE__->config( alarm => { 
    567                              override => { 
    568                                 re      => qr{/foo/}, 
    569                                 timeout => 3  
    570                                 } 
    571                             } ); 
    572  sub foo : Global 
    573  { 
     587    override => { 
     588        re      => qr{/foo/}, 
     589        timeout => 3  
     590    } 
     591 }); 
     592  
     593 sub foo : Global { 
    574594   my ($self,$c) = @_; 
    575595   $c->alarm->off;      # negates the override in config 
     
    655675for more details. 
    656676 
    657 Win32 systems don't have alarm() or other signal handlers. That's not really the fault 
    658 of this module, but listed here in case you try using this on a Win32 system and it balks. 
    659 You might try setting the PERL_SIGNALS environment variable to C<unsafe> but that wouldn't 
    660 be very safe, now would it? 
     677Win32 systems don't have alarm() or other signal handlers, so B<use_native_signals> 
     678gets turned on if running under Win32. 
     679 
     680Some users report that Sys::SigAction does not play nicely with FCGI, 
     681so you can set the B<use_native_signals> to a true value to use the built-in 
     682%SIG handlers instead of Sys::SigAction. 
    661683 
    662684=head1 AUTHOR 
  • Catalyst-Plugin-Alarm/trunk/Changes

    r1977 r2924  
    1414      instead of NEXT. 
    1515 
    16  
     160.05  21 Aug 2010 
     17    - add 'use_native_signals' config option to avoid dependency 
     18      on Sys::SigAction (see https://rt.cpan.org/Ticket/Display.html?id=60566) 
  • Catalyst-Plugin-Alarm/trunk/MANIFEST

    r2921 r2924  
    55t/00-load.t 
    66t/01-alarm.t 
     7t/02-native-signals.t 
    78t/TestApp/Controller/Root.pm 
    89t/TestApp.pm 
  • Catalyst-Plugin-Alarm/trunk/Makefile.PL

    r1977 r2924  
    44 
    55WriteMakefile( 
    6     NAME              => 'Catalyst::Plugin::Alarm', 
    7     VERSION_FROM      => 'Alarm.pm', 
    8     PREREQ_PM         => { 
    9                      'Sys::SigAction' => 0.04,  
    10                      'Time::HiRes'    => 1.0, 
    11                      'Class::Accessor::Fast' => 0, 
    12                      'Catalyst::Runtime'     => 5.7, 
    13                      'MRO::Compat' => 0, 
     6    NAME         => 'Catalyst::Plugin::Alarm', 
     7    VERSION_FROM => 'Alarm.pm', 
     8    PREREQ_PM    => { 
     9        'Sys::SigAction'        => 0.11, 
     10        'Time::HiRes'           => 1.0, 
     11        'Class::Accessor::Fast' => 0, 
     12        'Catalyst::Runtime'     => 5.8, 
     13        'MRO::Compat'          => 0, 
    1414 
    15                      },  
    16     AUTHOR            => 'Peter Karman <pkarman@atomiclearning.com>', 
    17     dist              => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', }, 
    18     clean             => { FILES => 'Catalyst-Plugin-Alarm-*' }, 
     15    }, 
     16    AUTHOR => 'Peter Karman <karman@cpan.org>', 
     17    dist   => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', }, 
     18    clean => { FILES => 'Catalyst-Plugin-Alarm-*' }, 
    1919 
    2020); 
  • Catalyst-Plugin-Alarm/trunk/README

    r898 r2924  
    1 Catalyst::Plugin::Alarm version 0.01 
     1Catalyst::Plugin::Alarm 
    22================ 
    33 
    4 Implements Sys::SigAction alarms with Time::HiRes for Catalyst. 
     4Implements alarms with Time::HiRes for Catalyst. 
    55 
    66INSTALLATION 
     
    1717This module requires these other modules and libraries: 
    1818 
    19   Sys::SigAction 
     19  Sys::SigAction (optional) 
    2020  Time::HiRes 
    2121 
  • Catalyst-Plugin-Alarm/trunk/t/00-load.t

    r898 r2924  
    11use Test::More tests => 1; 
    2 BEGIN { use_ok('Alarm') }; 
     2BEGIN { use_ok('Alarm') } 
    33 
    4 diag( "Testing Catalyst::Plugin::Alarm $Catalyst::Plugin::Alarm::VERSION" ); 
    5 diag( "Testing Sys::SigAction $Sys::SigAction::VERSION" ); 
     4diag("Testing Catalyst::Plugin::Alarm $Catalyst::Plugin::Alarm::VERSION"); 
    65 
     6eval { 
     7    require Sys::SigAction; 
     8    diag("Testing Sys::SigAction $Sys::SigAction::VERSION"); 
     9}; 
  • Catalyst-Plugin-Alarm/trunk/t/01-alarm.t

    r2920 r2924  
    33use Catalyst::Test 'TestApp'; 
    44use Test::More tests => 7; 
     5 
     6diag("USE_NATIVE_SIGNALS = $Catalyst::Plugin::Alarm::USE_NATIVE_SIGNALS"); 
    57 
    68# some tests defined in the TestApp files 
  • Catalyst-Plugin-Alarm/trunk/t/TestApp.pm

    r2920 r2924  
    2020            } 
    2121        }, 
    22         global => 5 
     22        global             => 5, 
     23        use_native_signals => $ENV{USE_NATIVE_SIGNALS}, 
    2324    }, 
    2425); 
Note: See TracChangeset for help on using the changeset viewer.