Skip to content

A simple way to monitor for any changes in your Node.js application and magically restart the server


computer girl success

In most of my Node.js application servers, I add a simple section of code at the end which watches specific application files, and terminates the server if any are changed.  In tandem with PM2, this allows the code to restart automatically when either pushing a new version, or directly making changes to the code.

There are other applications like nodemon that provide similar functionality, however I feel that PM2 is a better overall process management system.  This capability was not available, so I just added it directly into my applications.

In my application, I perform a simple process.exit(), however you may prefer to raise a signal in your application so you can gracefully shutdown.

console.log('Starting filename watch');

function restartServer(event, filename) {
    console.log('event is: ' + event);
    if (filename) {
        console.log('filename provided: ' + filename);
    } else {
        console.log('filename not provided');
    }
    process.exit(0);
}

fs.watch('jobs_processor.js', restartServer);
fs.watch('lib/hercle_jobs/lib/hercle_jobs.js', restartServer);
fs.watch('lib/hercle_jobs_v2.js', restartServer);
fs.watch('lib/ims_utility.js', restartServer);

How to Rotate Logs using PM2 Process Manager for Node.js


girl reading service manual

Pm2 is a great utility for keeping your Node production applications running.  It  provides high-level process management, and consolidates all your logs into one common location.  In addition, you can view those logs in a real-time manner using the PM2 logs command.

I now use PM2 in all new Node installations, heavily depending on its ability to capture all Node output into easily organized logs.  One feature lacking is the built-in ability to rotate your log files.  This can create some pretty large files if you are not careful.  Because PM2 does not accept a signal to rotate the log, you need to use the copytruncate capability of logrotate.   Simply add the following in a file called PM2 in /etc/logrotate.d. You can change the frequency (daily, weekly, etc),  number of copies to keep (rotate 5), and you can use the compress option if you want the logs to be compressed when rotated.  I usually skip this step, I prefer to easily be able to access the files without having to uncompress first.

Let me know if you have any other handy tips for using PM2.

/root/.pm2/logs/*.log {
daily
rotate 5
missingok
notifempty
sharedscripts
copytruncate
dateext
}

 

Node.js – fs.open/fs.writeFile, mode, file permissions, umask, and how they interact


Girl Reading Book
While working with Node.js recently, I ran into a situation with the mode parameter used in several functions; fs.open, fs.appendFile, fs.writeFile, etc. When trying to create a new file and specifying a specific mode parameter to allow user and group writes, I found that the file would get created, however would have the wrong permissions.  After much experimenting, I realized that the Linux server’s umask was getting applied, and preventing the creation of a file with the permissions I wanted.

The work around turned out to be fairly simple, although it does create a slight security hole, and should only be used in cases where you can control access to the application.

I simply wrapped the file create calls with the following code :

oldmask = process.umask(newmask);
fs.open(path, flags, [mode], callback)
process.umask(oldmask);

This only needs be performed when the open (or other method) will create a new file.  Let me know if you have experienced a similar issue, and if you have found other solutions to resolve.

Simple DIY Lotus Elise Front License Plate Bracket


Lotus Elise License Plate Holder

Since buying my Lotus Elise in 2005, I have been pulled over three times for not having a license plate on the front of the car.  Fortunately, the friendly police officers always seemed to let me go with just a warning.  While I definitely don’t like the look of the license plate mounted up front, I really did not have one installed because there just is not a real  good bracket out there.

Recently I came across a company that utilized the stock tow-ring nut located within the grill to mount a license plate bracket.  It was a very sexy solution, and I got ready to plunk down my credit card.  However, when I saw the final shopping cart price of $100, I decided to take a different course.

I found that an 8MM x 60MM bolt with the appropriate nuts and washers did an excellent job of mounting a left over garage door bracket to the front end of the car.  I lined up the holes in the bracket with the license plate, and painlessly drilled a couple holes in the license plate to match up with the bracket holes.  A few sprays of black paint, and it was ready to bolt-up.  I think it turned out pretty well, and saved me a cool $100.

 

Lotus Elise License Plate HolderLotus Elise License Plate HolderLotus Elise License Plate Holder

How to beat the CEL – Custom Lotus Elise Cold Air Intake


IMG_3080
I have tried a number of different cold air intakes for my 2005 Lotus Elise, and while I loved the sound and performance, I have been plagued by the infamous P0171 code that accompanies the inevitable check engine light.

Finally I decided to take matters into my own hand a develop my own solution.  The conventional wisdom was that the aftermarket cold air intakes modified the intake pipe size, and MAF sensor location just enough to confuse the Elise’s fragile ECU.  This seemed to make sense, as I did have varying success with the aftermarket units, my last one was a simple pipe with a K&N filter clamped on the end.  However, the pipe had a slightly larger bore size than the stock air-box.

So, what was my solution?  It was simple, I purchased a used Exige air-box and carefully cut-out the enclosed air-pipe using a sharp sawzall and some pliers to clean up the ends.  Go slow and take your time.  Once you have it removed, find a K&N filter that will fit the end of the pipe, and clamp it down solid.  I think you will find that it will solve any CEL issues you may have had, you can say goodbye to P071 for good!

 

lotus elise air box

IMG_3083

Infiniti M37x FSM – Service Manual


Pretty girl fixing car

If you prefer to do your own vehicle maintenance and repair, then you need the service manual for your car.  This FSM is for the 2011 Infiniti M37x.

Infiniti 2011 M37x FSM

 

Forward

Accelerator Control System
Automatic Drive Positioner
Audio, Visual & Navigation
Body Control System
Brake System
Brake Control System
Body Repair
Cruise Control System
Charging System
Engine Cooling
Driver Assistance System
Defogger
Door
Driveline
Drive Mode System
Engine Control System
Engine Mechanical
Exhaust System
Exterior Lighting System
Exterior
Front Axle
Fuel System
Front Suspension
General Information
Glass & Window
Heater & Air Conditioning System
Heater & Air Conditioning Control System
Horn
Interior Lighting System
Interior
Instrument Panel
LAN System
Engine Lubrication
Maintenance
Mirrors
Meter, Warning Lamp & Indicator
Parking Brake System
Power Control System
Power Supply, Ground, & Circuit Elements
Power Window Control System
Power Outlet
Rear Axle
Roof
Rear Suspension
Seat Belt
Seat Belt Control System
Seat
Security Control System
SRS Airbag
SRS Airbag Control System
Steering System
Steering Control System
Starting System
Transaxle & Transmission
Ventilation System
Warning Chime System
Road Wheels and Tires
Wiper & Washer