init.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package tick
  2. import "log"
  3. import "database/sql"
  4. import _ "github.com/go-sql-driver/mysql"
  5. import "fmt"
  6. import "time"
  7. import "errors"
  8. var db *sql.DB
  9. var tserver *TickServer
  10. var serverconf *ServerConf
  11. type newDs func(conf *DsConf) (DataSource, error)
  12. var drivers = make(map[string]newDs)
  13. //设置数据库连接
  14. func initdb(dbconf *DBConf) {
  15. var err error
  16. db, err = sql.Open(dbconf.DBDriver, dbconf.DSN)
  17. if err != nil {
  18. panic(err)
  19. }
  20. }
  21. func getDriver(name string) (newDs, error) {
  22. if ret, ok := drivers[name]; ok {
  23. return ret, nil
  24. }
  25. return nil, errors.New("getDriver " + name + " error.")
  26. }
  27. func SetConf(conf *ServerConf) {
  28. serverconf = conf
  29. }
  30. func InitDb() {
  31. initdb(&serverconf.DBConf)
  32. }
  33. func Init() {
  34. //parse
  35. //log.Println("initdb")
  36. initdb(&serverconf.DBConf)
  37. var err error
  38. //getdblock,global 获取一个数据库的锁,这个锁可以证明没有其他的实例运行,操作数据库
  39. tserver, err = NewTickServer()
  40. if err != nil {
  41. panic(err)
  42. }
  43. //log.Println("run datasource...")
  44. runDataSource(serverconf)
  45. }
  46. func getFilePath(ty string) (string, error) {
  47. if conf, ok := serverconf.DsMap[ty]; ok {
  48. return conf.SaveDir, nil
  49. }
  50. return "", ErrDataSourceType
  51. }
  52. func runDataSource(conf *ServerConf) {
  53. for name, dsConf := range conf.DsMap {
  54. if dsConf.Run {
  55. ds := NewDataSource(name, dsConf)
  56. if ds != nil {
  57. tserver.AddDataSource(ds, dsConf.Download)
  58. go ds.Run()
  59. }
  60. //log.Println("run", name)
  61. }
  62. }
  63. }
  64. func NewDataSource(name string, conf *DsConf) DataSource {
  65. driver, err := getDriver(name)
  66. if err != nil {
  67. log.Fatal(err)
  68. }
  69. ds, err := driver(conf)
  70. if err != nil {
  71. log.Fatal(err)
  72. }
  73. return ds
  74. }
  75. func lockDb(name string) bool {
  76. //setLock
  77. q := "UPDATE system_lock SET flag=1 WHERE name='%s' AND flag <> 1"
  78. q = fmt.Sprintf(q, name)
  79. result, err := db.Exec(q)
  80. if err != nil {
  81. //log.Println("[lockdb]", err)
  82. return false
  83. }
  84. n, err := result.RowsAffected()
  85. if err != nil {
  86. //log.Println("[lockdb]", err)
  87. return false
  88. }
  89. if n == 1 {
  90. return true
  91. }
  92. //log.Println("[lockdb]", "lock an locked lock")
  93. return false
  94. }
  95. func LockDb(name string, wait bool) bool {
  96. for {
  97. ok := lockDb(name)
  98. if !ok && wait {
  99. time.Sleep(time.Millisecond * 100)
  100. continue
  101. }
  102. return ok
  103. }
  104. }
  105. func UnlockDb(name string) bool {
  106. q := "UPDATE system_lock SET flag=0 WHERE name='%s' AND flag <> 0"
  107. q = fmt.Sprintf(q, name)
  108. result, err := db.Exec(q)
  109. if err != nil {
  110. //log.Println("[unlockdb]", err)
  111. return false
  112. }
  113. n, err := result.RowsAffected()
  114. if err != nil {
  115. //log.Println("[unlockdb]", err)
  116. return false
  117. }
  118. if n == 1 {
  119. return true
  120. }
  121. panic("unlock a unlocked lock")
  122. }