1 00:00:01,300 --> 00:00:03,390 I'm taking a look at the listener test file. 2 00:00:03,390 --> 00:00:08,140 Back inside of our Nats test project you'll recall that inside of here we said that anytime that our 3 00:00:08,140 --> 00:00:13,330 process was about to be stopped either by an interrupt signal or a terminate signal we tried to close 4 00:00:13,330 --> 00:00:16,940 down our client gracefully so we would call stand out close. 5 00:00:16,990 --> 00:00:18,750 The client would then attempt to close. 6 00:00:18,790 --> 00:00:21,590 Once it did close it would he met the closed event. 7 00:00:21,610 --> 00:00:23,610 So we captured that closed event. 8 00:00:23,620 --> 00:00:26,170 We said OK the thing has been cleaned up successfully. 9 00:00:26,170 --> 00:00:29,080 And then we actually exited our process. 10 00:00:29,080 --> 00:00:34,780 So we're gonna do something very similar back inside of our ticketing project as well so let me first 11 00:00:34,780 --> 00:00:37,060 tell you one way that we could implement this. 12 00:00:37,060 --> 00:00:39,690 This is not the good way to do it it's not the right way. 13 00:00:39,700 --> 00:00:43,660 I just want to point out some issues and I'm going to show it to you because this might be something 14 00:00:43,690 --> 00:00:45,200 you would do on your own. 15 00:00:45,440 --> 00:00:47,480 I'm going to take stand out on closed right here. 16 00:00:47,500 --> 00:00:48,780 I'm going to copy it. 17 00:00:48,910 --> 00:00:51,340 Go back over to Nats wrapper. 18 00:00:51,520 --> 00:00:57,130 I'm gonna find the Connect function and then right after that I'm going to paste in that stand out on 19 00:00:57,130 --> 00:00:57,890 close. 20 00:00:57,940 --> 00:01:02,350 I do have to change stand to this dot underscore a client of course. 21 00:01:02,440 --> 00:01:03,820 I'm also gonna go back to listener. 22 00:01:03,820 --> 00:01:09,740 I'm gonna take the to process dot on statements at the bottom copy those look back over it and that's 23 00:01:09,740 --> 00:01:15,630 wrapper and paste them right underneath and then once again I'll update stand to this dot underscore 24 00:01:15,720 --> 00:01:21,060 client and I go to put a exclamation or ultimately you know we could just do this dot client be a better 25 00:01:21,060 --> 00:01:21,710 way of doing this. 26 00:01:23,420 --> 00:01:26,550 OK so this right here would definitely work. 27 00:01:26,840 --> 00:01:32,660 This would make sure that after we connect we then attempt to capture any closed event. 28 00:01:32,660 --> 00:01:37,310 And once we capture it we're going to make sure we do cuts logs say hey everything was shut down successfully 29 00:01:37,310 --> 00:01:38,430 and then exit. 30 00:01:38,450 --> 00:01:43,380 And we also set up two listeners on the process to make sure there's ever an interrupt or terminate. 31 00:01:43,470 --> 00:01:46,100 We are going to go ahead and close everything down. 32 00:01:46,130 --> 00:01:48,230 This would definitely work without a doubt. 33 00:01:48,350 --> 00:01:50,210 But we do not want to do this. 34 00:01:50,210 --> 00:01:55,880 The reason why is that we have some method inside of here that is buried inside of some class inside 35 00:01:55,880 --> 00:02:02,080 of our project that can possibly cause our entire process to exit entirely. 36 00:02:02,110 --> 00:02:07,390 We do not ever want to have some method inside of some hidden away file that is going to suddenly cause 37 00:02:07,390 --> 00:02:12,940 the entire program to exit definitely bad design right here as we might write this file. 38 00:02:12,990 --> 00:02:18,150 We might decide to say put it inside of our common module and then use it throughout all of our different 39 00:02:18,150 --> 00:02:19,490 services. 40 00:02:19,680 --> 00:02:23,720 We would then have some shared library that might cause our program to exit entirely. 41 00:02:23,730 --> 00:02:29,300 Can you imagine what that would be like if Mongoose behave the same if Mongoose could just suddenly 42 00:02:29,300 --> 00:02:32,120 arbitrarily decide to exit your entire program. 43 00:02:32,120 --> 00:02:34,640 That would probably not be super ideal. 44 00:02:34,670 --> 00:02:39,320 So rather than putting code like this hidden away inside of some method we only ever want to allow something 45 00:02:39,320 --> 00:02:46,830 to exit our entire program from inside of some more central location such as our index t s file so rather 46 00:02:46,830 --> 00:02:51,870 than trying to directly exits from inside of connect we're going to instead put together a little bit 47 00:02:51,870 --> 00:02:56,580 of functionality that's going to allow us to see that the client is attempting to close and then we'll 48 00:02:56,580 --> 00:03:00,740 do the actual exit command from inside of index start to yes. 49 00:03:00,860 --> 00:03:03,920 So again this would work but not ideal. 50 00:03:03,940 --> 00:03:05,170 So how are we going to fix this. 51 00:03:05,170 --> 00:03:08,320 Well essentially we're just going to take the same code where we're going to write it out inside of 52 00:03:08,320 --> 00:03:09,890 our index not test file. 53 00:03:10,060 --> 00:03:11,710 I'm going to cut all that right there. 54 00:03:13,870 --> 00:03:20,140 I'm then going to go back to index start to yes and then right after our connect statement I'm going 55 00:03:20,140 --> 00:03:25,360 to paste all that stuff in so we do have to repair a couple of references. 56 00:03:25,430 --> 00:03:33,860 I'm to make sure I update this to Nat's wrapper client and then I'll update this right here. 57 00:03:33,890 --> 00:03:41,460 Both these this clients both them should now be at Nats wrapper not clients like so OK. 58 00:03:41,490 --> 00:03:42,590 So this is much better. 59 00:03:42,600 --> 00:03:47,040 So now instead of a very central location inside of our project we can see that there is a condition 60 00:03:47,040 --> 00:03:50,320 under which we're going to exit our entire program. 61 00:03:50,400 --> 00:03:55,620 It would be really nice if we could somehow integrate this into our class because right now. 62 00:03:55,620 --> 00:04:00,780 Well you know this is some very repetitive logic right here that we're probably going to want to duplicate 63 00:04:00,780 --> 00:04:02,240 between our different services. 64 00:04:02,310 --> 00:04:07,760 But again it just would not be a great form to hide this way inside of some far away file. 65 00:04:07,830 --> 00:04:12,300 So this is now the big question here is how do we actually test this stuff and make sure it's working 66 00:04:12,330 --> 00:04:13,500 as expected. 67 00:04:13,500 --> 00:04:15,870 Well let me show you how we can test this very easily. 68 00:04:15,870 --> 00:04:20,790 The first thing we're going to do is go back over to our terminal and we are going to re start scaffold. 69 00:04:20,790 --> 00:04:24,720 Now it's really important that you restart scaffold because if you don't you're going to run into some 70 00:04:24,780 --> 00:04:26,820 really strange situation. 71 00:04:26,880 --> 00:04:29,460 So I'm going to kill scaffold. 72 00:04:29,620 --> 00:04:31,070 I'm gonna see everything deleted. 73 00:04:31,070 --> 00:04:33,110 I'm going to give those pods just a moment. 74 00:04:34,170 --> 00:04:35,060 To get cleaned up. 75 00:04:35,200 --> 00:04:36,930 So looks like they're all getting terminated. 76 00:04:37,330 --> 00:04:37,610 Yep. 77 00:04:37,640 --> 00:04:38,690 Good enough. 78 00:04:38,750 --> 00:04:40,430 I'll go ahead and then restart scaffold 79 00:04:43,960 --> 00:04:46,490 so it's going to take a second for everything to come back online 80 00:04:49,100 --> 00:04:51,180 while that is all booting up. 81 00:04:51,590 --> 00:04:53,320 There's my connected to that's right there by the way. 82 00:04:53,330 --> 00:04:57,980 There's my connect connected to mongo DV and listening on port three thousand cats everything is now 83 00:04:58,010 --> 00:04:59,770 refreshed all running. 84 00:04:59,810 --> 00:05:03,300 Let's now try to simulate scarf or sweep. 85 00:05:03,300 --> 00:05:08,410 We're gonna try to simulate gnats going down we're gonna try to just delete the Nats pod entirely. 86 00:05:08,410 --> 00:05:12,580 That is going to cause the client to emit its own close event. 87 00:05:12,580 --> 00:05:18,750 So when that thing gets closed we should then see the process exit entirely so I going to go back to 88 00:05:18,780 --> 00:05:19,420 my terminal. 89 00:05:19,470 --> 00:05:21,690 I to find a new terminal window. 90 00:05:21,860 --> 00:05:23,990 I'm gonna get a listing of all my pods. 91 00:05:23,990 --> 00:05:29,780 I'm gonna find a pod running that's and then do a cube TTL delete pod and then put in the name of that 92 00:05:29,780 --> 00:05:33,050 pod if I now go backwards. 93 00:05:33,050 --> 00:05:38,000 The primary scaffold window I will see eventually a console log of Nats connection closed. 94 00:05:38,000 --> 00:05:39,160 Perfect. 95 00:05:39,230 --> 00:05:44,150 And then a couple seconds after that for you it might take several seconds might take all that to 30 96 00:05:44,150 --> 00:05:45,350 seconds or a minute. 97 00:05:45,350 --> 00:05:52,740 You should eventually see the container started back up so we did successfully kill the process inside 98 00:05:52,740 --> 00:05:58,310 there by calling process exit so we exited the process behind the scenes. 99 00:05:58,340 --> 00:06:04,340 The pod running our container saw that the primary process of our container exited the pod then automatically 100 00:06:04,340 --> 00:06:07,410 decided to restart the container inside there. 101 00:06:07,460 --> 00:06:11,460 So it restarted the container and that's why we are seeing brand new output right here. 102 00:06:12,260 --> 00:06:17,380 From T.S. no def we can probably also confirm that the container inside that pod was restarted. 103 00:06:17,410 --> 00:06:24,670 If we do another K or cube Seitel get pods and now we'll see that our ticket's deployment right there 104 00:06:24,670 --> 00:06:29,990 has had one restart that restart is because we lost our connection to net's all right. 105 00:06:29,990 --> 00:06:32,470 So I'd say this is looking pretty good. 106 00:06:32,510 --> 00:06:36,920 We are going to exit our process entirely any time that we lose our connection to gnats. 107 00:06:36,920 --> 00:06:39,130 That is definitely something we want. 108 00:06:39,170 --> 00:06:43,350 We are also going to make sure that any time that we get this interrupt signal or terminate signal we're 109 00:06:43,350 --> 00:06:45,940 going to close down the client manually as well. 110 00:06:45,980 --> 00:06:46,960 This looks pretty great. 111 00:06:46,970 --> 00:06:48,080 Let's take a pause right here. 112 00:06:48,080 --> 00:06:52,250 We still also have to test and make sure that whenever we create a new ticket we published an event 113 00:06:52,250 --> 00:06:52,690 as well. 114 00:06:52,820 --> 00:06:54,710 So still a little bit of stuff for us to do.