@certik/skynet 0.10.3 → 0.10.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.eslintrc.js CHANGED
@@ -1,13 +1,13 @@
1
1
  module.exports = {
2
2
  env: {
3
3
  node: true,
4
- browser: true,
4
+ browser: false,
5
5
  commonjs: true,
6
- es2021: true
6
+ es2021: true,
7
7
  },
8
- extends: "eslint:recommended",
8
+ extends: ["eslint:recommended", "plugin:import/recommended"],
9
9
  parserOptions: {
10
- ecmaVersion: 12
10
+ ecmaVersion: 12,
11
11
  },
12
- rules: {}
12
+ rules: {},
13
13
  };
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.10.6
4
+
5
+ - Fixed producer check function
6
+
7
+ ## 0.10.5
8
+
9
+ - BREAKING Changed the signature of `postGenieMessage` function in `opsgenie` to make it more customizable
10
+
11
+ ## 0.10.4
12
+
13
+ - Fixed typo in coinmarketcap ids in `PROTOCOLS` in `const`
14
+
3
15
  ## 0.10.3
4
16
 
5
17
  - Added coinmarketcap ids to `PROTOCOLS` in `const`
package/app.js CHANGED
@@ -1,9 +1,15 @@
1
1
  const { EOL } = require("os");
2
2
  const fetch = require("node-fetch");
3
3
  const { SKYNET_API_PREFIX } = require("./const");
4
- const { createIndexerApp, createModeIndexerApp } = require("./indexer");
4
+ const {
5
+ createIndexerApp,
6
+ createModeIndexerApp,
7
+ getIndexerLatestId,
8
+ getIndexerValidatedId,
9
+ getIndexerState,
10
+ } = require("./indexer");
5
11
  const { createDeploy, createModeDeploy } = require("./deploy");
6
- const { createProducerApp, createConsumerApp } = require("./kafka");
12
+ const { createProducerApp, createConsumerApp, getProducerLatestId } = require("./kafka");
7
13
  const { startApiApp } = require("./api");
8
14
  const { createMonitor, ERROR_LEVEL } = require("./monitor");
9
15
  const { getBinaryName, detectBin, detectWorkingDirectory } = require("./cli");
@@ -326,7 +332,13 @@ function modeIndexer({ name, selector, state, build, validate, check, env = {},
326
332
  const { monitor } = createMonitor({
327
333
  binaryName: `${getBinaryName()} check`,
328
334
  name,
329
- type: "stateful",
335
+ getState: async (nam, selectorFlags) => {
336
+ const latestId = await getIndexerLatestId(nam, selectorFlags);
337
+ const validatedId = await getIndexerValidatedId(nam, selectorFlags);
338
+ const buildState = await getIndexerState(nam, selectorFlags);
339
+
340
+ return { latestId, validatedId, buildState };
341
+ },
330
342
  selector,
331
343
  mode: true,
332
344
  check: check.func,
@@ -428,7 +440,11 @@ function producer({ name, selector, produce, check, state, env = {}, region = "u
428
440
  const { monitor } = createMonitor({
429
441
  binaryName: `${getBinaryName()} check`,
430
442
  name,
431
- type: "stateful",
443
+ getState: async (nam, selectorFlags) => {
444
+ const latestId = await getProducerLatestId(nam, selectorFlags);
445
+
446
+ return { latestId };
447
+ },
432
448
  selector,
433
449
  check: check.func,
434
450
  maxRetry: check.maxRetry,
@@ -520,7 +536,6 @@ function consumer({ name, selector, consume, check, env = {}, region = "us-east-
520
536
  const { monitor } = createMonitor({
521
537
  binaryName: `${getBinaryName()} check`,
522
538
  name,
523
- type: "stateless",
524
539
  selector,
525
540
  check: check.func,
526
541
  maxRetry: check.maxRetry,
@@ -660,7 +675,6 @@ function api({ name, routes, serve, env = {}, region = "us-east-1" }) {
660
675
  const { monitor } = createMonitor({
661
676
  binaryName: `${getBinaryName()} check`,
662
677
  name,
663
- type: "stateless",
664
678
  selector,
665
679
  check: check.func,
666
680
  maxRetry: check.maxRetry,
package/deploy.js CHANGED
@@ -91,7 +91,8 @@ const genConfig = ({
91
91
  port "http" {
92
92
  static = ${service.port}
93
93
  }
94
- }` : ""
94
+ }`
95
+ : ""
95
96
  }
96
97
 
97
98
  task "log-shipper" {
@@ -437,6 +438,7 @@ function createModeDeploy({
437
438
  SKYNET_NOMAD_PRODUCTION_ADDR: null,
438
439
  SKYNET_SLACK_TOKEN: null,
439
440
  OPSGENIE_API_KEY: null,
441
+ OPSGENIE_END_POINT: null,
440
442
  },
441
443
  region,
442
444
  cmd: `${check.bin} ${args} ${production ? "--production" : ""}`,
@@ -596,6 +598,7 @@ function createDeploy({
596
598
  SKYNET_NOMAD_PRODUCTION_ADDR: null,
597
599
  SKYNET_SLACK_TOKEN: null,
598
600
  OPSGENIE_API_KEY: null,
601
+ OPSGENIE_END_POINT: null,
599
602
  },
600
603
  region,
601
604
  cmd: `${check.bin} ${args} ${production ? "--production" : ""}`,
package/kafka.js CHANGED
@@ -439,4 +439,5 @@ module.exports = {
439
439
  createConsumerApp,
440
440
  produceMessages,
441
441
  consumeMessages,
442
+ getProducerLatestId,
442
443
  };
package/monitor.js CHANGED
@@ -3,7 +3,6 @@ const fetch = require("node-fetch");
3
3
  const { getSelectorFlags, getSelectorDesc, toSelectorString } = require("./selector");
4
4
  const { getBinaryName } = require("./cli");
5
5
  const { exponentialRetry } = require("./availability");
6
- const { getIndexerLatestId, getIndexerValidatedId, getIndexerState } = require("./indexer");
7
6
  const { postGenieMessage } = require("./opsgenie");
8
7
  const { getJobName } = require("./deploy");
9
8
 
@@ -67,7 +66,7 @@ async function getMostRecentJobLaunch(name) {
67
66
  }
68
67
  }
69
68
 
70
- function createMonitor({ binaryName, name, type = "stateless", mode = false, selector = {}, check, maxRetry = 2 }) {
69
+ function createMonitor({ binaryName, name, getState = null, mode = false, selector = {}, check, maxRetry = 2 }) {
71
70
  function monitor() {
72
71
  if (!binaryName) {
73
72
  binaryName = getBinaryName();
@@ -109,18 +108,16 @@ ${
109
108
  const startTime = Date.now();
110
109
  console.log(`[MONITOR] starting check, ${toSelectorString(selectorFlags, ", ")}`);
111
110
 
112
- const state = {};
111
+ let checkState = {};
113
112
 
114
- if (type === "stateful") {
115
- state.latestId = await getIndexerLatestId(name, selectorFlags);
116
- state.validatedId = await getIndexerValidatedId(name, selectorFlags);
117
- state.buildState = await getIndexerState(name, selectorFlags);
113
+ if (getState) {
114
+ checkState = await getState(name, selectorFlags);
118
115
  }
119
116
 
120
117
  let result = await exponentialRetry(
121
118
  async () => {
122
119
  try {
123
- const errors = await check({ verbose, state, mode, ...selectorFlags });
120
+ const errors = await check({ verbose, state: checkState, mode, ...selectorFlags });
124
121
 
125
122
  if (!Array.isArray(errors)) {
126
123
  throw new Error(`check function must return array of error messages`);
@@ -153,12 +150,14 @@ ${
153
150
 
154
151
  // alert on opsgenie
155
152
  await postGenieMessage(
156
- `Failed Service Check: ${jobName}`,
157
- `<p><b>Service:</b><a href="${nomadAddr}/ui/jobs/${jobName}" target="_blank">${jobName}</a></p><p><b>Issues</b></p><ul>${sortErrors(
158
- result
159
- )
160
- .map((m) => `<li><b>${m.type}:</b> ${m.message}</li>`)
161
- .join("")}</ul>`,
153
+ {
154
+ message: `Failed Service Check: ${jobName}`,
155
+ description: `<p><b>Service:</b><a href="${nomadAddr}/ui/jobs/${jobName}" target="_blank">${jobName}</a></p><p><b>Issues</b></p><ul>${sortErrors(
156
+ result
157
+ )
158
+ .map((m) => `<li><b>${m.type}:</b> ${m.message}</li>`)
159
+ .join("")}</ul>`,
160
+ },
162
161
  verbose
163
162
  );
164
163
  } else {
package/opsgenie.js CHANGED
@@ -1,5 +1,5 @@
1
1
  const fetch = require("node-fetch");
2
- const hash = require("object-hash");
2
+ const md5 = require("md5");
3
3
 
4
4
  function getGenieKey() {
5
5
  return process.env.OPSGENIE_API_KEY;
@@ -15,32 +15,28 @@ function getGenieEndPoint() {
15
15
  return process.env.OPSGENIE_END_POINT;
16
16
  }
17
17
 
18
- async function postGenieMessage(msg, desc, verbose) {
18
+ async function postGenieMessage(body, verbose) {
19
19
  try {
20
20
  const genieKey = getGenieKey();
21
21
  const genieEndPoint = getGenieEndPoint();
22
22
 
23
- const body = {
24
- message: msg,
25
- description: desc
26
- };
27
-
28
23
  // Prevents duplicate alerts (See Opsgenie doc about alias)
29
- const bodyHash = hash(body);
30
- body.alias = bodyHash;
24
+ if (!body.alias) {
25
+ body.alias = md5(body.message);
26
+ }
31
27
 
32
28
  if (verbose) {
33
- console.log(`Making API call to Opsgenie ${msg} (${desc}):`, JSON.stringify(body, null, 2));
29
+ console.log(`Making API call to Opsgenie`, JSON.stringify(body, null, 2));
34
30
  }
35
31
 
36
32
  // Makes the call using fetch and ENV variables
37
33
  const response = await fetch(genieEndPoint, {
38
34
  method: "POST",
39
35
  headers: {
40
- "Content-Type": "application/json",
41
- "Authorization": "GenieKey " + genieKey
36
+ "Content-Type": "application/json",
37
+ Authorization: `GenieKey ${genieKey}`,
42
38
  },
43
- body: JSON.stringify(body)
39
+ body: JSON.stringify(body),
44
40
  });
45
41
 
46
42
  const result = await response.json();
@@ -48,8 +44,7 @@ async function postGenieMessage(msg, desc, verbose) {
48
44
  console.log(`Result of API call to Opsgenie... ${result}`);
49
45
  }
50
46
 
51
- return result
52
-
47
+ return result;
53
48
  } catch (error) {
54
49
  console.error("Failed to make opsgenie API call", error);
55
50
 
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@certik/skynet",
3
- "version": "0.10.3",
3
+ "version": "0.10.6",
4
4
  "description": "Skynet Shared JS library",
5
5
  "main": "index.js",
6
6
  "author": "CertiK Engineering",
7
7
  "scripts": {
8
- "lint": "eslint . test",
8
+ "lint": "eslint *.js test",
9
9
  "test": "ava"
10
10
  },
11
11
  "engines": {
@@ -18,9 +18,9 @@
18
18
  "execa": "^5.0.0",
19
19
  "express": "^4.18.1",
20
20
  "kafkajs": "^1.15.0",
21
+ "md5": "^2.3.0",
21
22
  "meow": "^7.0.1",
22
23
  "node-fetch": "^2.6.1",
23
- "object-hash": "^2.2.0",
24
24
  "snowflake-sdk": "^1.6.3",
25
25
  "web3": "^1.3.5",
26
26
  "which": "^2.0.2"
@@ -28,6 +28,7 @@
28
28
  "devDependencies": {
29
29
  "ava": "^3.13.0",
30
30
  "eslint": "^7.22.0",
31
+ "eslint-plugin-import": "^2.26.0",
31
32
  "sinon": "^11.1.2"
32
33
  },
33
34
  "license": "MIT"