/
gcp_lock.go
90 lines (76 loc) · 2.22 KB
/
gcp_lock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package gcp
import (
"context"
"fmt"
"log"
"strconv"
"time"
"cloud.google.com/go/storage"
)
type GoogleStorageLock struct {
Client *storage.Client
Bucket *storage.BucketHandle
Context context.Context
}
func (googleLock *GoogleStorageLock) Lock(transactionId int, resource string) (bool, error) {
now := time.Now().Format(time.RFC3339)
fileName := resource
wc := googleLock.Bucket.Object(fileName).NewWriter(googleLock.Context)
wc.ContentType = "text/plain"
wc.Metadata = map[string]string{
"LockId": strconv.Itoa(transactionId),
"CreatedAt": now,
}
bucketAttrs, err := googleLock.Bucket.Attrs(googleLock.Context)
if err != nil {
log.Printf("failed to get bucket attributes: %v\n", err)
}
bucketName := bucketAttrs.Name
if err := wc.Close(); err != nil {
log.Printf("createFile: unable to close bucket %q, file %q: %v\n", bucketName, fileName, err)
}
return true, nil
}
func (googleLock *GoogleStorageLock) Unlock(resource string) (bool, error) {
fileName := resource
existingLockTransactionId, err := googleLock.GetLock(resource)
if err != nil {
log.Printf("failed to get lock: %v\n", err)
}
if existingLockTransactionId == nil {
return false, nil
}
fileObject := googleLock.Bucket.Object(fileName)
err = fileObject.Delete(googleLock.Context)
if err != nil {
return false, fmt.Errorf("failed to delete lock file: %v\n", err)
}
return true, nil
}
func (googleLock *GoogleStorageLock) GetLock(resource string) (*int, error) {
fileName := resource
fileObject := googleLock.Bucket.Object(fileName)
fileAttrs, err := fileObject.Attrs(googleLock.Context)
if err != nil {
// TODO: not sure if it's the best way to handle it
if err.Error() == "storage: object doesn't exist" {
return nil, nil
}
return nil, err
}
fileMetadata := fileAttrs.Metadata
lockIdStr := fileMetadata["LockId"]
transactionId, err := strconv.Atoi(lockIdStr)
if err != nil {
log.Printf("failed to parse LockId in object's metadata: %v\n", err)
}
return &transactionId, nil
}
func GetGoogleStorageClient() (context.Context, *storage.Client) {
ctx := context.Background()
client, err := storage.NewClient(ctx)
if err != nil {
log.Fatalf("Failed to create Google Storage client: %v", err)
}
return ctx, client
}