Basically, my requirement is to keep looking for new files in the folder, read the contents of the file and call the loopback API.
To achieve that I am trying to do something like below:
I have a remote method in loopback which needs to be called periodically i.e. after every 2 minutes. I read about the asynchronous boot scripts in the official loopback documentation here:
https://loopback.io/doc/en/lb2/Defining-boot-scripts#synchronous-and-asynchronous-boot-scripts
So far I have been able to write the following code:
module.exports = function(app, callback) {
setInterval(function() {
console.log('Hello world');
callback();
}, 120000);
};
But this throws an error below.
error: uncaughtException: listen EADDRINUSE :::443
Also, is there a way to call remote methods from the bootscripts ? Does loopback support cron/schedular or polling like functionality. Do I need to write a separate nodejs application which will call the API periodically.
Thanks
Your error states that your address(port number) if the server is already in use.
error: uncaughtException: listen EADDRINUSE :::443
You can try to listen on some other port number
app.listen(3000, function() {
console.log('listening on 3000')
});
Kill process running on same port
First, you would want to know which process is using port 3000
sudo lsof -i :3000
this will list all PID listening on this port, once you have the PID you can terminate it with the following:
kill -9 {PID}
Check if your code is not calling multiple listen on the same port
Let me know if this isn't resolving your problem
Related
I have a redis server which is working fine in my local but in ubuntu server is not working can someone gives the comment for installing redis in server
it is not working even with docker it is working only while i am running in local
const redis=require('redis');
var redisClient:any;
(async () => {
try {
redisClient = redis.createClient({ socket: { port: 6379 } });
await redisClient.connect();
// const redisClient = redis.createClient({
// port:"6379",
// host:'redis-service'
// });
redisClient.on('connect',()=>{
console.log('server connected to redis')
})
redisClient.on('ready',()=>{
console.log('Client Connect to redis and ready to use')
})
redisClient.on('error',(err:any)=>{
console.log(err)
})
redisClient.on('end',()=>{
console.log('Server disconnected from redis')
})
process.on('SIGINT',()=>{
redisClient.quit()
})
console.log('connected');
} catch (err) {
console.error(err)
}
})()
export{
redisClient
};
Make sure that Redis is not already running on the server. You can check this by running the command ps aux | grep redis. If Redis is running, you should see a line with the redis-server process.
Confirm that Redis is properly installed on your server by running redis-cli ping. If Redis is installed and running, it should return "PONG"
Check the Redis configuration file (redis.conf) and ensure that the IP and port settings match the settings on your local machine.
Make sure that the Redis server has the necessary permissions to access its data directory and that the correct ownership and permissions are set on its files.
Verify that the firewall rules on the server allow incoming connections on the Redis port (usually 6379)
Check for any compatibility issues between the version of Redis that you're running on your local machine and the version of Redis that's running on the Ubuntu server. If there's a version mismatch, it may be necessary to upgrade or downgrade one of the installations.
Try to run the Redis server with verbose output by running the command redis-server -v, this can give you some more information about the errors that are causing the server to fail.
Look at Redis log files, usually located in /var/log/redis for further clues about the cause of the problem.
I've been trying to get ReactPHP socket up and running for a bit now, once up, I can telnet to it on the specified port but I cannot use websocat or any js lib to connect via ws:// protocol. Any help would be appreciated.
$loop = React\EventLoop\Factory::create();
$socket = new React\Socket\Server('127.0.0.1:8000', $loop);
$socket->on('connection', function(ConnectionInterface $connection) use ($colors) {
$connection->write("Hello " . $connection->getRemoteAddress() . "!\n");
$connection->on('data', function($data) use ($connection){
$connection->write('received: ' . strtoupper($data));
});
});
echo "Listening on {$socket->getAddress()}\n";
$loop->run();
Server:
Listening on tcp://127.0.0.1:8000
Client:
websocat ws://localhost:8000
websocat: WebSocketError: HTTP failure
websocat: error running
For anyone struggle with this. React opens a low-level tcp socket which cannot be used with socket.io type ws:// connections. You will need to use a wrapper like Ratchet php.
I have a POST function in which I am trying to start VLC player using the child_process. I am using the latest Raspbian version.
router.post('/', function (req, res) {
let spawn = require('child_process').spawn;
let vlc = spawn('vlc');
vlc.stderr.on('data', function(data) {
console.log(data.toString());
});
vlc.on('exit', function(code){
console.log('Exit code: ' + code);
});
res.send({success: true})
});
After triggering the request, I get this message:
VLC is not supposed to be run as root. Sorry. If you need to use
real-time priorities and/or privileged TCP ports you can use
vlc-wrapper (make sure it is Set-UID root and cannot be run by
non-trusted users first).
Since VLC cannot be run as root, I added the UID argument to the vlc start script, and it now looks like this:
let vlc = spawn('vlc' ,{uid: 1000});
Where UID: 1000 is the ID of the user I always use.
After triggering the request, I get another message in the log:
[016f9960] main libvlc error: cannot open config file
(/root/.config/vlc/vlcrc): Permission denied
Home directory not accessible: Permission denied
[01762eb0] vlcpulse audio output error: PulseAudio server connection
failure: Connection refused
[0176bde8] dbus interface error: Failed to connect to the D-Bus
session daemon: Unable to autolaunch a dbus-daemon without a $DISPLAY
for X11
[0176bde8] main interface error: no suitable interface module
[016f9960] main libvlc error: interface "dbus,none" initialization
failed
[0176c7a8] main interface error: no suitable interface module
[016f9960] main libvlc error: interface "globalhotkeys,none"
initialization failed
[016f9960] main libvlc: Running vlc with the default interface. Use
'cvlc' to use vlc without interface.
error: XDG_RUNTIME_DIR not set in the environment.
[0176c7a8] skins2 interface error: cannot initialize OSFactory
[017614e0] main playlist: playlist is empty
[0176c7a8] [cli] lua interface: Listening on host "*console".
The player doesn't run. But if I run the same command via ssh, it will run. What could cause node from not running it?
The problem was that my server was running using
nodemon
I literally tried everything, and then when I was about to give up, I accidentally started server the normal way using node command. The VLC is starting without any errors. I didn't have to pass any user ID in arguments. It works fine just like this:
let spawn = require('child_process').spawn;
let vlc = spawn('vlc');
I'm really curious why nodemon could cause such a behaviour.
When using forever to run a node.js program as a daemon
i.e.
forever start myNodeTask
If the daemon (myNodeTask) decides it needs to exit, what is the correct way to do so?
If I just call process.exit() the program does terminate but it doesn't delete the forever log file, which leads me to believe that I need the program to exit in a more forever-friendly manner.
The node tasks I'm running are plain tcp servers that stream data to connected clients, not web servers.
The forever module always keeps the log files, even after a process has finished. There is no forever-friendly manner to delete those files.
But, you could use the forever-monitor module, which allow you to programatically use forever (from the docs):
var forever = require('forever-monitor'),
fs = require('fs');
var child = new (forever.Monitor)('your-filename.js', {
max: 3,
silent: true,
options: []
});
child.on('exit', function () {
console.log('your-filename.js has exited after 3 restarts');
// here you can delete your log file
fs.unlink('path_to_your_log_file', function (err) {
// do something amazing
});
});
child.start();
How to restart Node.js server from code? For example, if using expressjs framework,
app.get('/restart', function (req, res, next) {
//Code to restart a server
})
I want to restart server from app like this, without going to console etc. How to do it?
I use forever in order to start and monitoring application.
So the restart function like this:
app.get('/restart', function (req, res, next) {
process.exit(1);
});
After the shutdown of server, forever will restart service.
console:
Express server listening on port 3000 in development mode
error: Forever detected script exited with code: 1
error: Forever restarting script for 2 time
Express server listening on port 3000 in development mode
By combining npm restart and child_process.exec() it appears possible to restart the server programmatically. And since it allows for several scripts to be run for stopping, restarting, and starting this option would work even for multi-process servers or otherwise complex servers where simply calling process.exit() isn't viable.
You can mark a child-process as detached which I believe makes it survive past the parent's destruction. So if you use that, with a "sleep" command at the start of the command (in case concurrent running is an issue), it should work.
So presumably something like this (untested):
const newProcessInstance = spawn(process.argv[0], process.argv.slice(1), {
detached: true,
stdio: 'ignore', // not sure if this is needed
});