kill node server after tests have run gitlab - javascript

I am trying to spin up a node server on my gitlab server. I have set up my gitlab-ci.yml file and it all seems to work. However, I want to kill the node server after the tests have finished running.
The relevant section of my gitlab-ci.yml file looks like this:
unit_tests:
stage: test
variables:
GIT_STRATEGY: clone
script:
- npm install
- npm run websocket-server
- npm test
after_script:
- //what should go here to kill the node server after the tests have run?

You can use pkill for that.
A simple usage method could be something like this:
after_script:
- pkill node
Read pkill(1)'s manual for more info:
https://linux.die.net/man/1/pkill

Related

How to terminate CircleCI after running tasks?

My test passes as good as expected but the process hangs on the running status! is it a side effect of the background process? how to solve this problem?
(I need to run nodeJs as a background process to testing my app)
my config file:
version: 2.1
orbs:
node: circleci/node#1.1.6
jobs:
build-and-run:
executor:
name: node/default
steps:
- checkout
- node/with-cache:
steps:
- run: npm install
- run:
name: initial run
command: npm run start-server-for-test && sleep 5
background: true
- run: npm run ci-test
workflows:
build-and-run:
jobs:
- build-and-run
This doesn't looks like a CircleCI error. Run this same commands in your machine and try if it works. Also you can run jobs with ssh enabled in CircleCI and login to the machine and execute the commands, in that way you could troubleshoot.

How to run node.js application from script?

I am trying to run my node.js application from a script I have written:
echo "Starting node application"
sudo node /home/pi/PPBot/bot.js
exit 0
I run the script like this: sudo /etc/init.d/botscript
The output when running the script is:
Start node application
sudo: node: command not found
I have also tried replacing node by /home/pi/.nvm/versions/node/v.8.11.3/bin/node but this resulted in the same output.
I have already installed NodeJS through NVM. Simply using the command node bot.js works from the command line. However as can be seen above it does not work through the script.
if you want to run a simple node js file then use the command
-> node filename
ex.: node server.js
if you want to run a node js file with nodemon then use the command
-> nodemon filename
ex.: nodemon server.js

Bot shuts down when putty window is closed

I created a discord bot and am now attempting to run it off an Ubuntu Machine.
I installed the folders of the bot and NodeJs, here is what I used to install NodeJS:
sudo apt-get install -y nodejs
Then I used cd to select the directory, and started my bot using node index.js
The bot started, however when I went to close the putty and keep it running on the VPS the bot shutdown. Here is what the directory looks like.
I think the problem is that when you start the app in the putty window, that process is linked to the window and gets terminated when that is closed.
To avoid that you can use a host service like screen, tmux, nohup, bg and so on...
If you want to know which is the best, try looking at this question from the askUbuntu Stack Exchange.
The key concept is that you open a new window using the tmux command (or screen, ...), then run your bot like you always do. When you want to leave but keep the process runing, you can detach the session with a key combination, that changes from service to service.
If you want to access that window again, you can run a command that will "restore" your session, like
tmux list-sessions
tmux attach-session -t 0
The NodeJS instance is terminated when putty is closed. You need something to keep the instance alive. Try:
PM2: http://pm2.keymetrics.io/
or,
Forever: https://github.com/foreverjs/forever#readme
Recommended though is to run the node instance as a service that can reboot on startup. Try looking at this:
https://stackoverflow.com/a/29042953/7739392
The shell runs in the foreground. This means any scripts you start there will end once you end your session. A simple solution would be to run your script in the background by adding the & after the call:
node index.js &
A better solution would be to create a service you can ask the service daemon to run for you. However, adding the & should get you what you want for now.
I recommend using one of these two node modules - ForeverJS or PM2. I'll show you how to quickly get started with ForeverJS but PM2 would be very similar.
You can easily install ForeverJS by typing the following in your terminal:
$ npm install forever -g
You may need to use SUDO depending on your user's privileges to get this working properly. It is NOT recommended to use it in production due to the security risks.
Once installed CD to your projects file directory and like you typed 'node index.js' you will do something similar with ForeverJS.
$ forever start index.js
Now when you exit the terminal your NodeJS application will remain as a running process.

Error installing node.js on google compute engine docker image

I am trying to setup a node.js app inside docker, using as host the google compute engine VM gci-stable-55-8872-71-0 (debian), from image project google-containers:
$ gcloud compute instances create myvm --image-project google-containers --image gci-stable-55-8872-71-0 --zone europe-west1-b --machine-type f1-micro --scopes compute-rw
then I try to get a docker container running:
$ sudo docker build -t forperfuse/test .
but I keep getting errors when installing node:
The command '/bin/sh -c npm install' returned a non-zero code: 1
all other dependencies install well but node and npm are not installing- I have tried several options but still cannot get it to work, can you please help? many thanks in advance...
I'm not sure about what is going on, looks like the run command in the dockerfile is aiming to a bash that has a weird header. If you can publish them we can try or...
You can use the bitnami docker image available in launcher for free and works like a charm.
https://console.cloud.google.com/launcher
And there search for the node.js image.

Express not listening on localhost:3000

So, we've built a basic express node website
Trying to run the app with DEBUG=express_example:* npm start
With node DEBUG=express_example:* npm start
Also, tried inside node runtime:
http://localhost:3000/ is not connecting
Where are we wrong?
You need to create a variable called DEBUG with set command.
There is not command like DEBUG, it is a name of variable, so please try to run your server with set (to create variable):
set DEBUG=express_example:* & npm start
Try
DEBUG='express_example:*' npm start
Your environment variable was not getting set properly. Note that you can have many different environment variables this way
TEST=foo DEBUG='bar' npm start

Categories