index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. export async function PostgreSQLKeep(dbconfig){
  32. if(!dbconfig?.dbpath) return
  33. DBDataDir = path.join(OWN_DIR,dbconfig?.dbpath)
  34. console.log("PostgreSQLKeep",dbconfig)
  35. // pgStop(dbconfig)
  36. let start = async ()=>{
  37. await InitAndStartDB(dbconfig)
  38. if(pgStatus(dbconfig)!="running"){
  39. console.log("DB Service Relaunching...")
  40. await wait(1000);
  41. await start();
  42. }
  43. }
  44. await start();
  45. return
  46. }
  47. function wait(delay){
  48. return new Promise((resolve)=>{
  49. setTimeout(() => {
  50. resolve(true)
  51. }, delay);
  52. })
  53. }
  54. async function InitAndStartDB(dbconfig){
  55. let status = pgStatus(dbconfig)
  56. console.log("status",status)
  57. if(status=="noinit"){
  58. let initRes = ""
  59. // ./initdb.exe -D "C:\web3\psqlite\data\db\minerdb" -U postgres -A trust
  60. try{initRes = proc.execSync(`${pgInitExe} -D "${DBDataDir}" -U ${dbconfig.username} -A trust`)}catch(err1){}
  61. console.log("initRes",initRes)
  62. await pgRestart(dbconfig)
  63. return
  64. }
  65. if(status=="nostart"){
  66. await pgRestart(dbconfig)
  67. return
  68. }
  69. return
  70. }
  71. function pgRestart(dbconfig){
  72. console.log("pgRestart")
  73. let startRes = ""
  74. let cmd = `${pgCtlExe}`
  75. let args = [`restart`,`-D`, `"${DBDataDir}"`, `-U`, `${dbconfig.username}`, `-o`, `"-p ${dbconfig.port}"`]
  76. console.log(cmd,args.join(" "))
  77. return new Promise(resolve=>{
  78. let pgstart
  79. let options = {
  80. detached:true,
  81. shell:true,
  82. // stdio:"ignore",
  83. windowsHide:true
  84. }
  85. if(process.platform == 'win32'){ // start 从窗口打开,脱离当前程序 /min 新窗口默认最小化
  86. // pgstart = proc.spawn("cmd",["/c","",cmd,...args],options);
  87. pgstart = proc.spawn("start",["/min",cmd,...args],options);
  88. }else{
  89. pgstart = proc.spawn(cmd,args,options);
  90. }
  91. pgstart.unref();
  92. setTimeout(() => {
  93. resolve(true)
  94. }, 5000);
  95. // console.log(pgstart)
  96. pgstart.stdout.on("data",(data)=>{
  97. console.log("stdout",data.toString())
  98. resolve(true)
  99. })
  100. pgstart.stderr.on("data",(data)=>{
  101. console.error("stdout",data)
  102. resolve(true)
  103. })
  104. pgstart.on("close",(code)=>{
  105. console.error("close",code)
  106. resolve(true)
  107. })
  108. })
  109. }
  110. function pgStop(dbconfig){
  111. let stopRes = ""
  112. try{stopRes = proc.execSync(`${pgCtlExe} stop -D "${DBDataDir}" -U ${dbconfig.username}`)}catch(err1){
  113. console.log(err1)
  114. }
  115. console.log("stopRes",stopRes)
  116. return
  117. }
  118. function pgStatus(dbconfig){
  119. // ./pg_ctl.exe status -D "C:\web3\psqlite\data\db\minerdb" -U postgres
  120. if(!fs.existsSync(DBDataDir)){
  121. console.log("DB Service Initializing...")
  122. return "noinit"
  123. }
  124. let statusRes = ""
  125. let cmd = `${pgCtlExe} status -D "${DBDataDir}" -U ${dbconfig.username}`
  126. // console.log(cmd)
  127. try{statusRes = proc.execSync(cmd)}catch(err1){
  128. // console.log(err1)
  129. if(err1.status == 3){
  130. return "nostart"
  131. }
  132. }
  133. console.log("statusRes",statusRes)
  134. if(statusRes?.indexOf("PID")){
  135. return "running"
  136. }
  137. return "nostart"
  138. }
  139. // module.exports.PostgreSQLKeep = PostgreSQLKeep