Skip to content

Commit cd39d1d

Browse files
committed
Update multiple threads example
1 parent 07b72f4 commit cd39d1d

File tree

8 files changed

+1219
-91
lines changed

8 files changed

+1219
-91
lines changed

JavaScript/3-multithread/actors/mailer.js

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,27 @@ const auth = require('../config');
66

77
const FROM = 'nodeua.com@gmail.com';
88

9-
ActorSystem.register(class Mailer {
10-
constructor() {
11-
console.log('Start actor: Mailer');
12-
this.transport = nodemailer.createTransport({
13-
service: 'gmail', auth
14-
});
15-
}
9+
ActorSystem.register(
10+
class Mailer {
11+
constructor() {
12+
console.log('Start actor: Mailer');
13+
this.transport = nodemailer.createTransport({
14+
service: 'gmail',
15+
auth,
16+
});
17+
}
1618

17-
message({ to, subject, message }) {
18-
const mail = { from: FROM, to, subject, text: message };
19-
this.transport.sendMail(mail, (error, data) => {
20-
if (error) console.log(error);
21-
else console.log(`Email sent: ${data.response}`);
22-
});
23-
}
19+
message({ to, subject, message }) {
20+
const mail = { from: FROM, to, subject, text: message };
21+
this.transport.sendMail(mail, (error, data) => {
22+
if (error) console.log(error);
23+
else console.log(`Email sent: ${data.response}`);
24+
});
25+
}
2426

25-
exit() {
26-
this.transport.close();
27-
console.log('Stop actor: Mailer');
28-
}
29-
});
27+
exit() {
28+
this.transport.close();
29+
console.log('Stop actor: Mailer');
30+
}
31+
},
32+
);

JavaScript/3-multithread/actors/monitoring.js

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,39 @@ const http = require('node:http');
66
const URL = 'http://localhost:8000/';
77
const INTERVAL = 2000;
88

9-
ActorSystem.register(class Monitoring {
10-
constructor() {
11-
console.log('Start actor: Monitoring');
12-
this.prevSuccess = true;
13-
this.timer = setInterval(() => {
14-
this.attempt(URL);
15-
}, INTERVAL);
16-
}
9+
ActorSystem.register(
10+
class Monitoring {
11+
constructor() {
12+
console.log('Start actor: Monitoring');
13+
this.prevSuccess = true;
14+
this.timer = setInterval(() => {
15+
this.attempt(URL);
16+
}, INTERVAL);
17+
}
1718

18-
attempt(url) {
19-
http.get(url, (res) => {
20-
const success = res.statusCode === 200;
21-
this.notify({ url, success, status: res.statusCode });
22-
}).on('error', (error) => {
23-
this.notify({ url, success: false, status: error.message });
24-
});
25-
}
19+
attempt(url) {
20+
http
21+
.get(url, (res) => {
22+
const success = res.statusCode === 200;
23+
this.notify({ url, success, status: res.statusCode });
24+
})
25+
.on('error', (error) => {
26+
this.notify({ url, success: false, status: error.message });
27+
});
28+
}
2629

27-
notify({ url, success, status }) {
28-
if (this.prevSuccess !== success) {
29-
this.prevSuccess = success;
30-
ActorSystem.send('Renderer', { url, success, status });
30+
notify({ url, success, status }) {
31+
if (this.prevSuccess !== success) {
32+
this.prevSuccess = success;
33+
ActorSystem.send('Renderer', { url, success, status });
34+
}
3135
}
32-
}
3336

34-
message() {}
37+
message() {}
3538

36-
exit() {
37-
clearInterval(this.timer);
38-
console.log('Stop actor: Monitoring');
39-
}
40-
});
39+
exit() {
40+
clearInterval(this.timer);
41+
console.log('Stop actor: Monitoring');
42+
}
43+
},
44+
);

JavaScript/3-multithread/actors/renderer.js

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,24 @@
22

33
const ActorSystem = require('../system');
44

5-
ActorSystem.register(class Renderer {
6-
constructor() {
7-
console.log('Start actor: Renderer');
8-
}
5+
ActorSystem.register(
6+
class Renderer {
7+
constructor() {
8+
console.log('Start actor: Renderer');
9+
}
910

10-
message({ url, success, status }) {
11-
const to = 'nodeua.com@gmail.com';
12-
const msg = success ? 'is available again' : 'is not available';
13-
const date = new Date().toUTCString();
14-
const reason = (success ? 'Status code: ' : 'Error code: ') + status;
15-
const message = `Resource ${url} ${msg} (${date})\n${reason}`;
16-
const subject = 'Server Monitoring';
17-
ActorSystem.send('Mailer', { to, subject, message });
18-
}
11+
message({ url, success, status }) {
12+
const to = 'nodeua.com@gmail.com';
13+
const msg = success ? 'is available again' : 'is not available';
14+
const date = new Date().toUTCString();
15+
const reason = (success ? 'Status code: ' : 'Error code: ') + status;
16+
const message = `Resource ${url} ${msg} (${date})\n${reason}`;
17+
const subject = 'Server Monitoring';
18+
ActorSystem.send('Mailer', { to, subject, message });
19+
}
1920

20-
exit() {
21-
console.log('Stop actor: Renderer');
22-
}
23-
});
21+
exit() {
22+
console.log('Stop actor: Renderer');
23+
}
24+
},
25+
);

JavaScript/3-multithread/actors/root.js

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,22 @@
22

33
const ActorSystem = require('../system');
44

5-
ActorSystem.register(class Root {
6-
constructor() {
7-
console.log('Start actor: Root');
8-
ActorSystem.start('Monitoring');
9-
ActorSystem.start('Renderer');
10-
ActorSystem.start('Mailer', 3);
11-
}
5+
ActorSystem.register(
6+
class Root {
7+
constructor() {
8+
console.log('Start actor: Root');
9+
ActorSystem.start('Monitoring');
10+
ActorSystem.start('Renderer');
11+
ActorSystem.start('Mailer', 3);
12+
}
1213

13-
message() {}
14+
message() {}
1415

15-
exit() {
16-
ActorSystem.stop('Monitoring');
17-
ActorSystem.stop('Renderer');
18-
ActorSystem.stop('Mailer');
19-
console.log('Stop actor: Root');
20-
}
21-
});
16+
exit() {
17+
ActorSystem.stop('Monitoring');
18+
ActorSystem.stop('Renderer');
19+
ActorSystem.stop('Mailer');
20+
console.log('Stop actor: Root');
21+
}
22+
},
23+
);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
const init = require('eslint-config-metarhia');
4+
5+
init[0].rules['class-methods-use-this'] = 'off';
6+
7+
module.exports = init;

0 commit comments

Comments
 (0)