Twelve o ‘clock at night, in bed
Rich woman: Here’s 500 yuan for you. Thank you
Tadpole: No hard work, master
Rich woman: My husband is coming home. You can go now
Tadpole: Yes, master
Then the tadpole jumped down from the third floor villa
.
Tadpole is a programmer, but also a technician
Code by day, foot by night
Because of years of knocking code, so superb fingering
Always push the customer to the sun dragon
.
The rich woman is young and rich, but short of love
For being married to an 80-year-old husband
Little tadpole obedient live not sticky people
Won the heart of a rich woman
.
See you again
Rich woman: so boring, please me quickly
Tadpole: Master, LET me use my finger to type “Dijkstra shortest Path algorithm” on your leg
Rich woman: You’ve done that before
Tadpole: I’ll write javascript Advanced Programming on your back with my chest hair.
Rich woman: No
Tadpole: Then I will show you a ventriloquism with my tongue
Rich woman: Tongue? A room?
Tadpole: Don’t think too much about it. It’s a regular performance
Then the little tadpole performed a piece of tongue broken rock
The rich woman rejoiced, and the applause was prolonged
.
After the performance, the rich woman blushed
Rich woman: I like you
Tadpole: I like myself, too
Woman: Do you love me
Tadpole: I can’t fall for clients
Rich woman: How can I get your heart
Tadpole: kill me, pull out your heart, hee hee
Rich woman: Hee hee
.
Rich woman: By the way, come to my girls’ party next week
Tadpole: Won’t you take your husband
Rich woman: He’s 80 years old. He’s a disgrace
Tadpole: That will cost me more
Rich woman: How much
Tadpole: 200 yuan for two hours
Women: deal
.
Girlfriends party
8:00 p.m., fancy hotels, ostentatious displays of wealth
Bestie A: I bought A bag, only 100,000 yuan, so cheap
Bestie B: I packed three little milk dogs and let them fight the landlord all night
Bestie C: My husband has many small animals, such as BMWS, Land Rovers and Jaguars
Rich woman silently forward: I bought a siheyuan in Beijing
The other girls stopped talking
.
“The tadpole realized
This was no ordinary meal
But a feast to pack to force between girlfriends
Tadpole: What do you need me to do
Rich woman: I need you to beat their boy pet
.
After drinking three cruise
It’s time to show off your boy’s pet
Each girl brought her own boy pet
There are muscle men, high-class ducks, male models and so on
Muscle guy goes on stage with an eight-pack and a crotch the size of a punching bag
Male models go straight for long legs and ugly faces
The senior duck sticks out his tongue on the stage and says, “Slightly slightly…” The sound of
Boys and girls have different strengths
She won applause from her girlfriends
.
It’s the tadpole’s turn
A plaid shirt in shit-green
stupefied
The host ask: hot chicken, you want to show what specialty
Tadpole: Tongue broken rock
Host: Other than that
Tadpole: Chest hair broken rock
Host rage: I suspect you are to smash the field
Tadpole: Yeah, I’m just here to crash
The host called in 20 security guards
Ten seconds later the guards all fell to the ground convulsing
Tadpole chest hair covered in cryptic javascript code
.
Host: I’m sorry, Dad, I offended you just now
Host: Dad, please introduce yourself to the audience
Tadpole from the bag out of ten million dollars (rich woman sent)
Lit and thrown out of the 98th floor
Burning money flew in the air
The tadpole said, “I am a man of different colors who cheaters and plays with women’s feelings.”
All the women in the audience were screaming
He bled from his seven orifice because of excessive excitement
Boys and girls see their masters licking tadpoles like crazy
I feel green
Compere: dad you still have what gentle point of special skill show to everybody
Tadpole: I want to sing a song of Sun Yanzhi’s “green light” for all the male hot chicken present here
“
“Green light”
Love is like a green light
All over your head
I blame my sister-in-law for being too pretty
.
Don’t blame dad for being heartless
It’s your spicy chicken
Ah, green light, ah, green light
To illuminate you and me
“
Boys: Tadpole, you’ve gone too far
Tadpole: each according to his ability to be scum
Boys: What are you trying to do
Tadpole: I want to dance on your grave
Male pets explode and scratch their heads like crazy
He jumped off the 98th floor
The tadpole won the race
Became the number one boy pet
.
Hotel balcony, breeze blowing
Rich woman: Thank you for tonight
Tadpole: For the money, I should
Rich woman: I like you
Tadpole: I like myself, too
Woman: Do you love me
Tadpole: I can’t fall for clients
Rich woman: How can I get your heart
Tadpole: kill me, pull out your heart, hee hee
Rich woman: Hee hee
.
The gang bosses
Inside, pinch your feet
Rich woman: I’m dying
Tadpole: What’s wrong
Rich woman: My husband has a woman outside
Rich woman: he’s a gangster, trying to kill me, with my mistress
Tadpole gently caresses rich woman’s face: fool, before this, I will kill him
Little tadpole and rich woman embrace kiss together
Pull a thread between your mouths
The tadpole disappears into the night sky
.
The assassination of bosses
Twelve o ‘clock at night
Outside the hotel room of a mob boss
A small card was tucked in
It says “Miss Bao”
And a picture of a tadpole in a speedo
.
Mob bosses have weird quirks
Like a little milk dog with a hairy chest
The tadpole fulfilled all his fantasies about being a Suckling dog
.
Sure enough, the door opened
The tadpole stuck a thousand chest hairs into the big guy
Big Guy: You… Who are you
Tadpole: I’m your father
Big guy: Why did you kill me
Tadpole: Because I’m your father
Big man: Can we not mention dad
Tadpole: I had an affair with your wife
Gang leader, Cu, died at 80
.
For the assassination of a mob boss
On the way back that night
Little tadpole being hunted by 1,000 killers
I used up my last chest hair
The tadpole is outnumbered
She was dying in a pool of blood
As he was dying, the rich woman emerged from the killers
She smiled and stabbed the tadpole with a knife
I tore my heart out
The tadpole looked at her in horror
Rich woman: I finally got your heart, hee hee
(after)
Author: The tadpole in the first place
Wechat official account: Front-end Diaosi
Making: github.com/airuikun/bl…
Principle and Implementation of Hot Deployment of Modules on NodeJS
background
As you all know, if there is any code change in the back-end service started by NodeJS, the process must be restarted for the code to take effect.
When the NodeJS process is restarted, fathers will access the service, and there will be 502 bad Gateway for a short time. Fathers will not be happy
If you have a watch mechanism on your server
When the code on the server changes frequently, or frequently in a short period of time, it stays 502 bad Gateway
The so-called “restart for a while, keep restarting straight.”
Recently, when making relevant requirements for cloud compilation, frequent changes of service codes and frequent updates of code function modules occurred in a short period of time, and the updated codes took effect when the service could not be restarted.
This involves the concept of hot deployment, where newly deployed code takes effect without restarting the service.
Let me explain the principles and implementation of hot deployment to dads
Why code doesn’t work in real time
When loading a function module via require(‘xx/xx.js’), Node will cache the result of require(‘xx/xx.js’) in require.cache(‘xx/xx.js’)
When we call require(‘xx/xx.js’) multiple times, node does not reload and instead reads directly from the require.cache(‘xx/xx.js’)
So when dad changes the xx/xx.js file on the server, Node only reads the cache and does not load dad’s latest code
Source code address and use
In order to implement this hot deployment mechanism, it is necessary to look up information on the Internet and dig holes everywhere
The following code is refined, fully operational, hot deployment fundamentals that you can extend on your own: Smart-Node-reload
Note The latest version of Node 12 May cause an error. The require.cache has been modified and the problem has been reported to the authorities
Git Clone will run directly without installation
npm start
Copy the code
This is when hot deployment change listening is turned on
How do you see the effect
Dad, look at the /hots/hot.js file
const hot = 1
module.exports = hot
Copy the code
Change the first line of code to const hot = 111
const hot = 111
module.exports = hot
Copy the code
You can see that the terminal is listening for code changes, and then dynamically loading your latest code and getting the result of execution:
Hot deployment file: hot.js, result: {'hot.js': 111 }
Copy the code
The hot deployment service listens for the code changes, reloads the code, and the dads can get the latest code execution results in real time, all while running online without the Node process restarting
The source code parsing
LoadHandlers main function
const handlerMap = {};/ / cache
const hotsPath = path.join(__dirname, "hots");
// Load the file code and listen for changes in the contents of the specified folder
const loadHandlers = async() = > {// Iterate over all files in the specified folder
const files = await new Promise((resolve, reject) = > {
fs.readdir(hotsPath, (err, files) => {
if (err) {
reject(err);
} else{ resolve(files); }}); });// Initialize all files and cache the results of each file in the handlerMap variable
for (let f in files) {
handlerMap[files[f]] = await loadHandler(path.join(hotsPath, files[f]));
}
// Listen for changes in file contents in the specified folder
await watchHandlers();
};
Copy the code
LoadHandlers are the main function of the whole hot deployment service. We specify that the hoTS folder in the server root is used to listen for changes and hot deployment
Use fs.readdir to scan all files in the HOTS folder, loadHandler method to load and run each scanned file, and cache the result in handlerMap
File change listening is then turned on using the watchHandlers method
WatchHandlers listen for file changes
// Monitor file changes under the specified folder
const watchHandlers = async() = > {// It is recommended to use chokidar's NPM package instead of folder listening
fs.watch(hotsPath, { recursive: true }, async (eventType, filename) => {
// Get the absolute path to each file
// Require. Resolve. After the path has been concatenated, it will take the initiative to determine whether the file in this path exists
const targetFile = require.resolve(path.join(hotsPath, filename));
// When you load a module in require, the module's data will be cached in require.cache. The next time you load the same module, it will go directly to require.cache
// The first thing we do in a hot load deployment is clear the require.cache of the corresponding file
const cacheModule = require.cache[targetFile];
// Remove parent's references to the current module in require.cache, otherwise it will cause memory leaks
/ / the record once triggered by a single line of code "attack", "https://cnodejs.org/topic/5aaba2dc19b2e3db18959e63
/ / the line of the delete the require. Cache memory leak caused by murder, https://zhuanlan.zhihu.com/p/34702356
if (cacheModule.parent) {
cacheModule.parent.children.splice(cacheModule.parent.children.indexOf(cacheModule), 1);
}
// Clear the require.cache cache of the module corresponding to the specified path
require.cache[targetFile] = null;
// Reload the changed module file to achieve hot loading deployment effect and update the reloaded result to the handlerMap variable
const code = await loadHandler(targetFile)
handlerMap[filename] = code;
console.log("Hot deployment file:", filename, ", execution result:, handlerMap);
});
};
Copy the code
The watchHandlers function listens for changes to files in the specified folder, cleans and updates the cache.
Use fs.watch native function to listen for file changes under hoTS folder. When the file changes, calculate the absolute path targetFile of the file
Cache [targetFile] = null; require. Cache [targetFile] = null;
Cache [targetFile].parent is a reference to the parent level of the cache
if (cacheModule.parent) {
cacheModule.parent.children.splice(cacheModule.parent.children.indexOf(cacheModule), 1);
}
Copy the code
LoadHandler Loads the file
// Load the code for the specified file
const loadHandler = filename= > {
return new Promise((resolve, reject) = > {
fs.readFile(filename, (err, data) => {
if (err) {
resolve(null);
} else {
try {
// Use the Script method of the VM module to precompile the changed file code, check for syntax errors, and find out whether there are syntax errors and other errors in advance
new vm.Script(data);
} catch (e) {
// Syntax error, failed to compile
reject(e);
return;
}
// After the compile passes, re-require to load the latest code
resolve(require(filename)); }}); }); };Copy the code
The loadHandler function loads the specified file and verifies the syntax of the new file.
Read the contents of the file with fs.readfile
Use the node native VM module vm.Script method to precompile the changed file code, check syntax errors, and find out whether there are syntax errors and other errors in advance
If the check passes, use the resolve(require(filename)) method to load the file require again and automatically add the file to require.cache
The end:
That’s all for hot deployment, smart-Node-reload
This code is my minimalist code, easy for everyone to read and understand, interested partners can use this code to further expand
Well… After writing this article, it is already two o ‘clock in the morning. It is not easy to write this article. Please star, follow, like and follow me
Author: The tadpole in the first place
Wechat official account: Front-end Diaosi
Making: github.com/airuikun/bl…