简体   繁体   中英

Issue with installing node application as a service using Inno Setup and node-windows script

The service installs but will not start, either straight away or after system reboot, when running the following script from Inno Setup:

#define ....
#define NODE "node-v12.16.2-x64.msi"

...

[Files]
...

[Run]
Filename: "{sys}\msiexec.exe"; Parameters: "/passive /i ""{app}\{#NODE}""";
Filename: "{sys}\netsh.exe"; Parameters: "advfirewall firewall add rule name=""Node In"" program=""{pf64}\nodejs\node.exe"" dir=in action=allow enable=yes"; Flags: runhidden;
Filename: "{sys}\netsh.exe"; Parameters: "advfirewall firewall add rule name=""Node Out"" program=""{pf64}\nodejs\node.exe"" dir=out action=allow enable=yes"; Flags: runhidden;
Filename: "{pf64}\nodejs\node.exe"; Parameters: "{app}\validation-installer-node-windows.js";

validation-installer-node-windows.js:

var Service = require('node-windows').Service;
// Create a new service object
var svc = new Service({
     name:'receipt-validation-app1_7',
     description: 'Testing Team Receipt Validation.',
     script: 'C:\\xxxxx-receipt-validation-app1_v2\\app.js'
});

// Listen for the "install" event, which indicates the
// process is available as a service.

svc.on('install',function(){
     svc.start();
});

svc.install();

When the Inno Setup installer runs, it definitely installs the service and it is set to "Automatic" in startup type. However, when I restart my machine it does not auto-start.

However, when I run

node validation-installer-node-windows.js

the service installs (after I change the name so it doesn't clash) and autostarts straight away without reboot.

I have also tried running as admin. No special permissions were required when running validation-installer-node-windows.js from the terminal.


I attempted to trigger.js script using Pascal scripting and AfterInstall keyword as follows:

Source: "C:\...\validation-installer-node-windows.js"; DestDir: "{app}"; \
    Flags: ignoreversion; AfterInstall: RunNodeInstall()

[Run]
; Add Firewall Rules
Filename: "{sys}\netsh.exe"; Parameters: "advfirewall firewall add rule name=""Node In"" program=""{pf64}\nodejs\node.exe"" dir=in action=allow enable=yes"; Flags: runhidden;
Filename: "{sys}\netsh.exe"; Parameters: "advfirewall firewall add rule name=""Node Out"" program=""{pf64}\nodejs\node.exe"" dir=out action=allow enable=yes"; Flags: runhidden;
[Code]
procedure RunNodeInstall();
var
 ErrorCode: Integer;
begin
  if not Shellexec('', 'node', ExpandConstant('{app}\validation-installer-node-windows.js'),'',SW_HIDE,ewWaitUntilTerminated,ErrorCode) then
  begin
    MsgBox('Issue occured with installing application as a service!', mbInformation, MB_OK);
  end;
end;

Again, the script runs just fine and installs the service. However, like before it does not automatically set the status to "Running" and when hitting the nodejs endpoint it returns an "unable to connect" error which then sees the service status return to blank.


There are a series of log msgs, it starts with:

C:\woolworths-receipt-validation-app1_v2\app.js stopped running.

Then:

Restarted 1250 msecs after unexpected exit; attempts = 1

This iterates to 3 attempts and then the last log shows:

Child process [5616 - C:\Program Files\nodejs\node.exe --harmony C:\woolworths-receipt-validation-app1_v2\node_modules\node-windows\lib\wrapper.js --file C:\woolworths-receipt-validation-app1_v2\app.js --scriptoptions= --log "receipt-validation-app1_7 wrapper" --grow 0.25 --wait 1 --maxrestarts 3 --abortonerror n --stopparentfirst undefined] finished with 0

I have fixed it. The error is related to the way that Inno Setup creates a temporary directory and executes inside that directory. Basically I needed to have set the WorkingDir parameter, which as you can see above I did not set properly.

All credit to Martin Prikryl and Corey Butler for pointing me in the right direction.

The prototype for ShellExec is as follows:

function ShellExec(const Verb, Filename, Params, WorkingDir: String; const ShowCmd: Integer; const Wait: TExecWait; var ErrorCode: Integer): Boolean;

Foundhere

I have amended my code to the following:

procedure InstallNodeApp();
var
  ErrorCode: Integer;
var
  C, P, D: String;
begin
  C := 'node';
  P := 'validation-installer-node-windows.js';
  D := ExpandConstant('{app}');
  if not ShellExec('', C, P, D, SW_HIDE, ewWaitUntilTerminated, ErrorCode) then 
  begin
    ErrorCode:= -1;
    MsgBox('Issue occured with installing application as a service!',
      mbInformation, MB_OK);
  end;
end;

Can confirm it runs, installs the service, then works properly (connects to the app at localhost:8090 and returns a web app), and remains running.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM