Skip to content

Commit 0dd377b

Browse files
committed
github action
1 parent 59f426a commit 0dd377b

File tree

6 files changed

+160
-13
lines changed

6 files changed

+160
-13
lines changed

.github/workflows/go.yml

+6-5
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ jobs:
3838
CGO_ENABLED: 0
3939
GOOS: ${{ matrix.go-os }}
4040
GOARCH: ${{ matrix.go-arch }}
41-
run: go build -a -installsuffix cgo -o mqtt-executor-${{ matrix.go-os }}-${{ matrix.go-arch }} -v ./cmd/mqtt-executor/
42-
- uses: actions/upload-artifact@v2
43-
with:
44-
name: mqtt-executor-${{ matrix.go-os }}-${{ matrix.go-arch }}
45-
path: mqtt-executor-${{ matrix.go-os }}-${{ matrix.go-arch }}
41+
run: |
42+
BINARY=mqtt-executor-${{ matrix.go-os }}-${{ matrix.go-arch }}
43+
if [ "$GOOS" = "windows" ]; then
44+
BINARY=$BINARY.exe
45+
fi
46+
go build -a -installsuffix cgo -o $BINARY -v ./cmd/mqtt-executor/

.github/workflows/release.yml

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
on:
2+
push:
3+
tags:
4+
- 'v*' # Push events to matching v*, i.e. v1.0, v20.15.10
5+
6+
name: Create Release
7+
8+
jobs:
9+
release:
10+
name: Create Github Release
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Create Release
14+
id: create_release
15+
uses: actions/create-release@v1
16+
env:
17+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
18+
with:
19+
tag_name: ${{ github.ref }}
20+
release_name: Release ${{ github.ref }}
21+
draft: false
22+
prerelease: false
23+
- name: Output Release URL File
24+
run: echo "${{ steps.create_release.outputs.upload_url }}" > release_url.txt
25+
- name: Save Release URL File for publish
26+
uses: actions/upload-artifact@v1
27+
with:
28+
name: release_url
29+
path: release_url.txt
30+
31+
build:
32+
name: Build
33+
needs: [release]
34+
strategy:
35+
matrix:
36+
go-os: [linux, windows]
37+
go-arch: [386, amd64, arm, arm64]
38+
exclude:
39+
- go-os: windows
40+
go-arch: arm64
41+
runs-on: ubuntu-latest
42+
steps:
43+
- name: Load Release URL File from release job
44+
uses: actions/download-artifact@v1
45+
with:
46+
name: release_url
47+
- name: Get Release Upload URL
48+
id: get_release_info
49+
run: |
50+
value=`cat release_url/release_url.txt`
51+
echo ::set-output name=upload_url::$value
52+
- name: Set up Go 1.x
53+
uses: actions/setup-go@v2
54+
with:
55+
go-version: ^1.14
56+
id: go
57+
- name: Check out code into the Go module directory
58+
uses: actions/checkout@v2
59+
- name: Get dependencies
60+
run: |
61+
go get -v -t -d ./...
62+
if [ -f Gopkg.toml ]; then
63+
curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
64+
dep ensure
65+
fi
66+
- name: Build
67+
id: binary_build
68+
env:
69+
CGO_ENABLED: 0
70+
GOOS: ${{ matrix.go-os }}
71+
GOARCH: ${{ matrix.go-arch }}
72+
run: |
73+
BINARY=mqtt-executor-${{ matrix.go-os }}-${{ matrix.go-arch }}
74+
if [ "$GOOS" = "windows" ]; then
75+
BINARY=$BINARY.exe
76+
fi
77+
go build -a -installsuffix cgo -o $BINARY -v ./cmd/mqtt-executor/
78+
79+
#transfer BINARY env variable to following steps
80+
echo "::set-output name=binary_name::$BINARY"
81+
- name: Upload Release Asset
82+
id: upload-release-asset
83+
uses: actions/upload-release-asset@v1
84+
env:
85+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
86+
with:
87+
upload_url: ${{ steps.get_release_info.outputs.upload_url }}
88+
asset_path: ./${{ steps.binary_build.outputs.binary_name }}
89+
asset_name: ${{ steps.binary_build.outputs.binary_name }}
90+
asset_content_type: application/octet-stream

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.idea
1+
.idea/
22

33
# Binaries for programs and plugins
44
*.exe

cmd/mqtt-executor/config.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ func LoadConfig() {
7373
}
7474
}
7575

76-
func (c *applicationConfig) GetMQTTOpts() *MQTT.ClientOptions {
76+
func (c *applicationConfig) GetMQTTOpts(
77+
onConn MQTT.OnConnectHandler,
78+
onLost MQTT.ConnectionLostHandler) *MQTT.ClientOptions {
79+
7780
opts := MQTT.NewClientOptions()
7881

7982
opts.AddBroker(*c.Broker)
@@ -94,5 +97,8 @@ func (c *applicationConfig) GetMQTTOpts() *MQTT.ClientOptions {
9497
opts.WillTopic = c.TopicConfigurations.Availability.Topic
9598
}
9699

100+
opts.SetOnConnectHandler(onConn)
101+
opts.SetConnectionLostHandler(onLost)
102+
97103
return opts
98104
}

cmd/mqtt-executor/main.go

+17-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ func main() {
2929
defer close(signals)
3030
signal.Notify(signals, os.Interrupt, syscall.SIGTERM)
3131

32-
client := MQTT.NewClient(Config.GetMQTTOpts())
32+
client := MQTT.NewClient(Config.GetMQTTOpts(
33+
handleOnConnection,
34+
handleOnConnectionLost,
35+
))
3336
statusWorker.MqttClient = client
3437
trigger.MqttClient = client
3538
sensorWorker.MqttClient = client
@@ -63,6 +66,19 @@ func main() {
6366
shutdown(client)
6467
}
6568

69+
var handleOnConnection = func(client MQTT.Client) {
70+
if !trigger.IsInitialised() {
71+
return
72+
}
73+
74+
zap.L().Info("Reinitialise...")
75+
trigger.ReInitialise()
76+
}
77+
78+
var handleOnConnectionLost = func(client MQTT.Client, err error) {
79+
zap.L().Warn("Connection lost to broker.", zap.Error(err))
80+
}
81+
6682
func shutdown(client MQTT.Client) {
6783
zap.L().Info("Shutting down...")
6884

internal/mqtt/trigger.go

+39-5
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,60 @@ const (
2222
)
2323

2424
type Trigger struct {
25+
initialised bool
2526
lock sync.RWMutex
2627
runningCommands map[string]context.CancelFunc
2728
triggerConfigs []config.Trigger
29+
subscriptions map[string]subscription
30+
subscribeQOS byte
2831
publishQOS byte
2932

3033
Executor *cmd.CommandExecutor
3134
MqttClient MQTT.Client
3235
}
3336

37+
type subscription struct {
38+
trigger config.Trigger
39+
handler MQTT.MessageHandler
40+
}
41+
3442
func (t *Trigger) Initialise(subscribeQOS, publishQOS byte, triggerConfigs []config.Trigger) {
43+
t.subscribeQOS = subscribeQOS
3544
t.publishQOS = publishQOS
3645
t.runningCommands = map[string]context.CancelFunc{}
46+
t.subscriptions = map[string]subscription{}
3747
t.triggerConfigs = triggerConfigs //safe the configs so that we can unsubscribe later (see Close func)
3848

3949
for _, triggerConf := range triggerConfigs {
40-
t.MqttClient.Subscribe(triggerConf.Topic, subscribeQOS, t.createTriggerHandler(triggerConf))
50+
t.subscriptions[triggerConf.Topic] = subscription{
51+
trigger: triggerConf,
52+
handler: t.createTriggerHandler(triggerConf),
53+
}
54+
55+
t.MqttClient.Subscribe(triggerConf.Topic, subscribeQOS, t.subscriptions[triggerConf.Name].handler)
4156

4257
//publish the stopped state on startup
4358
t.publishStatus(triggerConf.Topic, PayloadStatusStopped)
4459
}
60+
61+
t.initialised = true
62+
}
63+
64+
func (t *Trigger) IsInitialised() bool {
65+
return t.initialised
66+
}
67+
68+
func (t *Trigger) ReInitialise() {
69+
for triggerName, subscription := range t.subscriptions {
70+
t.MqttClient.Subscribe(subscription.trigger.Topic, t.subscribeQOS, subscription.handler)
71+
72+
//publish the current state on reinitialisation
73+
if t.isCommandRunning(triggerName) {
74+
t.publishStatus(subscription.trigger.Topic, PayloadStatusRunning)
75+
} else {
76+
t.publishStatus(subscription.trigger.Topic, PayloadStatusStopped)
77+
}
78+
}
4579
}
4680

4781
func (t *Trigger) createTriggerHandler(triggerConfig config.Trigger) MQTT.MessageHandler {
@@ -56,14 +90,14 @@ func (t *Trigger) createTriggerHandler(triggerConfig config.Trigger) MQTT.Messag
5690
switch action {
5791
case PayloadStart:
5892
//ensure that only one trigger runs at the same time
59-
if t.isCommandRunning(triggerConfig) {
93+
if t.isCommandRunning(triggerConfig.Name) {
6094
zap.L().Warn("Command is already running. Skip execution!", zap.String("trigger", triggerConfig.Name))
6195
return
6296
}
6397

6498
go t.executeCommand(message.Topic(), triggerConfig)
6599
case PayloadStop:
66-
if !t.isCommandRunning(triggerConfig) {
100+
if !t.isCommandRunning(triggerConfig.Name) {
67101
//no command running -> no action
68102
return
69103
}
@@ -75,12 +109,12 @@ func (t *Trigger) createTriggerHandler(triggerConfig config.Trigger) MQTT.Messag
75109
}
76110
}
77111

78-
func (t *Trigger) isCommandRunning(trigger config.Trigger) bool {
112+
func (t *Trigger) isCommandRunning(triggerName string) bool {
79113
//we only need read access
80114
t.lock.RLock()
81115
defer t.lock.RUnlock()
82116

83-
_, exist := t.runningCommands[trigger.Name]
117+
_, exist := t.runningCommands[triggerName]
84118
return exist
85119
}
86120

0 commit comments

Comments
 (0)