http://qs1969.pair.com?node_id=11100892


in reply to Re^8: Win32::Daemon service doesn't reach RUNNING state
in thread Win32::Daemon service doesn't reach RUNNING state

Thank you for keeping us updated and providing the code that works for you!

  • Comment on Re^9: Win32::Daemon service doesn't reach RUNNING state

Replies are listed 'Best First'.
Re^10: Win32::Daemon service doesn't reach RUNNING state
by SwaJime (Scribe) on Jun 03, 2019 at 18:38 UTC

    No problem!

    Received an update from Tomasz with a better way. Per Tomasz: "non-anonymous subroutine definitions in perl are being executed in compile time, which means the subs will be created even when the condition in your if is false. That means they can conflict with the future versions of Win32::Daemon."

    Better solution provided by Tomasz, which works for me:

    use Win32::Daemon; # restore AUTOLOAD sub which was erroneiously removed in 20181025 vers +ion of Win32::Daemon # note that newer versions of Win32::Daemon don't use AUTOLOAD at all if ($Win32::Daemon::VERSION == 20181025) { package Win32::Daemon; *Win32::Daemon::AUTOLOAD = sub { no strict; no warnings; # This AUTOLOAD is used to 'autoload' constants from the const +ant() # XS function. If a constant is not found then control is pas +sed # to the AUTOLOAD in AutoLoader. my( $Constant ) = $AUTOLOAD; my( $Result, $Value ); $Constant =~ s/.*:://; $Result = Constant( $Constant, $Value ); if( 0 == $Result ) { # The extension could not resolve the constant... $AutoLoader::AUTOLOAD = $AUTOLOAD; goto &AutoLoader::AUTOLOAD; return; } elsif( 1 == $Result ) { # $Result == 1 if the constant is valid but not defined # that is, the extension knows that the constant exists bu +t for # some wild reason it was not compiled with it. $pack = 0; ($pack,$file,$line) = caller; print "Your vendor has not defined 'Win32::Daemon' macro $ +constname, used in $file at line $line."; } elsif( 2 == $Result ) { # If $Result == 2 then we have a string value $Value = "'$Value'"; } # If $Result == 3 then we have a numeric value eval "sub $AUTOLOAD { return( $Value ); }"; goto &$AUTOLOAD; } }

      Ran into yet another issue. The request for 5 second intervals is not honored on the systems in question. Intervals can be 20 seconds or more apart. Here is a mod to make the execution happen closer to on time.

      our $startTime = time(); # put this line BEFORE use Win32::Daemon; ... ... ... our $t; sub Callback_Running { my ($Event, $Context) = @_; $t = $startTime if ! $t; if (SERVICE_RUNNING == Win32::Daemon::State()) { if (time() - $t >= $iSleep * 60) { print $fh "Checking in.\n"; # Do work here $t = time(); } # These two lines are needed for both versions $Context->{last_state} = SERVICE_RUNNING; Win32::Daemon::State( SERVICE_RUNNING ); } elsif (SERVICE_PAUSED == Win32::Daemon::State()) { # Without this, pause/continue fails $Context->{last_state} = SERVICE_PAUSED; Win32::Daemon::State( SERVICE_PAUSED ); } }

        Ran into yet another issue. The request for 5 second intervals is not honored on the systems in question. Intervals can be 20 seconds or more apart. Here is a mod to make the execution happen closer to on time.

        Thats the thing about sleep, you only get to ask what you want, doesn't mean system will give you that 100% of the time -- what you do get 80%-98% of the time is exactly what you asked for, in addition to not wasting CPU cycles by emulating "sleep"