@rpcbase/server 0.67.0 → 0.70.0

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/bin.js CHANGED
@@ -12,23 +12,20 @@ const run_agent = require("./cli/run_agent")
12
12
 
13
13
  let is_command = false
14
14
 
15
+
15
16
  const args = yargs(hideBin(process.argv))
16
- .command("start", "run server/infrastructure", () => {}, (argv) => {
17
+ .command("start", "run server/infrastructure", () => {}, (args) => {
17
18
  is_command = true
18
- start_server()
19
+ start_server(args)
19
20
  })
20
- .command("agent", "run the agent", () => {}, (argv) => {
21
+ .command("agent", "run the agent", () => {}, (args) => {
21
22
  is_command = true
22
23
  run_agent()
23
24
  })
24
- .command("build", "build server", () => {}, (argv) => {
25
+ .command("build", "build server", () => {}, (args) => {
25
26
  is_command = true
26
27
  build_server()
27
28
  })
28
- .command("ping", "ping", () => {}, (argv) => {
29
- is_command = true
30
- console.log("PING")
31
- })
32
29
  .option("verbose", {
33
30
  alias: "v",
34
31
  type: "boolean",
@@ -38,5 +35,12 @@ const args = yargs(hideBin(process.argv))
38
35
  .parse()
39
36
 
40
37
  if (!is_command) {
41
- console.log("default com22mand")
38
+ console.log("server default com22mand")
42
39
  }
40
+
41
+ // // add helpers to ensure proper process termination
42
+ // const exit_clean = () => {
43
+ // process.exit()
44
+ // }
45
+ // process.on("SIGINT", exit_clean) // ctrl-c
46
+ // process.on("SIGTERM", exit_clean) // kill
@@ -2,7 +2,6 @@
2
2
  const path = require("path")
3
3
  const fs = require("fs")
4
4
 
5
- const get_runtime = require("./get_runtime")
6
5
  const run_docker = require("./run_docker")
7
6
  const run_native = require("./run_native")
8
7
 
@@ -11,11 +10,10 @@ const build_server = async() => {
11
10
  // const build_schemas =
12
11
  // const infrastructure_dir = path.join(process.cwd(), "../infrastructure")
13
12
  //
14
- // // prefix
13
+ // prefix
15
14
  // const proj_parent_dir = path.join(infrastructure_dir, "../../")
16
15
  // const proj_prefix = path.basename(proj_parent_dir)
17
16
  //
18
- // const runtime = get_runtime()
19
17
  //
20
18
  // if (runtime === "native") {
21
19
  // run_native(infrastructure_dir, proj_prefix)
package/cli/run_docker.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /* @flow */
2
- const {spawn} = require("child_process")
2
+ const {execSync} = require("child_process")
3
3
  const path = require("path")
4
4
  const fs = require("fs")
5
5
 
@@ -7,7 +7,7 @@ const chalk = require("chalk")
7
7
 
8
8
  const is_production = "yes" === process.env.INFRASTRUCTURE_IS_PRODUCTION
9
9
 
10
- const run_docker = async(infrastructure_dir, proj_prefix, is_native) => {
10
+ const run_docker = async(infrastructure_dir, proj_prefix) => {
11
11
  // env
12
12
  const env_path = path.relative(infrastructure_dir, path.join(process.cwd(), "./.env"))
13
13
 
@@ -19,12 +19,7 @@ const run_docker = async(infrastructure_dir, proj_prefix, is_native) => {
19
19
  // compose files
20
20
  const compose_files = ["-f", "common.yml"]
21
21
  // TODO: which files are loaded and when should be documented
22
- if (!is_native && exists("native.yml")) {
23
- compose_files.push("-f", "native.yml")
24
- }
25
- if (!is_native && !is_production && exists("native.dev.yml")) {
26
- compose_files.push("-f", "native.dev.yml")
27
- }
22
+ // when running with native mode, do not load native infrastructure
28
23
  if (is_production && exists("prod.yml")) {
29
24
  compose_files.push("-f", "prod.yml")
30
25
  }
@@ -36,41 +31,14 @@ const run_docker = async(infrastructure_dir, proj_prefix, is_native) => {
36
31
  if (!is_production) opts.push("--abort-on-container-exit")
37
32
 
38
33
  const cmd = "docker-compose"
39
- // const cmd_args = `--env-file ${env_path} -p ${proj_prefix} ${compose_files.join(" ")} up ${opts.join(" ")}`
40
34
  const cmd_args = ["--env-file", env_path, "-p", proj_prefix, ...compose_files, "up", ...opts]
41
35
 
42
36
  console.log(cmd_args.join(" "))
43
37
 
44
- // trim extra docker spaces
45
- const fmt_line = (line) => line.replace(" |", "")
46
-
47
- const ps = spawn(cmd, cmd_args, {cwd: infrastructure_dir})
48
- ps.stdout.on("data", (data) => {
49
- const lines = data.toString().split("\n")
50
- lines.forEach((line) => {
51
- if (line.trim() === "") {
52
- return
53
- }
54
- process.stdout.write(`${chalk.cyan("docker")}: ${fmt_line(line)}\n`)
55
- })
56
- })
57
-
58
- ps.stderr.on("data", (data) => {
59
- const lines = data.toString().split("\n")
60
- lines.forEach((line) => {
61
- if (line.trim() === "") {
62
- return
63
- }
64
- process.stdout.write(`${chalk.cyan("docker")}: ${chalk.yellow(fmt_line(line))}\n`)
65
- })
66
- })
38
+ // WARNING: blocks the process
39
+ // this is the only way to have docker-compose shut down gracefully
40
+ execSync(`${cmd} ${cmd_args.join(" ")}`, {cwd: infrastructure_dir, stdio: "inherit"})
67
41
 
68
- // try {
69
- //
70
- // } catch (err) {
71
- // console.log("start_server::error\n", err)
72
- // process.exit(1)
73
- // }
74
42
  }
75
43
 
76
44
  module.exports = run_docker
package/cli/run_native.js CHANGED
@@ -1,46 +1,110 @@
1
1
  /* @flow */
2
2
  const {spawn} = require("child_process")
3
- // const dotenv = require("dotenv")
4
3
  const path = require("path")
5
4
  const fs = require("fs")
6
5
 
6
+ const dotenv = require("dotenv")
7
+ const mkdirp = require("mkdirp")
7
8
 
8
- const run_processes = [
9
- ["yarn", ["dev"]]
10
- // ["mongod", [
11
- // "--quiet",
12
- // "--port", "$DATABASE_PORT_0",
13
- // "--replSet", "rs0",
14
- // "--logpath", "/var/log/logs.txt",
15
- // ]],
16
- // ["mongod", [
17
- // "--quiet",
18
- // "--port", "$DATABASE_PORT_2",
19
- // "--replSet", "rs0",
20
- // "--logpath", "/var/log/logs.txt",
21
- // ]],
22
- ]
23
-
24
- // const get_env = () => {
25
- // const env_path = path.join(process.cwd(), ".env")
26
- // const env_buf = fs.readFileSync(env_path)
27
- // const env = dotenv.parse(env_buf)
28
- // return env
29
- // }
9
+ // TODO: verify mongod and redis-sever executables are present
10
+ // and have the right version
30
11
 
12
+ // "redis-server", "/usr/local/etc/redis/redis.conf", "--port", $WORKER_QUEUE_PORT
13
+
14
+ // load env
15
+ const get_env = () => {
16
+ const env_path = path.join(process.cwd(), "./.env")
17
+ const env_buf = fs.readFileSync(env_path)
18
+ const env = dotenv.parse(env_buf)
19
+ return env
20
+ }
21
+
22
+ const init_processes = () => {
23
+ const env = get_env()
24
+
25
+ const infrastructure_conf_dir = path.join(__dirname, "../infrastructure")
26
+
27
+ // create data dirs
28
+ const db_path = path.join(process.cwd(), "../infrastructure/data/database/db/")
29
+ const db_logs_path = path.join(process.cwd(), "../infrastructure/data/database/log/")
30
+
31
+ const session_store_dir = path.join(process.cwd(), "../infrastructure/data/session-store/data")
32
+
33
+ // TODO: add a redis cache
34
+ const processes = [
35
+ // web server
36
+ // TODO: do not use yarn dev server command
37
+ {
38
+ name: "server",
39
+ dirs: [],
40
+ cmd: ["yarn", ["dev"]]
41
+ },
42
+ // mongodb database
43
+ {
44
+ name: "database",
45
+ dirs: [
46
+ db_path, db_logs_path
47
+ ],
48
+ cmd: ["mongod", [
49
+ "--quiet",
50
+ "--dbpath", db_path,
51
+ "--port", env.DATABASE_PORT,
52
+ // "--bind_ip_all",
53
+ // "--logpath", "/var/log/logs.txt",
54
+ "--replSet", "rs0"
55
+ ]]
56
+ },
57
+ {
58
+ name: "session-store",
59
+ dirs: [
60
+ session_store_dir,
61
+ ],
62
+ cmd: ["redis-server", [
63
+ path.join(infrastructure_conf_dir, "./redis/redis.conf"),
64
+ "--dir", session_store_dir,
65
+ "--port", env.SESSION_STORE_PORT,
66
+ ]]
67
+ },
68
+ ]
69
+
70
+ // create missing directories
71
+ processes.forEach((item) => {
72
+ item.dirs.forEach((dir) => {
73
+ if (!fs.existsSync(dir)) {
74
+ mkdirp.sync(dir)
75
+ }
76
+ })
77
+ })
78
+
79
+ return processes
80
+ }
31
81
 
32
82
  // TODO: run each native process from list
33
83
  // TODO: handle process close
34
84
  const run_native = (infrastructure_dir, proj_prefix) => {
35
85
  console.log("running in NATIVE mode")
36
- // const env = get_env()
37
86
 
38
- const ps = spawn("yarn", ["dev"], {env: {...process.env, CONTAINER_MODE: "native"}})
39
- ps.stdout.on("data", (data) => {
40
- process.stdout.write(data.toString())
41
- })
87
+ const processes = init_processes()
42
88
 
89
+ const run_process = (item) => {
90
+ // console.log("start process", item)
91
+ const ps = spawn(item.cmd[0], item.cmd[1], {env: {...process.env, CONTAINER_MODE: "native"}})
43
92
 
93
+ ps.stdout.on("data", (data) => {
94
+ process.stdout.write(`${item.name}: ${data.toString()}`)
95
+ })
96
+
97
+ ps.stderr.on("data", (data) => {
98
+ process.stderr.write(`${item.name}: ${data.toString()}`)
99
+ })
100
+ }
101
+
102
+ const spawned = processes.map(run_process)
103
+
104
+ process.on("exit", () => {
105
+ console.log("EXIT from native1")
106
+ // ps.kill()
107
+ })
44
108
  }
45
109
 
46
110
  module.exports = run_native
@@ -2,30 +2,29 @@
2
2
  const path = require("path")
3
3
  const fs = require("fs")
4
4
 
5
- const get_runtime = require("./get_runtime")
6
5
  const run_docker = require("./run_docker")
7
6
  const run_native = require("./run_native")
8
-
7
+ const get_runtime = require("./get_runtime")
9
8
 
10
9
  // TODO: auto create network if necessary
11
10
  // docker network create local-network
12
11
 
13
- const start_server = async() => {
12
+ const start_server = async(args) => {
14
13
  const infrastructure_dir = path.join(process.cwd(), "../infrastructure")
15
14
 
15
+ const runtime = get_runtime()
16
+
16
17
  // prefix
17
18
  const proj_parent_dir = path.join(infrastructure_dir, "../../")
18
19
  const proj_prefix = path.basename(proj_parent_dir)
19
20
 
20
- const runtime = get_runtime()
21
-
22
- if (runtime === "native") {
23
- run_docker(infrastructure_dir, proj_prefix, true)
21
+ if (runtime === "docker") {
22
+ run_docker(infrastructure_dir, proj_prefix)
23
+ } else if (runtime === "native") {
24
24
  run_native(infrastructure_dir, proj_prefix)
25
25
  } else {
26
- run_docker(infrastructure_dir, proj_prefix)
26
+ throw new Error("unknown runtime")
27
27
  }
28
-
29
28
  }
30
29
 
31
30
  module.exports = start_server
@@ -0,0 +1,9 @@
1
+ systemLog:
2
+ destination: file
3
+ path: /opt/homebrew/var/log/mongodb/mongo.log
4
+ logAppend: true
5
+ storage:
6
+ dbPath: /opt/homebrew/var/mongodb
7
+ net:
8
+ bindIp: 127.0.0.1, ::1
9
+ ipv6: true