启动程序
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

154 rindas
2.8 KiB

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "net/http"
  6. "os"
  7. "os/exec"
  8. "time"
  9. )
  10. func main() {
  11. lockdir:=os.Getenv("TSZDIR")
  12. exitChannel := make(chan int, 10)
  13. //线程启动ipfs
  14. fmt.Println("start ipfs")
  15. datapath := lockdir+"go-ipfs_v0.7.0_windows-amd64\\go-ipfs\\ipfs.exe"
  16. ipfsCd := exec.Command(datapath,"daemon")
  17. go func() {
  18. err := ipfsCd.Run()
  19. if err != nil {
  20. fmt.Print("ipfs")
  21. fmt.Println(err)
  22. exitChannel <- 1
  23. }
  24. }()
  25. //线程启动fts
  26. fmt.Println("start fts")
  27. datapath = lockdir+"bin\\fts.exe"
  28. ftsCd := exec.Command(datapath)
  29. go func() {
  30. err := ftsCd.Run()
  31. if err != nil {
  32. fmt.Print("fts")
  33. fmt.Println(err)
  34. exitChannel <- 1
  35. }
  36. }()
  37. //线程启动etcd
  38. fmt.Println("start etcd")
  39. datadir :=os.Getenv("APPDATA")+"\\"+"LOCKING"
  40. datapath = lockdir+"etcd-v3.3.25-windows-amd64\\etcd.exe"
  41. etcdCd := exec.Command(datapath,"--data-dir",datadir)
  42. go func() {
  43. err := etcdCd.Run()
  44. if err != nil {
  45. fmt.Print("etcd")
  46. fmt.Println(err)
  47. exitChannel <- 1
  48. }
  49. }()
  50. //检测fts进程是否启动成功,最大超时5s
  51. timeOutMax := 50
  52. timeOutIndex := 0
  53. for true {
  54. resp, err := http.Get("http://127.0.0.1:7777")
  55. if err != nil {
  56. if timeOutIndex>timeOutMax{
  57. fmt.Printf("http.Get()函数执行错误,错误为:%v\n", err)
  58. exitChannel <- 1
  59. }
  60. time.Sleep(100*time.Millisecond)
  61. timeOutIndex++
  62. continue
  63. }
  64. defer resp.Body.Close()
  65. break
  66. }
  67. //检测etcd进程是否启动成功,最大超时5s
  68. timeOutMax = 50
  69. timeOutIndex = 0
  70. for true {
  71. resp, err := http.Get("http://127.0.0.1:2379")
  72. if err != nil {
  73. if timeOutIndex>timeOutMax{
  74. fmt.Printf("http.Get()函数执行错误,错误为:%v\n", err)
  75. exitChannel <- 1
  76. }
  77. time.Sleep(100*time.Millisecond)
  78. timeOutIndex++
  79. continue
  80. }
  81. defer resp.Body.Close()
  82. break
  83. }
  84. //检测ipfs进程是否启动成功,最大超时10s
  85. timeOutMax = 50
  86. timeOutIndex = 0
  87. for true {
  88. resp, err := http.Get("http://127.0.0.1:5001")
  89. if err != nil {
  90. if timeOutIndex>timeOutMax{
  91. fmt.Printf("http.Get()函数执行错误,错误为:%v\n", err)
  92. exitChannel <- 1
  93. }
  94. time.Sleep(200*time.Millisecond)
  95. timeOutIndex++
  96. continue
  97. }
  98. defer resp.Body.Close()
  99. break
  100. }
  101. //启动客户端
  102. datapath = lockdir+"locking-client-win32-x64\\locking-client.exe"
  103. lockingCd := exec.Command(datapath)
  104. err := lockingCd.Start()
  105. fmt.Println("start locking")
  106. log.Println(lockingCd.Process.Pid)
  107. if err != nil {
  108. fmt.Println(err)
  109. exitChannel <- 1
  110. }
  111. /*go func() {
  112. for true{
  113. log.Println(cd.Process.Pid)//client 存活情况
  114. }
  115. }()*/
  116. //收取退出命令
  117. for i := range exitChannel {
  118. if i==1{
  119. if ftsCd.Process !=nil{
  120. ftsCd.Process.Kill()
  121. }
  122. if ipfsCd.Process !=nil{
  123. ipfsCd.Process.Kill()
  124. }
  125. if etcdCd.Process !=nil{
  126. etcdCd.Process.Kill()
  127. }
  128. if lockingCd.Process !=nil{
  129. lockingCd.Process.Kill()
  130. }
  131. os.Exit(1)
  132. }
  133. }
  134. os.Exit(1)
  135. }