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


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

So for this I'm obviously going to have to change the service so that it can detect the version and compile whether it is 20131206 or 20181025/higher ... That should be an interesting task.

If anybody has already done this please let me know!

The issues reported so far in this thread rg 20131206 are still unresolved. :-(

On one system, Callback_Running is never called.

On another system, Callback_Running is called but Win32::Daemon::State() never returns SERVICE_RUNNING when queried by Callback_Running, even though it is set by Callback_Start. I have not been able to confirm that doubling the line mentioned earlier resolves this issue.

Thanks for all the help so far. This has been difficult to coordinate client screentime with Monk availability.

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

Replies are listed 'Best First'.
Re^6: Win32::Daemon service doesn't reach RUNNING state
by SwaJime (Scribe) on May 31, 2019 at 20:18 UTC
    Good news! We found the call to set the delay has a max somewhere between 5000 and 10000 on the affected systems. So the workaround, which is working:
    our $isleep = 15; # Number of seconds to sleep between iterations { my $counter = 0; sub Callback_Running { my($Event, $Context) = @_; if(SERVICE_RUNNING == Win32::Daemon::State()) { $counter += 5; print $fh $counter . "\n"; if ($counter >= $iSleep * 60) { print $fh "Checking in.\n"; # do stuff; $counter = 0; } $Context->{last_state} = SERVICE_RUNNING; Win32::Daemon::State( SERVICE_RUNNING ); } } }
    However, resume after pause still does not work
    C:\Users\john\test>net continue TestService . The Test Service (TestService) service failed to resume. More help is available by typing NET HELPMSG 3538.

    After pausing Repeated messages "The Test Service (TestService) service has reported an invalid current state 0." in event viewer. Continue does not work.

      Trying to implement the 2018 version of Daemon into my code.

      Basing on https://metacpan.org/pod/release/JDB/Win32-Daemon-20181025/lib/Win32/Daemon.pm

      However it seems SERVICE_STOPPED and SERVICE_RUNNING ate not defined ???

      Undefined subroutine &Win32::Daemon::SERVICE_STOPPED called at test-sv +c.pl line xxx.

        All issues resolved.

        Code now runs on 2013 and 2018 versions of Daemon.pm.

        I also got pause and resume to work! :-D

        #!/usr/bin/env perl use strict; use warnings; use File::Basename; use Cwd qw(abs_path getcwd); use Win32::Daemon; if ($Win32::Daemon::VERSION == 20181025) { # Constants were not properly exported in this version package Win32::Daemon; sub SERVICE_NOT_READY { return 0 }; sub SERVICE_STOPPED { return 1 }; sub SERVICE_START_PENDING { return 2 }; sub SERVICE_STOP_PENDING { return 3 }; sub SERVICE_RUNNING { return 4 }; sub SERVICE_CONTINUE_PENDING { return 5 }; sub SERVICE_PAUSE_PENDING { return 6 }; sub SERVICE_PAUSED { return 7 }; } package main; our $iSleep = 2; open(my $fh, ">>", "C:\\Users\\ru601501\\test\\test.log"); select($fh); $|=1; print "V: " . $Win32::Daemon::VERSION . "\n"; use Cwd qw(abs_path getcwd); use File::Spec::Functions; # Get command line argument - if none passed, use empty string my $opt = $ARGV[0] || ""; my $path = $ARGV[1]; print $fh "Running Test Service.\n"; print $fh "opt: $opt\n"; print $fh "path: $path\n" if $path; # Check command line argument if ($opt =~ /^(-i|--install)$/i) { install_service(); } elsif ($opt =~ /^(-r|--remove)$/i) { remove_service(); } else { if (!$path) { print $fh "This program is intended to be run as a service.\n" +; exit 1; } chdir $path; Win32::Daemon::RegisterCallbacks( { start => \&Callback_Start, running => \&Callback_Running, stop => \&Callback_Stop, pause => \&Callback_Pause, continue => \&Callback_Continue, } ); my %Context = ( last_state => SERVICE_STOPPED, start_time => time(), ); if ($Win32::Daemon::VERSION < 20180000) { Win32::Daemon::AcceptedControls(SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PAUSE_CONTINUE | SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_PARAMCHANGE | SERVICE_ACCEPT_NETBINDCHANGE); } # Setting more than 5 seconds causes some systems to never trigger + Callback_Running(). Win32::Daemon::StartService( \%Context, 5 * 1000); # 5 seconds if ($Win32::Daemon::VERSION < 20180000) { close STDERR; close STDOUT; } } { my $counter = 0; sub Callback_Running { my ($Event, $Context) = @_; if (SERVICE_RUNNING == Win32::Daemon::State()) { $counter += 5; # print $fh $counter . "\n"; if ($counter >= $iSleep * 60) { print $fh "Checking in.\n"; $counter = 0; } # 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 ); } } } sub Callback_Start { my($Event, $Context) = @_; print $fh "Starting\n"; $Context->{last_state} = SERVICE_RUNNING; Win32::Daemon::State( SERVICE_RUNNING ); } sub Callback_Pause { my($Event, $Context) = @_; print $fh "Paused.\n"; $Context->{last_state} = SERVICE_PAUSED; Win32::Daemon::State( SERVICE_PAUSED ); } sub Callback_Continue { my( $Event, $Context ) = @_; print $fh "Resumed running."; $Context->{last_state} = SERVICE_RUNNING; Win32::Daemon::State( SERVICE_RUNNING ); } sub Callback_Stop { my($Event, $Context) = @_; print $fh "Stopped.\n"; $Context->{last_state} = SERVICE_STOPPED; Win32::Daemon::State( SERVICE_STOPPED ); # We need to notify the Daemon that we want to stop callbacks and +the service. Win32::Daemon::StopService(); } sub install_service { my ($path, $parameters); my $dir = getcwd; # Get the program's full filename, break it down into constituent +parts my $fn = Win32::GetFullPathName($0); my ($cwd,$bn,$ext) = ( $fn =~ /^(.*\\)(.*)\.(.*)$/ ) [0..2] ; # Determine service's path to executable based on file extension if ($ext eq "pl") { # Source perl script - invoke perl interpreter $path = "\"$^X\""; # Parameters include extra @INC directories and perl script # @INC directories must not end in \ otherwise perl hangs my $inc = ($cwd =~ /^(.*?)[\\]?$/) [0]; $parameters = "-I " . "\"$inc\"" . " \"$fn\" \"myflag\" \"$dir +\""; } else { # Invalid file type? die "Can not install service for $fn, file extension $ext not supported."; } my $sServiceName = "TestService"; # Populate the service configuration hash # The hash is required by Win32::Daemon::CreateService my %srv_config = ( name => $sServiceName, display => "Test Service ($sServiceName)", path => $path, description => "For debugging.", parameters => $parameters, # service_type => SERVICE_WIN32_OWN_PROCESS, # start_type => SERVICE_AUTO_START, ); if ($Win32::Daemon::VERSION < 20180000) { $srv_config{service_type} = SERVICE_WIN32_OWN_PROCESS; $srv_config{start_type} = SERVICE_AUTO_START; } # Install the service if (Win32::Daemon::CreateService(\%srv_config)) { print $fh "Test Service has been installed.\n"; print $fh "Setting agent service to delayed start.\n"; print $fh "sc config $sServiceName start= delayed-auto\n"; print $fh `sc config $sServiceName start= delayed-auto` . "\n +"; } else { print $fh "Failed to install Test Service: " . Win32::FormatMe +ssage(Win32::Daemon::GetLastError()) . "\n"; } } sub remove_service { print $fh "Test Service is being removed.\n"; my $sServiceName = "TestService"; my $hostname = Win32::NodeName(); if (Win32::Daemon::DeleteService($sServiceName)) { print $fh "Test Service uninstalled successfully.\n"; } else { print $fh "Failed to uninstall Test Service.\n"; } }

        Hopefully I won't run into more issues.