index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // const shell = require('shelljs')
  2. const proc = require('child_process')
  3. const path = require('path')
  4. const fs = require('fs')
  5. var PG_CTL_EXE = "pg_ctl"
  6. var PG_INIT_EXE = "initdb"
  7. if(process.platform == 'win32'){
  8. PG_CTL_EXE = "pg_ctl.exe"
  9. PG_INIT_EXE = "initdb.exe"
  10. }
  11. // 获取EXE执行当前目录
  12. var OWN_DIR = process.env.PORTABLE_EXECUTABLE_DIR || process.env.OWD || process.cwd() || __dirname
  13. // 设置PG二进制程序目录
  14. var CWD = path.join(OWN_DIR,"node_modules/@embedded-postgres/linux-x64/native/bin/")
  15. if(process.platform == 'win32'){
  16. CWD = path.join(OWN_DIR,"node_modules/@embedded-postgres/windows-x64/native/bin/")
  17. }
  18. // 可执行文件
  19. var pgCtlExe = path.join(CWD,PG_CTL_EXE)
  20. var pgInitExe = path.join(CWD,PG_INIT_EXE)
  21. var DBDataDir
  22. /**
  23. *
  24. * @param {*} dbconfig
  25. * @desc
  26. * npm i -D @embedded-postgres/linux-x64 -f
  27. * npm i -D @embedded-postgres/windows-x64 -f
  28. * cp -rf node_modules/@embedded-postgres/linux-x64/native/* bin/pg/bin/
  29. * cp -rf node_modules/@embedded-postgres/windows-x64/native/* bin/pg/bin/
  30. */
  31. async function PostgreSQLKeep(dbconfig){
  32. DBDataDir = path.join(OWN_DIR,dbconfig.dbpath)
  33. console.log("PostgreSQLKeep",dbconfig)
  34. // pgStop(dbconfig)
  35. let start = async ()=>{
  36. await InitAndStartDB(dbconfig)
  37. if(pgStatus(dbconfig)!="running"){
  38. console.log("DB Service Relaunching...")
  39. await wait(1000);
  40. await start();
  41. }
  42. }
  43. await start();
  44. return
  45. }
  46. function wait(delay){
  47. return new Promise((resolve)=>{
  48. setTimeout(() => {
  49. resolve(true)
  50. }, delay);
  51. })
  52. }
  53. async function InitAndStartDB(dbconfig){
  54. let status = pgStatus(dbconfig)
  55. console.log("status",status)
  56. if(status=="noinit"){
  57. let initRes = ""
  58. // ./initdb.exe -D "C:\web3\psqlite\data\db\minerdb" -U postgres -A trust
  59. try{initRes = proc.execSync(`${pgInitExe} -D "${DBDataDir}" -U ${dbconfig.username} -A trust`)}catch(err1){}
  60. console.log("initRes",initRes)
  61. await pgRestart(dbconfig)
  62. return
  63. }
  64. if(status=="nostart"){
  65. await pgRestart(dbconfig)
  66. return
  67. }
  68. return
  69. }
  70. function pgRestart(dbconfig){
  71. console.log("pgRestart")
  72. let startRes = ""
  73. let cmd = `${pgCtlExe}`
  74. let args = [`restart`,`-D`, `"${DBDataDir}"`, `-U`, `${dbconfig.username}`, `-o`, `"-p ${dbconfig.port}"`]
  75. console.log(cmd,args.join(" "))
  76. return new Promise(resolve=>{
  77. let pgstart
  78. let options = {
  79. detached:true,
  80. shell:true,
  81. // stdio:"ignore",
  82. windowsHide:true
  83. }
  84. if(process.platform == 'win32'){ // start 从窗口打开,脱离当前程序 /min 新窗口默认最小化
  85. // pgstart = proc.spawn("cmd",["/c","",cmd,...args],options);
  86. pgstart = proc.spawn("start",["/min",cmd,...args],options);
  87. }else{
  88. pgstart = proc.spawn(cmd,args,options);
  89. }
  90. pgstart.unref();
  91. setTimeout(() => {
  92. resolve(true)
  93. }, 5000);
  94. // console.log(pgstart)
  95. pgstart.stdout.on("data",(data)=>{
  96. console.log("stdout",data.toString())
  97. resolve(true)
  98. })
  99. pgstart.stderr.on("data",(data)=>{
  100. console.error("stdout",data)
  101. resolve(true)
  102. })
  103. pgstart.on("close",(code)=>{
  104. console.error("close",code)
  105. resolve(true)
  106. })
  107. })
  108. }
  109. function pgStop(dbconfig){
  110. let stopRes = ""
  111. try{stopRes = proc.execSync(`${pgCtlExe} stop -D "${DBDataDir}" -U ${dbconfig.username}`)}catch(err1){
  112. console.log(err1)
  113. }
  114. console.log("stopRes",stopRes)
  115. return
  116. }
  117. function pgStatus(dbconfig){
  118. // ./pg_ctl.exe status -D "C:\web3\psqlite\data\db\minerdb" -U postgres
  119. if(!fs.existsSync(DBDataDir)){
  120. console.log("DB Service Initializing...")
  121. return "noinit"
  122. }
  123. let statusRes = ""
  124. let cmd = `${pgCtlExe} status -D "${DBDataDir}" -U ${dbconfig.username}`
  125. // console.log(cmd)
  126. try{statusRes = proc.execSync(cmd)}catch(err1){
  127. // console.log(err1)
  128. if(err1.status == 3){
  129. return "nostart"
  130. }
  131. }
  132. console.log("statusRes",statusRes)
  133. if(statusRes?.indexOf("PID")){
  134. return "running"
  135. }
  136. return "nostart"
  137. }
  138. module.exports.PostgreSQLKeep = PostgreSQLKeep