runtime.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // SiYuan - Build Your Eternal Digital Garden
  2. // Copyright (c) 2020-present, b3log.org
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package util
  17. import (
  18. "os"
  19. "reflect"
  20. "runtime"
  21. "sync"
  22. "time"
  23. "github.com/88250/gulu"
  24. "github.com/dustin/go-humanize"
  25. )
  26. const DatabaseVer = "20220501" // 修改表结构的话需要修改这里
  27. const (
  28. ExitCodeReadOnlyDatabase = 20 // 数据库文件被锁
  29. ExitCodeUnavailablePort = 21 // 端口不可用
  30. ExitCodeCreateConfDirErr = 22 // 创建配置目录失败
  31. ExitCodeBlockTreeErr = 23 // 无法读写 blocktree.msgpack 文件
  32. ExitCodeOk = 0 // 正常退出
  33. ExitCodeFatal = 1 // 致命错误
  34. )
  35. func logBootInfo() {
  36. s, _ := SizeOfDirectory(DataDir, true)
  37. dataDirSize := humanize.Bytes(uint64(s))
  38. LogInfof("kernel is booting:\n"+
  39. " * ver [%s]\n"+
  40. " * arch [%s]\n"+
  41. " * runtime mode [%s]\n"+
  42. " * working directory [%s]\n"+
  43. " * read only [%v]\n"+
  44. " * container [%s]\n"+
  45. " * database [ver=%s]\n"+
  46. " * workspace directory [%s, data %s]",
  47. Ver, runtime.GOARCH, Mode, WorkingDir, ReadOnly, Container, DatabaseVer, WorkspaceDir, dataDirSize)
  48. }
  49. func IsMutexLocked(m *sync.Mutex) bool {
  50. state := reflect.ValueOf(m).Elem().FieldByName("state")
  51. return state.Int()&1 == 1
  52. }
  53. func RandomSleep(minMills, maxMills int) {
  54. r := gulu.Rand.Int(minMills, maxMills)
  55. time.Sleep(time.Duration(r) * time.Millisecond)
  56. }
  57. func SetNetworkProxy(proxyURL string) {
  58. if err := os.Setenv("HTTPS_PROXY", proxyURL); nil != err {
  59. logger.Errorf("set env [HTTPS_PROXY] failed: %s", err)
  60. }
  61. if err := os.Setenv("HTTP_PROXY", proxyURL); nil != err {
  62. logger.Errorf("set env [HTTP_PROXY] failed: %s", err)
  63. }
  64. if "" != proxyURL {
  65. logger.Infof("use network proxy [%s]", proxyURL)
  66. }
  67. }