This course has been updated! We now recommend you take the Full Stack for Front-End Engineers, v3 course.
Transcript from the "Process Manager" Lesson
[00:00:00]
>> Jem Young: But I still have a problem. I don't like that I have to run node.appjs every single time. What happens if I restart my server? What happens if I just kill it? What happens if my computer goes to sleep at this point and the shell exits, which means it'll kill every program that's running.
[00:00:14] We need some sort of manager so we're going to use a process manager. And a process manager is just a way of keeping your application up and running. It can handle errors, it handles restarts. Let's say you have a place in your code that throws a non handled exception.
[00:00:31] What does that do in Node? That'll just kill the program. So you need some sort of service that handles that and then restarts gracefully. And it's job is to keep the process up and running constantly and it does it in the background. So today we're gonna use, for this exercise we're gonna use pm2.
[00:00:49] You can also use a program called forever and forever is good, it's a perfectly fine process manager, pm2 is a bit more full featured. And it's a little bit cleaner to use. But feel free to use any process manager you want, but in this case we'll use pm2.
[00:01:04] So we're gonna us npm again. And because we're doing a global install, we're gonna have to use the sudo command. So sudo npm i -g for global pm2.
>> Jem Young: Yes?
>> Speaker 2: So rule of thumb I've been told is avoid installing global. This is an exception where-
>> Jem Young: Yes.
[00:01:30]
>> Speaker 2: What are the exceptions? When is it safe to use a global install?
>> Jem Young: When you need to run it globally from any directory. Because this isn't, let's say I had four or five other node applications running, I want pm2 to access all of that. So installing and locally wouldn't make as much sense because we didn't then we have to pipe everything into node modules, things like that.
[00:01:51] But that's an excellent point. You generally don't want to do things globally unless you have to. If you wanna run things globally but from the local directory you can use something called MPX. Which will look at first in your local and then move up. But in this case, we wanna run globally because its a process manager.
[00:02:06] Great question though, that is staying on top of things.
>> Jem Young: Awesome, that was a big install. Man, there's a lot to this thing, good times. Anyways, no one ever looks at the library being installed. If you asked me what are my issues with npm, and it's not npm, it's just package managers, is you don't know what's actually going on.
[00:02:37] You have no idea what any of these packages do. Again it's trust, the Internet's built on trust and I trust that this is working correctly. Tomorrow we're going to run a higher version of node, just because I like to use modern features. But we're using the baseline install that came with, because we installed from the apt repo, they're always behind, I don't know why.
[00:02:58] But today I didn't wanna go into how to change that, how to point that to a different version. But tomorrow we're gonna run probably node 10, or 12, or whatever the LTS version is. But for today we're okay.
>> Jem Young: So let's go ahead and start pm2. So pm2 start app.js.
[00:03:24]
>> Jem Young: Cool, and it's now running with id 0 because it's only thing running right now. What's nice is now, I can go do whatever I want and it's just running in the background. If it crashes it's going to do its absolute best to keep it up, just in case.
[00:03:40] But to make sure that it starts when the server restarts, because right now if the server restarts, we still have to go pm2 start app.js. We can modify the pm2 startup file so that it creates a daemon that will always start our application when we start the server.
[00:03:56] So we'll say pm2 startup.
>> Jem Young: Cool.
>> Jem Young: I don't think I need to set the startup script. I think it does it for me, but we'll check. So what I'm gonna do now, we get to version control, make sure I did that right. We may actually have to copy and paste this in.
[00:04:25] But I don't think I had to do that when I ran this through last time. But what I'm gonna do to test this out, is I'm gonna take down my server. The fastest way to find out if you did things correctly. And I'm gonna turn it off, yeah, turn off.
[00:04:42] It takes a second, cool. The server is off and let's turn it back on. Give it a minute, is it up and running yet? I just used just the off button.
>> Jem Young: Which, it's not terribly useful because you still, and Digital Ocean will tell you this, you're still getting billed.
[00:05:07] Just cuz you turn it off, doesn't mean you deallocated yourself, just means it's not running. An important distinction, cuz you might turn it off and think well, I won't need the server, they're still charging you. [LAUGH] So your mileage may vary. Okay is the server up? It might take a second.
[00:05:22] So pm2 status,
>> Jem Young: And we got the app running, so pm2 save.
>> Jem Young: Cool, so now it's saved. When the server restarts, pm2 will get up and running as it did before. This is actually a pretty big deal cuz you'll forget to do it and you will kill your server.
[00:05:44] Everything is working, check this out mom, and you're like, I don't see anything, you're a failure as a son and as a engineer. No, that doesn't matter, my mom loves me no matter what.
>> [LAUGH]
>> Jem Young: [LAUGH] That's way too much information.