@@ -42,7 +42,6 @@ Name: "installservice"; Description: "Install as Windows Service (Recommended)";
4242[Files]
4343Source : " dist\*" ; DestDir : " {app} " ; Flags : ignoreversion recursesubdirs createallsubdirs
4444Source : " Logo\favicon.ico" ; DestDir : " {app} " ; Flags : ignoreversion
45- ; Note: Application will create its own configuration files
4645
4746[Dirs]
4847Name : " {app} \config" ; Permissions: everyone-full
@@ -53,18 +52,14 @@ Name: "{group}\{cm:UninstallProgram,{#MyAppName}}"; Filename: "{uninstallexe}"
5352Name : " {commondesktop}\{#MyAppName}" ; Filename : " {app} \{#MyAppExeName}" ; IconFilename: " {app} \favicon.ico" ; Tasks: desktopicon
5453
5554[Run]
56- ; For fresh installs only - create and start service
55+ ; Create service only if it doesn't exist (fresh install)
5756Filename : " {sys} \sc.exe" ; Parameters : " create " " {#MyServiceName}" " binPath= " " \" " {app} \{#MyAppExeName}\" " " " DisplayName= " " {#MyAppName}" " start= auto" ; Tasks: installservice; Flags : runhidden ; Check : not ServiceExists(' {#MyServiceName}' )
5857Filename : " {sys} \sc.exe" ; Parameters : " description " " {#MyServiceName}" " " " Cleanuparr download management service" " " ; Tasks: installservice; Flags : runhidden ; Check : not ServiceExists(' {#MyServiceName}' )
5958
60- ; For updates - stop service if running, wait for complete shutdown, then restart
61- Filename : " {sys} \sc.exe" ; Parameters : " stop " " {#MyServiceName}" " " ; Flags : runhidden ; Check : ServiceExists(' {#MyServiceName}' ) and IsServiceRunning(' {#MyServiceName}' ) and IsTaskSelected(' installservice' )
62- Filename : " {sys} \sc.exe" ; Parameters : " start " " {#MyServiceName}" " " ; Tasks: installservice; Flags : runhidden ; Check : ServiceExists(' {#MyServiceName}' ) and ServiceExistedBefore
59+ ; Start service (both fresh install and update)
60+ Filename : " {sys} \sc.exe" ; Parameters : " start " " {#MyServiceName}" " " ; Tasks: installservice; Flags : runhidden
6361
64- ; For fresh installs - start the newly created service
65- Filename : " {sys} \sc.exe" ; Parameters : " start " " {#MyServiceName}" " " ; Tasks: installservice; Flags : runhidden ; Check : not ServiceExistedBefore
66-
67- ; Open web interface (only if service is selected)
62+ ; Open web interface
6863Filename : " http://localhost:11011" ; Description : " Open Cleanuparr Web Interface" ; Flags : postinstall shellexec nowait ; Check : IsTaskSelected(' installservice' )
6964
7065; Run directly (if not installed as service)
@@ -76,15 +71,6 @@ Filename: "{sys}\timeout.exe"; Parameters: "/t 5"; Flags: runhidden; Check: Serv
7671Filename : " {sys} \sc.exe" ; Parameters : " delete " " {#MyServiceName}" " " ; Flags : runhidden ; Check : ServiceExists(' {#MyServiceName}' )
7772
7873[Code]
79- var
80- ServiceExistedBefore: Boolean;
81-
82- procedure CreateConfigDirs ;
83- begin
84- // Create config directory - application will create its own config files
85- ForceDirectories(ExpandConstant(' {app}\config' ));
86- end ;
87-
8874function ServiceExists (ServiceName: string): Boolean;
8975var
9076 ResultCode: Integer;
@@ -95,119 +81,90 @@ end;
9581function IsServiceRunning (ServiceName: string): Boolean;
9682var
9783 ResultCode: Integer;
98- Output: AnsiString ;
99- OutputFile: string ;
84+ TempFile: string ;
85+ StatusOutput: AnsiString ;
10086begin
10187 Result := False;
102- OutputFile := ExpandConstant(' {tmp}\service_status.txt' );
88+ TempFile := ExpandConstant(' {tmp}\service_status.txt' );
10389
104- // Query service status and capture output
105- if Exec(ExpandConstant(' {sys}\sc.exe' ), ' query "' + ServiceName + ' "' , ' ' , SW_HIDE, ewWaitUntilTerminated, ResultCode) and (ResultCode = 0 ) then
90+ // Use PowerShell to get service status
91+ if Exec(ExpandConstant(' {sys}\WindowsPowerShell\v1.0\powershell.exe' ),
92+ ' -Command "try { (Get-Service -Name '' ' + ServiceName + ' '' -ErrorAction Stop).Status } catch { '' NotFound'' }" > "' + TempFile + ' "' ,
93+ ' ' , SW_HIDE, ewWaitUntilTerminated, ResultCode) and (ResultCode = 0 ) then
10694 begin
107- // Use PowerShell to get service status more reliably
108- if Exec(ExpandConstant(' {sys}\WindowsPowerShell\v1.0\powershell.exe' ),
109- ' -Command "& {(Get-Service -Name '' ' + ServiceName + ' '' -ErrorAction SilentlyContinue).Status}"' ,
110- ' ' , SW_HIDE, ewWaitUntilTerminated, ResultCode) then
95+ if LoadStringFromFile(TempFile, StatusOutput) then
11196 begin
112- // If we can't determine status precisely, assume it might be running to be safe
113- Result := True;
97+ Result := (Pos(' Running' , StatusOutput) > 0 );
11498 end ;
99+ DeleteFile(TempFile);
115100 end ;
116101end ;
117102
118- function WaitForServiceStop (ServiceName: string): Boolean;
103+ function WaitForServiceStop (ServiceName: string; TimeoutSeconds: Integer ): Boolean;
119104var
120105 Counter: Integer;
121- ResultCode: Integer;
122- StatusOutput: AnsiString;
123- TempFile: string;
124106begin
125107 Result := True;
126108 Counter := 0 ;
127- TempFile := ExpandConstant(' {tmp}\service_check.txt' );
128109
129- // Wait up to 30 seconds for service to stop
130- while Counter < 30 do
110+ while Counter < TimeoutSeconds do
131111 begin
132- // Check service status using PowerShell for more reliable output
133- if Exec(ExpandConstant(' {sys}\WindowsPowerShell\v1.0\powershell.exe' ),
134- ' -Command "& {try { $s = Get-Service -Name '' ' + ServiceName + ' '' -ErrorAction Stop; $s.Status } catch { '' NotFound'' }}" > "' + TempFile + ' "' ,
135- ' ' , SW_HIDE, ewWaitUntilTerminated, ResultCode) then
136- begin
137- if LoadStringFromFile(TempFile, StatusOutput) then
138- begin
139- // If service is stopped or not found, we're good
140- if (Pos(' Stopped' , StatusOutput) > 0 ) or (Pos(' NotFound' , StatusOutput) > 0 ) then
141- begin
142- DeleteFile(TempFile);
143- Exit;
144- end ;
145- end ;
146- end ;
147-
112+ if not IsServiceRunning(ServiceName) then
113+ Exit;
148114 Sleep(1000 );
149115 Counter := Counter + 1 ;
150116 end ;
151117
152- // Cleanup temp file
153- DeleteFile(TempFile);
154-
155- // If we get here, service didn't stop in time
156- if Counter >= 30 then
157- begin
158- MsgBox(' Warning: Service took longer than expected to stop. Installation will continue but the service may need to be restarted manually.' ,
159- mbInformation, MB_OK);
160- Result := False;
161- end ;
118+ Result := False;
162119end ;
163120
164121function InitializeSetup (): Boolean;
165122var
166123 ResultCode: Integer;
167124begin
168- // Remember if service existed before installation
169- ServiceExistedBefore := ServiceExists(' {#MyServiceName}' );
125+ Result := True;
170126
171- // Only stop service if it exists and is running
172- if ServiceExistedBefore and IsServiceRunning(' {#MyServiceName}' ) then
127+ // If service exists and is running, stop it for the update
128+ if ServiceExists( ' {#MyServiceName} ' ) and IsServiceRunning(' {#MyServiceName}' ) then
173129 begin
174130 if MsgBox(' Cleanuparr service is currently running and needs to be stopped for the installation. Continue?' ,
175131 mbConfirmation, MB_YESNO) = IDYES then
176132 begin
177133 Exec(ExpandConstant(' {sys}\sc.exe' ), ' stop "{#MyServiceName}"' , ' ' , SW_HIDE, ewWaitUntilTerminated, ResultCode);
178- if not WaitForServiceStop(' {#MyServiceName}' ) then
134+
135+ if not WaitForServiceStop(' {#MyServiceName}' , 30 ) then
179136 begin
180- // Service didn't stop properly, but continue anyway
181- Log( ' Warning: Service did not stop cleanly, continuing with installation ' );
137+ MsgBox( ' Warning: Service took longer than expected to stop. Installation will continue but you may need to restart the service manually. ' ,
138+ mbInformation, MB_OK );
182139 end ;
183140 end
184141 else
185142 begin
186143 Result := False;
187- Exit;
188144 end ;
189145 end ;
190-
191- Result := True;
192146end ;
193147
194148function InitializeUninstall (): Boolean;
195149var
196150 ResultCode: Integer;
197151begin
152+ Result := True;
153+
198154 if ServiceExists(' {#MyServiceName}' ) then
199155 begin
200156 if MsgBox(' Cleanuparr service will be stopped and removed. Continue with uninstallation?' ,
201157 mbConfirmation, MB_YESNO) = IDYES then
202158 begin
203- Exec(ExpandConstant(' {sys}\sc.exe' ), ' stop "{#MyServiceName}"' , ' ' , SW_HIDE, ewWaitUntilTerminated, ResultCode);
204- WaitForServiceStop(' {#MyServiceName}' );
159+ if IsServiceRunning(' {#MyServiceName}' ) then
160+ begin
161+ Exec(ExpandConstant(' {sys}\sc.exe' ), ' stop "{#MyServiceName}"' , ' ' , SW_HIDE, ewWaitUntilTerminated, ResultCode);
162+ WaitForServiceStop(' {#MyServiceName}' , 30 );
163+ end ;
205164 end
206165 else
207166 begin
208167 Result := False;
209- Exit;
210168 end ;
211169 end ;
212- Result := True;
213170end ;
0 commit comments