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
143
144
145
146
147
148
149
|
package main
import (
"os"
"os/exec"
"os/signal"
"strings"
"sync"
"syscall"
"time"
log "github.com/Sirupsen/logrus"
"github.com/kr/beanstalk"
)
// beanstalkd, the message queue
type Queue struct {
Conn *beanstalk.Conn
Tube *beanstalk.Tube
}
func (app *Queue) newBeanstalkd() error {
conn, err := beanstalk.Dial("tcp", "192.168.122.1:11300")
//conn, err := beanstalk.Dial("tcp", "127.0.0.1:11300")
if err != nil {
return err
}
app.Conn = conn
app.Tube = &beanstalk.Tube{conn, "gospelchor"}
return nil
}
func main() {
app := Queue{}
err := app.newBeanstalkd()
if err != nil {
log.WithFields(
log.Fields{
"error": err,
},
).Fatal("cannot connect to beanstalkd")
}
// closes the connection to the beanstalkd server on exit
defer func() {
app.Conn.Close()
}()
cntGoRoutine := 0
// we will ensure that we wait for the same number
// of routines to complete by using a waitGroup
waitGroup := &sync.WaitGroup{}
waitGroup.Add(cntGoRoutine + 1)
shutdownChan := make(chan bool)
log.Println("app started. beginning consuming jobs...")
app.consumeJob(shutdownChan, waitGroup, 1)
// waits for shutting down
term := make(chan os.Signal, 1)
signal.Notify(term, os.Interrupt)
signal.Notify(term, syscall.SIGTERM)
<-term
// sends the signal to close to the workers
// +1 because we have a separate goroutine
// to check for buried jobs
for i := 0; i < cntGoRoutine+1; i++ {
go func() {
shutdownChan <- true
}()
}
// we wait, until they complete
log.Info("waiting for goroutine to finish")
waitGroup.Wait()
}
func (app *Queue) consumeJob(shutdownChannel chan bool, waitGroup *sync.WaitGroup, number int) {
tubeset := beanstalk.NewTubeSet(app.Conn, "gospelchor")
// we tell when we return
defer waitGroup.Done()
// Looping to consume all Jobs
for {
select {
case _ = <-shutdownChannel:
log.WithFields(
log.Fields{
"goroutine_id": number + 1,
},
).Info("getting message to return: ")
return
default:
id, body, err := tubeset.Reserve(60 * time.Second)
if err != nil {
//log.Println(err)
continue
}
log.WithFields(
log.Fields{
"job_id": id,
},
).Info("recieving job")
/*
err = json.Unmarshal(body)
if err != nil {
app.Conn.Bury(id, 1)
log.WithFields(
log.Fields{
"job_id": id,
"error": err,
},
).Warn("decoding json failed")
continue
}
*/
if !strings.EqualFold(string(body), "true") {
app.Conn.Bury(id, 1)
continue
}
// rsync den ordner rüber
cmdArgs := []string{"--delete", "-avz", "-e ssh", "applications:/var/www/gospeladlershof.de/gospeladlershof.de/", "/var/www/gospeladlershof.de/gospeladlershof.de/"}
if _, err = exec.Command("rsync", cmdArgs...).Output(); err != nil {
log.WithFields(
log.Fields{
"error": err,
},
).Warn("fatal error using rsync")
app.Conn.Bury(id, 1)
} else {
log.Println("seite synchronisiert")
// Deleting the job from beanstalkd
app.Conn.Delete(id)
}
}
}
}
|