@@ -45,35 +45,35 @@ type SQLTransaction struct {
4545 tx * sql.Tx
4646}
4747
48- func (S * SQLTransaction ) Exec (query string , args ... interface {}) (sql.Result , error ) {
48+ func (s * SQLTransaction ) Exec (query string , args ... interface {}) (sql.Result , error ) {
4949 log .Debugf ("Exec: %s, args: %v" , query , args )
50- return S .tx .Exec (query , args ... )
50+ return s .tx .Exec (query , args ... )
5151
5252}
5353
54- func (S * SQLTransaction ) Prepare (query string ) (* sql.Stmt , error ) {
54+ func (s * SQLTransaction ) Prepare (query string ) (* sql.Stmt , error ) {
5555 log .Debugf ("Prepare: %s" , query )
56- return S .tx .Prepare (query )
56+ return s .tx .Prepare (query )
5757}
5858
59- func (S * SQLTransaction ) Query (query string , args ... interface {}) (* sql.Rows , error ) {
59+ func (s * SQLTransaction ) Query (query string , args ... interface {}) (* sql.Rows , error ) {
6060 log .Debugf ("Query: %s, args: %v" , query , args )
61- return S .tx .Query (query , args ... )
61+ return s .tx .Query (query , args ... )
6262}
6363
64- func (S * SQLTransaction ) QueryRow (query string , args ... interface {}) * sql.Row {
64+ func (s * SQLTransaction ) QueryRow (query string , args ... interface {}) * sql.Row {
6565 log .Debugf ("QueryRow: %s, args: %v" , query , args )
66- return S .tx .QueryRow (query , args ... )
66+ return s .tx .QueryRow (query , args ... )
6767}
6868
69- func (S * SQLTransaction ) QueryContext (ctx context.Context , query string , args ... interface {}) (* sql.Rows , error ) {
69+ func (s * SQLTransaction ) QueryContext (ctx context.Context , query string , args ... interface {}) (* sql.Rows , error ) {
7070 log .Debugf ("QueryContext: %s, args: %v" , query , args )
71- return S .tx .QueryContext (ctx , query , args ... )
71+ return s .tx .QueryContext (ctx , query , args ... )
7272}
7373
74- func (S * SQLTransaction ) ExecContext (ctx context.Context , query string , args ... interface {}) (sql.Result , error ) {
74+ func (s * SQLTransaction ) ExecContext (ctx context.Context , query string , args ... interface {}) (sql.Result , error ) {
7575 log .Debugf ("ExecContext: %s, args: %v" , query , args )
76- return S .tx .ExecContext (ctx , query , args ... )
76+ return s .tx .ExecContext (ctx , query , args ... )
7777}
7878
7979type SQLDatabase struct {
@@ -125,12 +125,12 @@ type SQLRepository struct {
125125}
126126
127127type SQLServerConfig struct {
128- Host string `mapstructure:"host", envconfig:"DB_HOST"`
129- Port int `mapstructure:"port", envconfig:"DB_PORT"`
130- User string `mapstructure:"user", envconfig:"DB_USER"`
131- Password string `mapstructure:"password", envconfig:"DB_PASSWORD"`
132- Scheme string `mapstructure:"scheme", envconfig:"DB_SCHEME"`
133- Driver string `mapstructure:"driver", envconfig:"DB_DRIVER"`
128+ Host string `mapstructure:"host" envconfig:"DB_HOST"`
129+ Port int `mapstructure:"port" envconfig:"DB_PORT"`
130+ User string `mapstructure:"user" envconfig:"DB_USER"`
131+ Password string `mapstructure:"password" envconfig:"DB_PASSWORD"`
132+ Scheme string `mapstructure:"scheme" envconfig:"DB_SCHEME"`
133+ Driver string `mapstructure:"driver" envconfig:"DB_DRIVER"`
134134}
135135
136136func NewSQLRepository (config * SQLServerConfig ) (* SQLRepository , error ) {
@@ -142,13 +142,15 @@ func NewSQLRepository(config *SQLServerConfig) (*SQLRepository, error) {
142142 db .SetMaxOpenConns (5 )
143143 db .SetConnMaxLifetime (0 )
144144 db .SetMaxIdleConns (5 )
145+ if log .IsLevelEnabled (log .DebugLevel ) {
146+ go func () {
147+ for {
148+ fmt .Printf ("In use %d not use %d open %d wait %d\n " , db .Stats ().Idle , db .Stats ().InUse , db .Stats ().OpenConnections , db .Stats ().WaitCount )
149+ time .Sleep (time .Second * 5 )
150+ }
151+ }()
152+ }
145153
146- //go func() {
147- // for {
148- // fmt.Printf("In use %d not use %d open %d wait %d\n", db.Stats().Idle, db.Stats().InUse, db.Stats().OpenConnections, db.Stats().WaitCount)
149- // time.Sleep(time.Second * 5)
150- // }
151- //}()
152154 return & SQLRepository {
153155 db : & SQLDatabase {db },
154156 }, nil
@@ -388,11 +390,9 @@ func (s *SQLRepository) GetJobs(ctx context.Context) (jobs *[]model.Job, returnE
388390}
389391
390392func (s * SQLRepository ) getJobs (ctx context.Context , tx SQLDBOperations ) (* []model.Job , error ) {
391- query := fmt .Sprintf (`
392- SELECT v.id, v.source_path,v.source_size, v.target_path,v.target_size, vs.event_time, vs.status, vs.notification_type, vs.message
393+ query := `SELECT v.id, v.source_path,v.source_size, v.target_path,v.target_size, vs.event_time, vs.status, vs.notification_type, vs.message
393394 FROM jobs v
394- INNER JOIN job_status vs ON v.id = vs.job_id
395- ` )
395+ INNER JOIN job_status vs ON v.id = vs.job_id`
396396 rows , err := tx .QueryContext (ctx , query )
397397 if err != nil {
398398 return nil , err
@@ -564,13 +564,11 @@ func (s *SQLRepository) updateJob(ctx context.Context, tx SQLDBOperations, job *
564564
565565func (s * SQLRepository ) getTimeoutJobs (ctx context.Context , tx SQLDBOperations , timeout time.Duration ) ([]* model.TaskEvent , error ) {
566566 timeoutDate := time .Now ().Add (- timeout )
567- timeoutDate .Format (time .RFC3339 )
568567
569568 rows , err := tx .QueryContext (ctx , "select v.* from job_events v right join " +
570569 "(select job_id,max(job_event_id) as job_event_id from job_events where notification_type='Job' group by job_id) as m " +
571570 "on m.job_id=v.job_id and m.job_event_id=v.job_event_id where status in ('assigned','started') and v.event_time < $1::timestamptz" , timeoutDate )
572571
573- //2020-05-17 20:50:41.428531 +00:00
574572 if err != nil {
575573 return nil , err
576574 }
@@ -619,7 +617,6 @@ func (s *SQLRepository) WithTransaction(ctx context.Context, transactionFunc fun
619617func (s * SQLRepository ) queuedJob (ctx context.Context , tx SQLDBOperations ) (* model.Job , error ) {
620618 rows , err := tx .QueryContext (ctx , "select job_id, job_event_id from job_status where notification_type='Job' and status='queued' order by event_time asc limit 1" )
621619
622- //2020-05-17 20:50:41.428531 +00:00
623620 if err != nil {
624621 return nil , err
625622 }
0 commit comments