@@ -2,13 +2,19 @@ package mongo
22
33import "github.com/cloudtask/cloudtask-center/cache/driver/types"
44import "github.com/cloudtask/common/models"
5+ import "github.com/cloudtask/libtools/gounits/system"
56import "gopkg.in/mgo.v2/bson"
67import mgo "gopkg.in/mgo.v2"
78
89import (
10+ "strings"
911 "time"
1012)
1113
14+ const (
15+ defaultMaxPoolSize = 20
16+ )
17+
1218const (
1319 SYS_LOCATIONNS = "sys_locations"
1420 SYS_JOBS = "sys_jobs"
@@ -19,47 +25,100 @@ type M bson.M
1925
2026type D bson.D
2127
28+ //MgoConfigs is exported
29+ type MgoConfigs struct {
30+ Hosts string
31+ DataBase string
32+ Auth map [string ]string
33+ Options []string
34+ }
35+
2236//Engine is exported
2337type Engine struct {
24- Hosts string
25- DBName string
26- User string
27- Password string
28- MaxPoolSize int
38+ MgoConfigs
2939 globalSession * mgo.Session
3040 failPulseTimes int
3141 stopCh chan struct {}
3242}
3343
3444//NewEngine is exported
35- func NewEngine (hosts string , dbName string , maxPoolSize int , user string , password string ) * Engine {
45+ func NewEngine (configs MgoConfigs ) * Engine {
3646
3747 return & Engine {
38- Hosts : hosts ,
39- DBName : dbName ,
40- User : user ,
41- Password : password ,
42- MaxPoolSize : maxPoolSize ,
43- stopCh : make (chan struct {}),
48+ MgoConfigs : configs ,
49+ stopCh : make (chan struct {}),
4450 }
4551}
4652
53+ func generateHostURL (configs MgoConfigs ) (string , error ) {
54+
55+ configs .Hosts = strings .TrimSpace (configs .Hosts )
56+ if len (configs .Hosts ) == 0 {
57+ return "" , ErrMongoStorageDriverHostsInvalid
58+ }
59+
60+ configs .DataBase = strings .TrimSpace (configs .DataBase )
61+ if len (configs .DataBase ) == 0 {
62+ return "" , ErrMongoStorageDriverDataBaseInvalid
63+ }
64+
65+ var authStr string
66+ if len (configs .Auth ) > 0 {
67+ var (
68+ user , password string
69+ ret bool
70+ )
71+ if user , ret = configs .Auth ["user" ]; ret {
72+ authStr = user
73+ if password , ret = configs .Auth ["password" ]; ret {
74+ authStr = authStr + ":" + password
75+ }
76+ authStr = authStr + "@"
77+ }
78+ }
79+
80+ var optsStr string
81+ if len (configs .Options ) > 0 {
82+ for index , value := range configs .Options {
83+ optsStr = optsStr + value
84+ if index != len (configs .Options )- 1 {
85+ optsStr = optsStr + "&"
86+ }
87+ }
88+ }
89+
90+ mgoURL := "mongodb://" + authStr + configs .Hosts + "/" + configs .DataBase
91+ if optsStr != "" {
92+ mgoURL = mgoURL + "?" + optsStr
93+ }
94+
95+ if _ , err := mgo .ParseURL (mgoURL ); err != nil {
96+ return "" , err
97+ }
98+ return mgoURL , nil
99+ }
100+
47101//Open is exported
48102func (engine * Engine ) Open () error {
49103
50- session , err := mgo .Dial (engine .Hosts )
104+ var maxPoolSize = defaultMaxPoolSize
105+ opts := system .DriverOpts (engine .Options )
106+ if value , ret := opts .Int ("maxPoolSize" , "" ); ret {
107+ maxPoolSize = (int )(value )
108+ }
109+
110+ mgoURL , err := generateHostURL (engine .MgoConfigs )
51111 if err != nil {
52112 return err
53113 }
54114
55- session .SetMode (mgo .Strong , true )
56- session .SetPoolLimit (engine .MaxPoolSize )
57- database := session .DB (engine .DBName )
58- if engine .User != "" {
59- if err := database .Login (engine .User , engine .Password ); err != nil {
60- return err
61- }
115+ session , err := mgo .Dial (mgoURL )
116+ if err != nil {
117+ return err
62118 }
119+
120+ session .SetMode (mgo .Strong , true )
121+ session .SetPoolLimit (maxPoolSize )
63122 engine .globalSession = session
64123 go engine .pulseSessionLoop ()
65124 return nil
@@ -79,7 +138,7 @@ func (engine *Engine) getLocation(location string) (*models.WorkLocation, error)
79138 session := engine .getSession ()
80139 defer session .Close ()
81140 workLocation := & models.WorkLocation {}
82- if err := session .DB (engine .DBName ).C (SYS_LOCATIONNS ).
141+ if err := session .DB (engine .DataBase ).C (SYS_LOCATIONNS ).
83142 Find (M {"location" : location }).
84143 Select (M {"_id" : 0 }).One (workLocation ); err != nil {
85144 if err == mgo .ErrNotFound {
@@ -94,15 +153,15 @@ func (engine *Engine) postLocation(workLocation *models.WorkLocation) error {
94153
95154 session := engine .getSession ()
96155 defer session .Close ()
97- return session .DB (engine .DBName ).C (SYS_LOCATIONNS ).
156+ return session .DB (engine .DataBase ).C (SYS_LOCATIONNS ).
98157 Insert (workLocation )
99158}
100159
101160func (engine * Engine ) putLocation (workLocation * models.WorkLocation ) error {
102161
103162 session := engine .getSession ()
104163 defer session .Close ()
105- return session .DB (engine .DBName ).C (SYS_LOCATIONNS ).
164+ return session .DB (engine .DataBase ).C (SYS_LOCATIONNS ).
106165 Update (M {"location" : workLocation .Location }, workLocation )
107166}
108167
@@ -111,7 +170,7 @@ func (engine *Engine) readLocationsName() ([]string, error) {
111170 session := engine .getSession ()
112171 defer session .Close ()
113172 workLocations := []* models.WorkLocation {}
114- if err := session .DB (engine .DBName ).C (SYS_LOCATIONNS ).
173+ if err := session .DB (engine .DataBase ).C (SYS_LOCATIONNS ).
115174 Find (M {}).
116175 Select (M {"_id" : 0 , "location" : 1 }).
117176 All (& workLocations ); err != nil {
@@ -130,7 +189,7 @@ func (engine *Engine) readSimpleJobs(query M) ([]*models.SimpleJob, error) {
130189 session := engine .getSession ()
131190 defer session .Close ()
132191 jobs := []* models.SimpleJob {}
133- if err := session .DB (engine .DBName ).C (SYS_JOBS ).
192+ if err := session .DB (engine .DataBase ).C (SYS_JOBS ).
134193 Find (query ).
135194 Select (M {"_id" : 0 , "jobid" : 1 , "name" : 1 , "location" : 1 , "groupid" : 1 , "servers" : 1 , "enabled" : 1 , "stat" : 1 }).
136195 All (& jobs ); err != nil {
@@ -144,7 +203,7 @@ func (engine *Engine) readJobs(query M) ([]*models.Job, error) {
144203 session := engine .getSession ()
145204 defer session .Close ()
146205 jobs := []* models.Job {}
147- if err := session .DB (engine .DBName ).C (SYS_JOBS ).
206+ if err := session .DB (engine .DataBase ).C (SYS_JOBS ).
148207 Find (query ).
149208 Select (M {"_id" : 0 }).
150209 All (& jobs ); err != nil {
@@ -176,7 +235,7 @@ func (engine *Engine) getJob(jobid string) (*models.Job, error) {
176235 session := engine .getSession ()
177236 defer session .Close ()
178237 job := & models.Job {}
179- if err := session .DB (engine .DBName ).C (SYS_JOBS ).
238+ if err := session .DB (engine .DataBase ).C (SYS_JOBS ).
180239 Find (M {"jobid" : jobid }).
181240 Select (M {"_id" : 0 }).One (job ); err != nil {
182241 if err == mgo .ErrNotFound {
@@ -191,15 +250,15 @@ func (engine *Engine) putJob(job *models.Job) error {
191250
192251 session := engine .getSession ()
193252 defer session .Close ()
194- return session .DB (engine .DBName ).C (SYS_JOBS ).
253+ return session .DB (engine .DataBase ).C (SYS_JOBS ).
195254 Update (M {"jobid" : job .JobId }, job )
196255}
197256
198257func (engine * Engine ) postJobLog (jobLog * models.JobLog ) error {
199258
200259 session := engine .getSession ()
201260 defer session .Close ()
202- return session .DB (engine .DBName ).C (SYS_LOGS ).
261+ return session .DB (engine .DataBase ).C (SYS_LOGS ).
203262 Insert (jobLog )
204263}
205264
0 commit comments