Service Project Not Receiving Stop-MyService command

This forum can be browsed by the general public. Posting is limited to current SAPIEN license holders with active maintenance and does not offer a response time guarantee.
Forum rules
DO NOT POST LICENSE NUMBERS, ACTIVATION KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM.
Only the original author and our tech personnel can reply to a topic that is created in this forum. If you find a topic that relates to an issue you are having, please create a new topic and reference the other in your post.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 5 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
User avatar
vilation
Posts: 3
Last visit: Tue Jan 31, 2023 12:55 pm

Service Project Not Receiving Stop-MyService command

Post by vilation »

To help you better we need some information from you.

*** Please fill in the fields below. If you leave fields empty or specify 'latest' rather than the actual version your answer will be delayed as we will be forced to ask you for this information. ***

Product, version and build: PowerShell Studio 2018 v5.5.154
32 or 64 bit version of product: 64
Operating system: Windows Server 2012 R2
32 or 64 bit OS: 64

*** Please add details and screenshots as needed below. ***

I've created a service from the Service Project Template that generates several background jobs that will continuously run. When I issue the stop command to the service the Stop-command never seems to reach the service. I've set up commands that update a log file when the Stop-MyService command is issued and nothing makes its way into that function.

I've verified the Invoke-MyService and everything else seems to run well, but I cannot cleanly stop my service. Any help would be appreciated.


DO NOT POST SUBSCRIPTIONS, KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM
User avatar
brittneyr
Site Admin
Posts: 1655
Last visit: Thu Mar 28, 2024 3:14 pm
Answers: 39
Been upvoted: 30 times

Re: Service Project Not Receiving Stop-MyService command

Post by brittneyr »

If a service takes too long to stop, it will timeout and an error will be returned. Make sure all of your jobs are aware that the service is stopping and are able to stop within seconds after attempting to stop the service.
Brittney
SAPIEN Technologies, Inc.
User avatar
vilation
Posts: 3
Last visit: Tue Jan 31, 2023 12:55 pm

Re: Service Project Not Receiving Stop-MyService command

Post by vilation »

  1. function Stop-MyService
  2. {
  3.     "ACTIVITY,Stopping Service" >> $global:ConfigurationImport.confLogFilePath
  4.     $global:bRunService = $false # Signal main loop to exit
  5.     $CountDown = 30 # Maximum wait for loop to exit
  6.     while($global:bServiceRunning -and $Countdown -gt 0)
  7.     {
  8.         Start-Sleep -Seconds 1 # wait for your main loop to exit
  9.         $Countdown = $Countdown - 1
  10.     }
  11.     Try
  12.     {
  13.                 Get-Job $jobname | Stop-Job
  14.                 Get-EventSubscriber | Where {$_.SourceIdentifier -eq $jobname} | Unregister-Event
  15.                 Get-Job $jobname | Remove-Job
  16.         }
  17.     catch
  18.     {
  19.         $_.Exception.Message >> $global:ConfigurationImport.confLogFilePath
  20.     }
  21. }
That is the capture of my Stop-MyService function. It never seems to actually issue the stop-myservice command. I've added a capture for the $global:bRunService = $false flag on my jobs so that they stop themselves when it switches, but since the Stop-MyService function never seems to run I'm not sure how it's supposed to change that flag. The service stop will eventually time out and everything stops responding when I attempt to stop the service, but it hangs and never actually comes up with an error. I believe this is because it never actually enters the Stop-MyService function...

Am I missing something?
User avatar
brittneyr
Site Admin
Posts: 1655
Last visit: Thu Mar 28, 2024 3:14 pm
Answers: 39
Been upvoted: 30 times

Re: Service Project Not Receiving Stop-MyService command

Post by brittneyr »

Just for my clarification, it never writes to your log file? Is there any information that is returned in the event logs when you try to stop the service? Also, how are you packaging your service project?
The Stop-MyService function is called when your service is asked to stop by the operating system or when using Stop-Service. This is also the function that you would terminate running jobs and secondary runspaces; otherwise the service process may hang and not exit properly. As some modules create their own threads, jobs, runspaces, etc. it is also a good idea to use remove-module for any modules you load implicitly or explicitly.
Brittney
SAPIEN Technologies, Inc.
User avatar
mxtrinidad
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

Re: Service Project Not Receiving Stop-MyService command

Post by mxtrinidad »

It may be possible that due to the scope of variable in the function, when it goes from the Invoke-Service to the Stop-Service, the Get-Job might be null.

We need to see what you have in the Invoke-Service. Can you please share the code?

Thanks
User avatar
mxtrinidad
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

Re: Service Project Not Receiving Stop-MyService command

Post by mxtrinidad »

Using some basic assumption, I created a service job that output to a text file, found the Stop-MyService is not working keeping the service running when it should stop.

I'll have to talk to the developer which is on vacation until next week.

Thanks for notifying the issue!
User avatar
vilation
Posts: 3
Last visit: Tue Jan 31, 2023 12:55 pm

Re: Service Project Not Receiving Stop-MyService command

Post by vilation »

Good to know! I'll continue to use it as is until a fix can be implemented.

For those curious a basic version of my script is as follows:
  1. # Warning: Do not rename Start-MyService, Invoke-MyService and Stop-MyService functions
  2.  
  3.  
  4. function Start-MyService
  5. {
  6.     # Place one time startup code here.
  7.     # Initialize global variables and open connections if needed
  8.     "Starting Service" >> log.txt
  9.     Start-Job -Name JobName -ScriptBlock { Get-Content -Tail 0 -Wait | where $_ -Like "*WARNING*"}
  10.    
  11.     $global:bRunService = $true
  12.     $global:bServiceRunning = $false
  13.     $global:bServicePaused = $false;
  14. }
  15.  
  16. function Invoke-MyService
  17. {
  18.     $global:bServiceRunning = $true
  19.     while($global:bRunService) {
  20.         try
  21.         {
  22.             if($global:bServicePaused -eq $false) #Only act if service is not paused
  23.             {
  24.                 Receive-Job -Name 'JobName' >> C:\Logs\log.txt
  25.                 #Place code for your service here
  26.                 #e.g. $ProcessList = Get-Process solitaire -ErrorAction SilentlyContinue
  27.                
  28.                 # Use Write-Host or any other PowerShell output function to write to the System's application log
  29.             }          
  30.         }
  31.         catch
  32.         {
  33.             # Log exception in application log
  34.             Write-Host $_.Exception.Message
  35.         }      
  36.         # Adjust sleep timing to determine how often your service becomes active.
  37.         if($global:bServicePaused -eq $true)
  38.         {
  39.             Start-Sleep -Seconds 20 # if the service is paused we sleep longer between checks.
  40.         }
  41.         else
  42.         {
  43.             Start-Sleep –Seconds 10 # a lower number will make your service active more often and use more CPU cycles
  44.         }
  45.     }
  46.     $global:bServiceRunning = $false
  47. }
  48.  
  49. function Stop-MyService
  50. {
  51.     "Stopping Service" >> Log.txt
  52.     $global:bRunService = $false # Signal main loop to exit
  53.     $CountDown = 30 # Maximum wait for loop to exit
  54.     while($global:bServiceRunning -and $Countdown -gt 0)
  55.     {
  56.         Start-Sleep -Seconds 1 # wait for your main loop to exit
  57.         $Countdown = $Countdown - 1
  58.     }
  59.     Get-Job -Name 'JobName' | Stop-Job
  60.     Get-Job -Name 'JobName' | Remove-Job
  61.     # Place code to be executed on service stop here
  62.     # Close files and connections, terminate jobs and
  63.     # use remove-module to unload blocking modules
  64. }
  65.  
  66. function Pause-MyService
  67. {
  68.     # Service is being paused
  69.     # Save state
  70.     $global:bServicePaused = $true
  71.     # Note that the thread your PowerShell script is running on is not suspended on 'pause'.
  72.     # It is your responsibility in the service loop to pause processing until a 'continue' command is issued.
  73.     # It is recommended to sleep for longer periods between loop iterations when the service is paused
  74.     # in order to prevent excessive CPU usage by simply waiting and looping.
  75. }
  76.  
  77. function Continue-MyService
  78. {
  79.     # Service is being continued from a paused state
  80.     # Restore any saved states if needed
  81.     $global:bServicePaused = $false
  82. }
User avatar
Olga_B
Site Admin
Posts: 196
Last visit: Thu Mar 28, 2024 8:34 am

Re: Service Project Not Receiving Stop-MyService command

Post by Olga_B »

I used your script to create Windows Service and tested it on both Windows 10 and Windows Server 2012 R2 OSes. I was able to stop and start service without any issues.

If you can, please zip and load up the .msi file, so that we can attempt to recreate the issue:

https://www.sapien.com/support/upload
This topic is 5 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.