mirror of
https://github.com/Fishwaldo/sched.git
synced 2025-07-23 05:18:35 +00:00
Add Examples + More Verbose Logging
Signed-off-by: Sherif Abdel-Naby <sherifabdlnaby@gmail.com>
This commit is contained in:
parent
cb8f233b4d
commit
8fc48d852e
21 changed files with 1169 additions and 3 deletions
59
examples/schedule-prom-metrics/main.go
Normal file
59
examples/schedule-prom-metrics/main.go
Normal file
|
@ -0,0 +1,59 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/sherifabdlnaby/sched"
|
||||
"github.com/uber-go/tally"
|
||||
"github.com/uber-go/tally/prometheus"
|
||||
"log"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
promReporter := prometheus.NewReporter(prometheus.Options{})
|
||||
promMterics, closer := tally.NewRootScope(tally.ScopeOptions{
|
||||
Tags: map[string]string{},
|
||||
CachedReporter: promReporter,
|
||||
Separator: prometheus.DefaultSeparator,
|
||||
}, 1*time.Second)
|
||||
defer closer.Close()
|
||||
|
||||
fixedEvery5s, err := sched.NewFixed(5 * time.Second)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("invalid interval: %s", err.Error()))
|
||||
}
|
||||
|
||||
job := func() {
|
||||
log.Println("Doing some work for random time...")
|
||||
time.Sleep(time.Duration(int(rand.Int63n(50)+1)*100) * time.Millisecond)
|
||||
log.Println("Finished Work.")
|
||||
}
|
||||
|
||||
// Create Schedule
|
||||
schedule := sched.NewSchedule("every5s", fixedEvery5s, job, sched.WithLogger(sched.DefaultLogger()),
|
||||
sched.WithMetrics(promMterics))
|
||||
|
||||
// Start Schedule
|
||||
schedule.Start()
|
||||
|
||||
// Star Prom Server
|
||||
http.Handle("/metrics", promReporter.HTTPHandler())
|
||||
go http.ListenAndServe(":8080", nil)
|
||||
log.Println("Prometheus Metrics at :8080/metrics")
|
||||
|
||||
// Listen to CTRL + C And indefintly wait shutdown.
|
||||
signalChan := make(chan os.Signal, 1)
|
||||
signal.Notify(signalChan, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT)
|
||||
_ = <-signalChan
|
||||
|
||||
// Stop before shutting down.
|
||||
schedule.Stop()
|
||||
|
||||
return
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue