Skip to content

Commit 66f4af7

Browse files
authored
Merge pull request #3 from schjan/logrus
Better + verbose Logging
2 parents 865c4fc + bed1014 commit 66f4af7

File tree

5 files changed

+55
-6
lines changed

5 files changed

+55
-6
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,7 @@ Usage of glock:
1515
can be job, stage or project (default "job")
1616
-unlock
1717
unlock the locked thing
18+
-v int
19+
(default 3) set verbosity level (3: Warning, 4: Info, 5: Debug) (default 3)
1820

1921
```

cmd/lock/main.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"fmt"
77
"github.com/cosee-gitlab/lock"
88
"github.com/cosee-gitlab/lock/db"
9-
"log"
9+
log "github.com/sirupsen/logrus"
1010
"time"
1111
)
1212

@@ -15,30 +15,42 @@ var doLock = flag.Bool("lock", false, "should I aquire a lock? Returns 0 if lock
1515
var doUnlock = flag.Bool("unlock", false, "unlock the locked thing")
1616
var lockExpiration = flag.Duration("expiration", 15*time.Minute, "time after the lock gets removed, even if unlock isn't called")
1717
var scope = flag.String("scope", "job", "can be job, stage or project")
18+
var verbose = flag.Int("v", 3, "set verbosity level (3: Warning, 4: Info, 5: Debug)")
1819

1920
func main() {
2021
flag.Parse()
2122

23+
if *verbose < 0 || 5 < *verbose {
24+
log.Fatalf("Illegal verbosity level: %v", *verbose)
25+
return
26+
}
27+
log.SetLevel(log.Level(*verbose))
28+
2229
env, err := lock.LoadEnvironment()
2330
if err != nil {
2431
log.Fatal(err)
32+
return
2533
}
2634

2735
if env.JobId == 0 || env.PipelineId == 0 || env.ProjectId == 0 || env.JobName == "" {
2836
log.Fatal("It seems that no GitLab environment variables are set")
37+
return
2938
}
3039

3140
if env.RedisHost == "" {
3241
log.Fatal("Please set LOCKS_REDIS_HOST variable")
42+
return
3343
}
3444

3545
if *doLock == *doUnlock {
3646
log.Fatal("I can't lock and unlock . Doin' nothing isn't an option, too")
47+
return
3748
}
3849

3950
client, err := db.New(env.RedisHost)
4051
if err != nil {
4152
log.Fatal(err)
53+
return
4254
}
4355

4456
var key string
@@ -54,20 +66,23 @@ func main() {
5466
key = fmt.Sprintf("%d", env.ProjectId)
5567
default:
5668
log.Fatalf("%q isn't a valid scope. Scope can be job, stage or project.", *scope)
69+
return
5770
}
5871
}
5972

6073
if *doLock {
6174
err = client.Lock(key, env.JobId, *lockExpiration, context.Background())
6275
if err != nil {
6376
log.Fatal(err)
77+
return
6478
}
6579
}
6680

6781
if *doUnlock {
6882
err = client.Unlock(key, env.JobId)
6983
if err != nil {
7084
log.Fatal(err)
85+
return
7186
}
7287
}
7388
}

db/redis.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"context"
55
"github.com/go-redis/redis"
66
"github.com/pkg/errors"
7-
"log"
7+
log "github.com/sirupsen/logrus"
88
"strconv"
99
"time"
1010
)
@@ -24,6 +24,7 @@ func New(host string) (*r, error) {
2424
client := redis.NewClient(options)
2525

2626
err = client.Ping().Err()
27+
log.WithError(err).Debug("Ping()")
2728
if err != nil {
2829
return nil, err
2930
}
@@ -32,6 +33,9 @@ func New(host string) (*r, error) {
3233
}
3334

3435
func (r *r) Lock(key string, jobId int, expiration time.Duration, ctx context.Context) error {
36+
log := log.WithField("key", key)
37+
log.Debug("-lock flag was set, so trying to acquire lock")
38+
3539
locked, err := r.tryLock(key, jobId, expiration)
3640
if err != nil {
3741
return err
@@ -40,7 +44,7 @@ func (r *r) Lock(key string, jobId int, expiration time.Duration, ctx context.Co
4044
return nil
4145
}
4246

43-
log.Printf("somebody else has locked the resource, so trying to get lock for %v", expiration)
47+
log.Warnf("somebody else has already locked the resource, so trying to get lock for %v", expiration)
4448

4549
for {
4650
select {
@@ -60,8 +64,12 @@ func (r *r) Lock(key string, jobId int, expiration time.Duration, ctx context.Co
6064
}
6165

6266
func (r *r) Unlock(key string, jobId int) error {
67+
log := log.WithField("key", key)
68+
log.Debug("-unlock flag was set, so trying to unlock")
69+
6370
var lockId int
6471
getLockId, err := r.c.Get(key).Result()
72+
log.WithField("getLockId", getLockId).WithError(err).Debugf("Get(key:%v)", key)
6573
if err != nil {
6674
if err == redis.Nil {
6775
lockId = 0
@@ -77,21 +85,26 @@ func (r *r) Unlock(key string, jobId int) error {
7785
}
7886

7987
delRes, err := r.c.Del(key).Result()
88+
log.WithField("result", delRes).WithError(err).Debugf("Del(key:%v)", key)
8089
if err != nil {
8190
return err
8291
}
8392

8493
if delRes != 1 {
85-
log.Print("Couldn't remove lock. It isn't there")
94+
log.Warn("Couldn't remove lock. It isn't there")
8695
}
8796

8897
return nil
8998
}
9099

91100
func (r *r) tryLock(key string, jobId int, expiration time.Duration) (bool, error) {
101+
log := log.WithField("key", key)
102+
log.Info("Trying...")
103+
92104
var newestJob int
93105
var lockId int
94106
getLockId, err := r.c.Get(key).Result()
107+
log.WithField("getLockId", getLockId).WithError(err).Debugf("Get(key:%v)", key)
95108
if err != nil {
96109
if err == redis.Nil {
97110
lockId = 0
@@ -107,6 +120,7 @@ func (r *r) tryLock(key string, jobId int, expiration time.Duration) (bool, erro
107120
}
108121

109122
getNewestJobResult, err := r.c.Get(key + newestJobKey).Result()
123+
log.WithField("getNewestJobResult", getNewestJobResult).WithError(err).Debugf("Get(key:%v)", key+newestJobKey)
110124
if err != nil {
111125
if err == redis.Nil {
112126
newestJob = 0
@@ -127,10 +141,12 @@ func (r *r) tryLock(key string, jobId int, expiration time.Duration) (bool, erro
127141
if newestJob > jobId {
128142
return false, errors.Errorf("Newer Job: %v > Actual Job: %v is also waiting for a lock.", newestJob, jobId)
129143
} else if newestJob < jobId {
130-
r.c.Set(key+newestJobKey, jobId, 0).Err()
144+
err = r.c.Set(key+newestJobKey, jobId, 0).Err()
145+
log.WithError(err).Debugf("Set(key:%v, jobId:%v, 0)", key, jobId)
131146
}
132147

133148
set, err := r.c.SetNX(key, jobId, expiration).Result()
149+
log.WithError(err).WithField("logAcquired", set).Debugf("SetNX(key:%v, jobId:%v, expiration:%v)", key, jobId, expiration)
134150
if err != nil {
135151
return false, err
136152
}

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@ module github.com/cosee-gitlab/lock
33
require (
44
github.com/go-redis/redis v6.14.1+incompatible
55
github.com/kelseyhightower/envconfig v1.3.0
6+
github.com/konsorten/go-windows-terminal-sequences v1.0.1 // indirect
67
github.com/kr/pretty v0.1.0 // indirect
78
github.com/onsi/gomega v1.4.2 // indirect
89
github.com/pkg/errors v0.8.0
10+
github.com/sirupsen/logrus v1.1.1
11+
golang.org/x/crypto v0.0.0-20181015023909-0c41d7ab0a0e // indirect
912
golang.org/x/net v0.0.0-20180926154720-4dfa2610cdf3 // indirect
10-
golang.org/x/sys v0.0.0-20180928133829-e4b3c5e90611 // indirect
13+
golang.org/x/sys v0.0.0-20181011152604-fa43e7bc11ba // indirect
1114
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
1215
)

go.sum

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
12
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
23
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
34
github.com/go-redis/redis v6.14.1+incompatible h1:kSJohAREGMr344uMa8PzuIg5OU6ylCbyDkWkkNOfEik=
@@ -8,6 +9,8 @@ github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
89
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
910
github.com/kelseyhightower/envconfig v1.3.0 h1:IvRS4f2VcIQy6j4ORGIf9145T/AsUB+oY8LyvN8BXNM=
1011
github.com/kelseyhightower/envconfig v1.3.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=
12+
github.com/konsorten/go-windows-terminal-sequences v0.0.0-20180402223658-b729f2633dfe/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
13+
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
1114
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
1215
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
1316
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
@@ -19,16 +22,26 @@ github.com/onsi/gomega v1.4.2 h1:3mYCb7aPxS/RU7TI1y4rkEn1oKmPRjNJLNEXgw7MH2I=
1922
github.com/onsi/gomega v1.4.2/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
2023
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
2124
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
25+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
26+
github.com/sirupsen/logrus v1.1.1 h1:VzGj7lhU7KEB9e9gMpAV/v5XT2NVSvLJhJLCWbnkgXg=
27+
github.com/sirupsen/logrus v1.1.1/go.mod h1:zrgwTnHtNr00buQ1vSptGe8m1f/BbgsPukg8qsT7A+A=
28+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
29+
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
30+
golang.org/x/crypto v0.0.0-20181015023909-0c41d7ab0a0e h1:IzypfodbhbnViNUO/MEh0FzCUooG97cIGfdggUrUSyU=
31+
golang.org/x/crypto v0.0.0-20181015023909-0c41d7ab0a0e/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
2232
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd h1:nTDtHvHSdCn1m6ITfMRqtOd/9+7a3s8RBNOZ3eYZzJA=
2333
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
2434
golang.org/x/net v0.0.0-20180926154720-4dfa2610cdf3 h1:dgd4x4kJt7G4k4m93AYLzM8Ni6h2qLTfh9n9vXJT3/0=
2535
golang.org/x/net v0.0.0-20180926154720-4dfa2610cdf3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
2636
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f h1:wMNYb4v58l5UBM7MYRLPG6ZhfOqbKu7X5eyFl8ZhKvA=
2737
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
38+
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
2839
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e h1:o3PsSEY8E4eXWkXrIP9YJALUkVZqzHJT5DOasTyn8Vs=
2940
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
3041
golang.org/x/sys v0.0.0-20180928133829-e4b3c5e90611 h1:O33LKL7WyJgjN9CvxfTIomjIClbd/Kq86/iipowHQU0=
3142
golang.org/x/sys v0.0.0-20180928133829-e4b3c5e90611/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
43+
golang.org/x/sys v0.0.0-20181011152604-fa43e7bc11ba h1:nZJIJPGow0Kf9bU9QTc1U6OXbs/7Hu4e+cNv+hxH+Zc=
44+
golang.org/x/sys v0.0.0-20181011152604-fa43e7bc11ba/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
3245
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
3346
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
3447
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=

0 commit comments

Comments
 (0)