-
Notifications
You must be signed in to change notification settings - Fork 111
/
index.js
49 lines (38 loc) · 1.56 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
const { getClient, constants } = require('./wrapper');
const { createNodes } = require('./setup');
const { electLeader } = require('./electleader');
const { createWorker } = require('./createworker');
const { listen } = require('./addlistener');
const { addTask } = require('./addtask');
const { verifyTheNodeExistsFeature } = require('./exists');
const logger = require('./logger');
const notifier = require('./notifier');
notifier.on('connect', (message) => logger.log('connect', message));
notifier.on('createNode', (message) => logger.log('createNode', message));
notifier.on('addTask', (message) => logger.log('addTask', message));
notifier.on('onChildren', (children) => {
children.forEach((child) => {
logger.log(`child id: ${child}`);
});
});
async function init() {
const client = getClient();
client.on('connect', async () => {
await createNodes(client, ['/workers', '/assign', '/tasks', '/status'], constants.ZOO_CONTAINER);
await createNodes(client, ['/myttl'], constants.ZOO_PERSISTENT_WITH_TTL, 5000);
notifier.on('leader', async () => {
await listen(client, '/workers');
await listen(client, '/assign');
notifier.on('createWorker', async () => {
await listen(client, '/tasks');
});
await createWorker(client);
await addTask(client, 'hello world');
});
await electLeader(client, '/master');
await verifyTheNodeExistsFeature(client);
});
}
if (require.main === module) {
init().catch(logger.error);
}