lancher-client.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package comm
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "os"
  6. "strings"
  7. "time"
  8. "github.com/nats-io/nats.go"
  9. )
  10. // 客户端运行
  11. var _currAppId = ""
  12. func LancherClientRun(bus *NatsBus, ExePath string, configfile string, iconUrl string) error {
  13. currApp, e := ParseConfigFile(configfile)
  14. if e != nil {
  15. return e
  16. }
  17. id := GetAppPathId(ExePath)
  18. _currAppId = id
  19. currApp.Id = id
  20. currApp.LastRuningTime = time.Now()
  21. currApp.RegTime = time.Now()
  22. currApp.Status = APPSTATE_RUNNING
  23. currApp.IconUrl = iconUrl
  24. pwd, err := os.Getwd()
  25. if err != nil {
  26. return err
  27. }
  28. currApp.Pwd = pwd
  29. currApp.ExeFpath = ExePath
  30. currApp.Args = strings.Join(os.Args[1:], " ")
  31. nc := bus.GetNatsConn()
  32. js, e := nc.JetStream()
  33. if e != nil {
  34. return e
  35. }
  36. payload, e := json.Marshal(currApp)
  37. if e != nil {
  38. return e
  39. }
  40. kv, e := js.CreateKeyValue(&nats.KeyValueConfig{Bucket: "lancher-apps"})
  41. if e != nil {
  42. return e
  43. }
  44. v, e := kv.Get(currApp.Id)
  45. if e != nil {
  46. if e == nats.ErrKeyNotFound {
  47. _, e := kv.Put(currApp.Id, payload)
  48. if e != nil {
  49. return e
  50. }
  51. return nil
  52. } else {
  53. return nil
  54. }
  55. }
  56. lastApp := &App{}
  57. e = json.Unmarshal(v.Value(), lastApp)
  58. if e != nil {
  59. return e
  60. }
  61. lastApp.Name = currApp.Name
  62. lastApp.Label = currApp.Label
  63. lastApp.Icon = currApp.Icon
  64. lastApp.Desc = currApp.Name
  65. lastApp.LastRuningTime = time.Now()
  66. lastApp.Status = currApp.Status
  67. lastApp.IconUrl = currApp.IconUrl
  68. lastApp.ExeFpath = currApp.ExeFpath
  69. lastApp.Args = currApp.Args
  70. lastApp.Pwd = currApp.Pwd
  71. payload, _ = json.Marshal(lastApp)
  72. _, e = kv.Update(currApp.Id, payload, v.Revision())
  73. if e != nil {
  74. return e
  75. }
  76. kv.Purge(currApp.Id, nats.LastRevision(v.Revision()))
  77. vs, _ := kv.History(currApp.Id)
  78. fmt.Println(len(vs))
  79. return nil
  80. }
  81. func LancherClientQuitApp(bus *NatsBus) {
  82. nc := bus.GetNatsConn()
  83. js, e := nc.JetStream()
  84. if e != nil {
  85. return
  86. }
  87. kv, e := js.CreateKeyValue(&nats.KeyValueConfig{Bucket: "lancher-apps"})
  88. if e != nil {
  89. return
  90. }
  91. v, e := kv.Get(_currAppId)
  92. if e != nil {
  93. return
  94. }
  95. lastApp := &App{}
  96. e = json.Unmarshal(v.Value(), lastApp)
  97. if e != nil {
  98. return
  99. }
  100. lastApp.Status = APPSTATE_INSTALLED
  101. payload, _ := json.Marshal(lastApp)
  102. kv.Update(_currAppId, payload, v.Revision())
  103. }