-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.js
executable file
·161 lines (148 loc) · 4.33 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#! /usr/bin/env node
const { Command, Option } = require("commander");
const { name, version } = require(__dirname + "/package.json");
const chalk = require("chalk");
// Check version
const lastestVersion = require("latest-version");
const semver = require("semver");
const program = new Command();
program
.version(version)
.option(
"-n, --name [name]",
"connection name [My Connection]",
"My Connection"
)
.option(
"-t, --token [token]",
"api token (get yours at https://taskforce.sh)",
process.env.TASKFORCE_TOKEN
)
.option(
"-p, --port [port]",
"redis port [6379]",
process.env.REDIS_PORT || "6379"
)
.option("--tls [tls]", "Activate secured TLS connection to Redis")
.option(
"-h, --host [host]",
"redis host [localhost]",
process.env.REDIS_HOST || "localhost"
)
.option("-d, --database [db]", "redis database [0]", "0")
.option("--passwd [passwd]", "redis password", process.env.REDIS_PASSWD)
.option(
"--spasswd [spasswd]",
"redis sentinel password",
process.env.REDIS_SENTINEL_PASSWD
)
.option("-u, --uri [uri]", "redis uri", process.env.REDIS_URI)
.option("--team [team]", "specify team where to put the connection")
.option(
"-b, --backend [host]",
"backend domain [api.taskforce.sh]",
"wss://api.taskforce.sh"
)
.option(
"-s, --sentinels [host:port]",
"comma-separated list of sentinel host/port pairs",
process.env.REDIS_SENTINELS
)
.option(
"-m, --master [name]",
"name of master node used in sentinel configuration",
process.env.REDIS_MASTER
)
.option(
"--nodes <nodes>",
"comma-separated list of cluster nodes uris to connect to",
process.env.REDIS_NODES ? process.env.REDIS_NODES : undefined
)
.option("--queues <queues>", "comma-separated list of queues to monitor")
.addOption(
new Option(
"--queuesFile <queuesFile>",
"file with queues to monitor"
).conflicts("queues")
)
.parse(process.argv);
console.info(
chalk.blueBright(
"Taskforce Connector v" + version + " - (c) 2017-2024 Taskforce.sh Inc."
)
);
const options = program.opts();
lastestVersion(name).then(function (newestVersion) {
if (semver.gt(newestVersion, version)) {
console.error(
chalk.yellow(
"New version " +
newestVersion +
" of taskforce available, please upgrade with yarn global add taskforce-connector"
)
);
}
if (!options.token) {
console.error(
chalk.red(
`ERROR: A valid token is required, use either TASKFORCE_TOKEN env or pass it with -t (get token at https://taskforce.sh)`
)
);
process.exit(1);
}
const queueNames = options.queuesFile
? parseQueuesFile(options.queuesFile)
: options.queues
? parseQueues(options.queues)
: undefined;
const connection = {
port: options.port,
host: options.host,
password: options.passwd,
sentinelPassword: options.spasswd,
db: options.database,
uri: options.uri,
tls: options.tls
? {
rejectUnauthorized: false,
requestCert: true,
agent: false,
}
: void 0,
sentinels:
options.sentinels &&
options.sentinels.split(",").map((hostPort) => {
const [host, port] = hostPort.split(":");
return { host, port };
}),
name: options.master,
};
const { Socket } = require("./dist/socket");
Socket(options.name, options.backend, options.token, connection, {
team: options.team,
nodes: options.nodes ? options.nodes.split(",") : undefined,
queueNames,
});
});
// Catch uncaught exceptions and unhandled rejections
process.on("uncaughtException", function (err) {
console.error(err, "Uncaught exception");
});
process.on("unhandledRejection", (reason, promise) => {
console.error({ promise, reason }, "Unhandled Rejection at: Promise");
});
function parseQueuesFile(file) {
// Load the queues from the file. The file must be a list of queues separated by new lines
const fs = require("fs");
const path = require("path");
const queuesFile = path.resolve(file);
if (fs.existsSync(queuesFile)) {
return fs.readFileSync(queuesFile, "utf8").split("\n").filter(Boolean);
} else {
console.error(chalk.red(`ERROR: File ${queuesFile} does not exist`));
process.exit(1);
}
}
function parseQueues(queuesString) {
return queuesString.split(",");
}