Skip to content

Commit

Permalink
Minor improvements to structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Ordspilleren committed May 18, 2021
1 parent 336e1c5 commit 772d48e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 35 deletions.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func init() {
}

func main() {
config.Monitors.StartMonitoring()
config.Monitors.StartMonitoring(wg, notifiers)

wg.Wait()
}
71 changes: 42 additions & 29 deletions monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io/ioutil"
"log"
"net/http"
"sync"
"time"

"github.com/PuerkitoBio/goquery"
Expand Down Expand Up @@ -35,8 +36,9 @@ type Monitor struct {
doneChannel chan bool
ticker *time.Ticker
id string
notify *notify.Notify
notifier *notify.Notify
client MonitorClient
storage *Storage
}

type Selectors struct {
Expand All @@ -46,36 +48,49 @@ type Selectors struct {

type Monitors []Monitor

func (m Monitors) StartMonitoring() {
for idx := range m {
m[idx].id = generateSHA1String(m[idx].URL)
m[idx].doneChannel = make(chan bool)
m[idx].ticker = time.NewTicker(m[idx].Interval * time.Second)
func (m *Monitor) Init(notifierMap NotifierMap) {
m.id = generateSHA1String(m.URL)
m.doneChannel = make(chan bool)
m.ticker = time.NewTicker(m.Interval * time.Minute)
m.storage = InitStorage(m.id)

m[idx].notify = notify.New()
m.notifier = notify.New()

for _, notifier := range m[idx].Notifiers {
m[idx].notify.UseServices(notifiers[notifier])
}
for _, notifier := range m.Notifiers {
m.notifier.UseServices(notifierMap[notifier])
}

if m[idx].UseChrome {
if m.UseChrome {

} else {
m[idx].client = HttpClient{}
}
} else {
m.client = HttpClient{}
}
}

wg.Add(1)
go func(monitor Monitor) {
for {
select {
case <-monitor.doneChannel:
wg.Done()
return
case <-monitor.ticker.C:
monitor.check()
}
func (m *Monitor) Start(wg *sync.WaitGroup) {
wg.Add(1)
go func(monitor *Monitor) {
monitor.check()
for {
select {
case <-monitor.doneChannel:
wg.Done()
return
case <-monitor.ticker.C:
monitor.check()
}
}(m[idx])
}
}(m)
}

func (m *Monitor) Stop() {
m.doneChannel <- true
}

func (m Monitors) StartMonitoring(wg *sync.WaitGroup, notifierMap NotifierMap) {
for idx := range m {
m[idx].Init(notifierMap)
m[idx].Start(wg)
}
}

Expand All @@ -91,12 +106,10 @@ func (m *Monitor) check() {

selectorContent := m.client.GetContent(m.URL, m.HTTPHeaders, m.Selectors)

storage := InitStorage(m)

if m.compareContent(storage, selectorContent[0]) {
if m.compareContent(m.storage, selectorContent[0]) {
log.Print("Content has changed!")
} else {
_ = m.notify.Send(
_ = m.notifier.Send(
context.Background(),
"Test Subject",
"Test Message!",
Expand Down
10 changes: 5 additions & 5 deletions storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import (
)

type Storage struct {
Monitor *Monitor
ID string
}

func InitStorage(m *Monitor) *Storage {
return &Storage{Monitor: m}
func InitStorage(id string) *Storage {
return &Storage{ID: id}
}

func (s *Storage) GetContent() string {
filePath := filepath.Join(config.StorageDirectory, s.Monitor.id)
filePath := filepath.Join(config.StorageDirectory, s.ID)

os.Mkdir(config.StorageDirectory, os.ModePerm)

Expand All @@ -33,7 +33,7 @@ func (s *Storage) GetContent() string {

func (s *Storage) WriteContent(content string) {
dataDir := "data"
filePath := filepath.Join(dataDir, s.Monitor.id)
filePath := filepath.Join(dataDir, s.ID)

err := ioutil.WriteFile(filePath, []byte(content), 0644)
if err != nil {
Expand Down

0 comments on commit 772d48e

Please sign in to comment.