-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
142 lines (117 loc) · 3.3 KB
/
main.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"context"
"fmt"
"log"
"os"
"time"
"github.com/vldcreation/sample-cron-go/internal/app"
"github.com/vldcreation/sample-cron-go/internal/pubsubs"
"github.com/vldcreation/sample-cron-go/internal/storage"
"github.com/vldcreation/sample-cron-go/internal/utils"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
initApp := app.App{}
app.Run(ctx, &initApp)
// init instance
// hashs := hashs.NewHashs(sha256.New224(), false, nil)
// declare path file
pathFile := "./test_data/sample.jpeg"
// check file exist or not
ok, err := utils.FileExists(pathFile)
if err != nil {
log.Fatalf("error check file exist: %v\n", err)
panic(err)
}
if !ok {
log.Fatalf("file not exist: %v\n", pathFile)
panic(err)
}
fileName, ext, err := utils.ParseFile(pathFile)
if err != nil {
log.Fatalf("error parse file: %v\n", err)
panic(err)
}
log.Printf("name: %v, ext: %v\n", fileName, ext)
// read path file
// put file to storage
err = initApp.Storage.FPut(ctx, initApp.Config.Storage.Bucket, fileName+"."+ext, pathFile, true, "image/jpeg")
if err != nil {
log.Fatalf("error put file to storage: %v\n", err)
panic(err)
}
var URLGenerated string
// first generated url
bt, err := initApp.Storage.Get(ctx, initApp.Config.Storage.Bucket, fileName+"."+ext)
if err != nil {
log.Fatalf("error get file from storage: %v\n", err)
panic(err)
}
URLGenerated = string(bt)
log.Printf("first url: %v\n", URLGenerated)
//
// initialize adjustment to stop cron
//
var (
maxCron int = 2
curCron int = 0
// use as flat channel to stop cron
isStop = make(chan bool)
)
fnIter := func() {
if curCron >= maxCron {
isStop <- true
// instead of using channel, we can publish message to pubsub (just for sample purpose)
// to stop cron
log.Printf("Send message to stop cron\n")
if err := initApp.Publisherer.Publish(ctx, &pubsubs.Message{
Topic: initApp.Config.PubSub.Topic,
Data: []byte("stop cron"),
}); err != nil {
log.Fatalf("error publish message: %v\n", err)
}
} else {
log.Printf("current cron: %v\n", curCron)
log.Printf("remain cron: %d\n", maxCron-curCron)
curCron++
}
}
// declare function to stop cron
fnStop := func(ctx context.Context, message *pubsubs.Message) {
log.Print(fmt.Sprintf("%s received %s\n", message.Topic, string(message.Data)))
initApp.Cron.Stop()
os.Exit(0)
}
// declare function to reSigned url, then send to cron
ResignedURLFunc := func() {
go fnIter()
log.Println("reSigned url for file ", fileName)
resResignedUrl, err := initApp.Storage.ReSignedURL(ctx, initApp.Config.Storage.Bucket, fileName+"."+ext, URLGenerated)
if err != nil {
log.Fatalf("error reSigned url: %v\n", err)
panic(err)
}
URLGenerated = resResignedUrl
log.Printf("new reSigned url: %v\n", resResignedUrl)
//
// initialize subcriber to stop cron
//
go func() {
select {
case <-isStop:
if err := initApp.Subscriberer.Subscribe(ctx, fnStop); err != nil {
log.Fatalf("error subscribe: %v\n", err)
}
}
}()
}
if err := initApp.Cron.AddJobWithInterval(storage.Test10Seconds, ResignedURLFunc); err != nil {
log.Fatalf("error add job: %v\n", err)
panic(err)
}
time.Sleep(storage.Test10Seconds)
initApp.Cron.StartWithBlocking()
// defer initApp.Cron.Stop()
}