Skip to content
This repository has been archived by the owner on Dec 6, 2023. It is now read-only.

Commit

Permalink
check hostname on occurrence trigger, and only output from source.
Browse files Browse the repository at this point in the history
  • Loading branch information
scottleedavis committed May 20, 2019
1 parent 9c9d419 commit 95a50ca
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 24 deletions.
2 changes: 1 addition & 1 deletion plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"id": "com.github.scottleedavis.mattermost-plugin-remind",
"name": "Remind Bot Mattermost Plugin",
"description": "Sets Reminders",
"version": "0.2.6",
"version": "0.2.7",
"server": {
"executables": {
"linux-amd64": "server/dist/plugin-linux-amd64",
Expand Down
2 changes: 1 addition & 1 deletion server/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ var manifest = struct {
Version string
}{
Id: "com.github.scottleedavis.mattermost-plugin-remind",
Version: "0.2.6",
Version: "0.2.7",
}
5 changes: 5 additions & 0 deletions server/occurrence.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"os"
"strconv"
"strings"
"time"
Expand All @@ -12,6 +13,8 @@ import (
)

type Occurrence struct {
Hostname string

Id string

Username string
Expand Down Expand Up @@ -215,7 +218,9 @@ func (p *Plugin) addOccurrences(request *ReminderRequest, occurrences []time.Tim
}
}

hostname, _ := os.Hostname()
occurrence := Occurrence{
Hostname: hostname,
Id: model.NewId(),
Username: request.Username,
ReminderId: request.Reminder.Id,
Expand Down
5 changes: 5 additions & 0 deletions server/reminder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"encoding/json"
"fmt"
"os"
"strings"
"time"

Expand Down Expand Up @@ -53,9 +54,13 @@ func (p *Plugin) TriggerReminders() {
p.API.LogError("Failed to unmarshal reminder occurrences " + fmt.Sprintf("%v", oErr))
return
}
hostname, _ := os.Hostname()

for _, occurrence := range occurrences {

if hostname != occurrence.Hostname {
continue
}
user, uErr := p.API.GetUserByUsername(occurrence.Username)
if uErr != nil {
p.API.LogError("failed to query user %s", user.Id)
Expand Down
3 changes: 3 additions & 0 deletions server/reminder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"encoding/json"
"fmt"
"os"
"testing"
"time"

Expand All @@ -24,8 +25,10 @@ func TestTriggerReminders(t *testing.T) {
}
testTime := time.Now().UTC().Round(time.Second)

hostname, _ := os.Hostname()
occurrences := []Occurrence{
{
Hostname: hostname,
Id: model.NewId(),
ReminderId: model.NewId(),
Occurrence: testTime,
Expand Down
23 changes: 1 addition & 22 deletions server/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@ package main

import (
"fmt"
"os"
"strings"
"time"

"github.com/mattermost/mattermost-server/model"
)

const TriggerHostName = "__TRIGGERHOST__"

func (p *Plugin) ScheduleReminder(request *ReminderRequest, channelId string) (*model.Post, error) {

user, uErr := p.API.GetUserByUsername(request.Username)
Expand Down Expand Up @@ -176,41 +173,23 @@ func (p *Plugin) InteractiveSchedule(triggerId string, user *model.User) {

func (p *Plugin) Run() {
p.Stop()
p.getAndSetLock()
if !p.running {
p.running = true
p.runner()
}
}

func (p *Plugin) Stop() {
p.API.KVSet(TriggerHostName, []byte(""))
p.running = false
}

func (p *Plugin) runner() {
go func() {
<-time.NewTimer(time.Second).C
if !p.running && !p.getAndSetLock() {
if !p.running {
return
}
p.TriggerReminders()
p.runner()
}()
}

func (p *Plugin) getAndSetLock() bool {
hostname, _ := os.Hostname()
bytes, bErr := p.API.KVGet(TriggerHostName)
if bErr != nil {
p.API.LogError("failed KVGet %s", bErr)
return false
}
if string(bytes) != "" && string(bytes) != hostname {
return false
} else if string(bytes) == hostname {
return true
}
p.API.KVSet(TriggerHostName, []byte(hostname))
return true
}

0 comments on commit 95a50ca

Please sign in to comment.