Skip to content

Commit

Permalink
Move package to autopoller
Browse files Browse the repository at this point in the history
Library package is moved to `autopoller` instead of `main`.

Signed-off-by: Elis Lulja <[email protected]>
  • Loading branch information
asimpleidea committed Dec 21, 2020
1 parent f338f67 commit b0051e2
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 4 deletions.
70 changes: 70 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package autopoller

import (
"context"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"

log "github.com/sirupsen/logrus"
)

func main() {
ctx, canc := context.WithCancel(context.Background())
defer canc()

exitChan := make(chan struct{})

p := New(10, false)
var freq int = 10
p.AddPage(&WebsitePage{
ID: "euronics-digital",
URL: "https://www.euronics.it/console/sony-computer/playstation-5-digital-edition/eProd202008907/",
PollSettings: PollSettings{
Type: FixedPolling,
Frequency: &freq,
},
UserAgents: []string{"Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"},
}, do)
p.AddPage(&WebsitePage{
ID: "euronics-standard",
URL: "https://www.euronics.it/console/sony-computer/playstation-5/eProd202008906/",
PollSettings: PollSettings{
Type: FixedPolling,
Frequency: &freq,
},
UserAgents: []string{"Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"},
}, do)

signalChan := make(chan os.Signal, 1)
signal.Notify(
signalChan,
syscall.SIGHUP, // kill -SIGHUP XXXX
syscall.SIGINT, // kill -SIGINT XXXX or Ctrl+c
syscall.SIGQUIT, // kill -SIGQUIT XXXX
)

go p.Start(ctx, exitChan)

<-signalChan
fmt.Println("os.Interrupt - shutting down...")
canc()

// PERFORM GRACEFUL SHUTDOWN HERE
<-exitChan // Wait for euronics to finish

fmt.Println("exiting for real")
os.Exit(0)
}

func do(w *WebsitePage, r *http.Response, e error) {
customFormatter := new(log.TextFormatter)
customFormatter.TimestampFormat = "2006-13-02 15:04:05"
customFormatter.FullTimestamp = true
l := log.WithFields(log.Fields{"id": w.ID, "resp": r.Status})
l.Logger.SetFormatter(customFormatter)
l.Info("got response")
defer r.Body.Close()
}
2 changes: 1 addition & 1 deletion poll_info.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package autopoller

import (
"net/http"
Expand Down
2 changes: 1 addition & 1 deletion poller.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package autopoller

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion utils.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package autopoller

import (
"math/rand"
Expand Down
2 changes: 1 addition & 1 deletion website_page.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package autopoller

// PollType represents the polling type (fixed or randomized)
type PollType string
Expand Down

0 comments on commit b0051e2

Please sign in to comment.