monitor writes to db
Former-commit-id: 5aa70ad9d835c607abb1c19dc620f6c45d31a866master
parent
5e87f1659d
commit
093d468f87
5
main.go
5
main.go
|
|
@ -27,7 +27,10 @@ func main() {
|
|||
}
|
||||
go InterruptAfter(s.Run, sigc)
|
||||
|
||||
m := monitor.New()
|
||||
m, err := monitor.New()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
go InterruptAfter(m.Run, sigc)
|
||||
|
||||
signal.Notify(sigc,
|
||||
|
|
|
|||
Binary file not shown.
|
|
@ -12,12 +12,13 @@ type Monitor struct {
|
|||
config.Stoppable
|
||||
}
|
||||
|
||||
func New() *Monitor {
|
||||
func New() (*Monitor, error) {
|
||||
q, err := newQueue()
|
||||
return &Monitor{
|
||||
queue: newQueue(),
|
||||
queue: q,
|
||||
Incoming: make(chan *Item),
|
||||
Outgoing: make(chan *Item),
|
||||
}
|
||||
}, err
|
||||
}
|
||||
|
||||
func (m *Monitor) Run() error {
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ func TestMonitorNew(t *testing.T) {
|
|||
func TestMonitorTrigger(t *testing.T) {
|
||||
initMonitor()
|
||||
|
||||
m := New()
|
||||
m, _ := New()
|
||||
i, _ := NewItem("MonitorTrigger", time.Second)
|
||||
i.setLast(never)
|
||||
if time.Now().After(i.Last()) {
|
||||
|
|
@ -46,7 +46,7 @@ func TestMonitorTrigger(t *testing.T) {
|
|||
func TestMonitorTriggered(t *testing.T) {
|
||||
initMonitor()
|
||||
|
||||
m := New()
|
||||
m, _ := New()
|
||||
i, _ := NewItem("MonitorTriggered", time.Second)
|
||||
i.setLast(time.Now().Add(time.Hour * -1))
|
||||
m.queue.Push(i)
|
||||
|
|
@ -64,7 +64,7 @@ func TestMonitorTriggered(t *testing.T) {
|
|||
func TestMonitorEnqueued(t *testing.T) {
|
||||
initMonitor()
|
||||
|
||||
m := New()
|
||||
m, _ := New()
|
||||
m.Incoming = make(chan *Item, 1)
|
||||
m.Incoming <- &Item{Key: "MonitorEnqueued"}
|
||||
|
||||
|
|
@ -81,8 +81,8 @@ func TestMonitorEnqueued(t *testing.T) {
|
|||
func TestMonitorEnqueue(t *testing.T) {
|
||||
initMonitor()
|
||||
|
||||
m := New()
|
||||
m.enqueue(nil)
|
||||
m, _ := New()
|
||||
m.enqueue(&Item{})
|
||||
|
||||
if m.queue.Len() != 1 {
|
||||
t.Error(m.queue.Len())
|
||||
|
|
@ -92,7 +92,7 @@ func TestMonitorEnqueue(t *testing.T) {
|
|||
func TestMonitorRun(t *testing.T) {
|
||||
initMonitor()
|
||||
|
||||
m := New()
|
||||
m, _ := New()
|
||||
m.Outgoing = make(chan *Item, 1)
|
||||
go m.Run()
|
||||
defer config.Values().Can()
|
||||
|
|
|
|||
|
|
@ -1,19 +1,55 @@
|
|||
package monitor
|
||||
|
||||
import "github.com/golang-collections/go-datastructures/queue"
|
||||
import (
|
||||
"local/rssmon3/config"
|
||||
"log"
|
||||
|
||||
"github.com/golang-collections/go-datastructures/queue"
|
||||
)
|
||||
|
||||
const nsQueued = "nsQueued"
|
||||
|
||||
type Queue struct {
|
||||
queue *queue.PriorityQueue
|
||||
}
|
||||
|
||||
func newQueue() *Queue {
|
||||
return &Queue{
|
||||
queue: queue.NewPriorityQueue(1),
|
||||
func newQueue() (*Queue, error) {
|
||||
q := queue.NewPriorityQueue(1)
|
||||
db := config.Values().DB
|
||||
keys, err := db.List([]string{nsQueued})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, key := range keys {
|
||||
b, err := db.Get(key, nsQueued)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
i := &Item{}
|
||||
if err := i.Decode(b); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
q.Put(i)
|
||||
}
|
||||
return &Queue{
|
||||
queue: q,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (q *Queue) Push(i *Item) {
|
||||
if i == nil {
|
||||
return
|
||||
}
|
||||
q.queue.Put(i)
|
||||
b, err := i.Encode()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
if err := config.Values().DB.Set(i.Key, b, nsQueued); err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (q *Queue) Pop() *Item {
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ func initQueue() {
|
|||
func TestQueue(t *testing.T) {
|
||||
initQueue()
|
||||
|
||||
q := newQueue()
|
||||
q, _ := newQueue()
|
||||
|
||||
if i := q.Peek(); i != nil {
|
||||
t.Error(i)
|
||||
|
|
@ -33,7 +33,7 @@ func TestQueue(t *testing.T) {
|
|||
func TestQueuePriority(t *testing.T) {
|
||||
initQueue()
|
||||
|
||||
q := newQueue()
|
||||
q, _ := newQueue()
|
||||
|
||||
i, _ := NewItem("iQueuePriority", time.Second)
|
||||
j, _ := NewItem("jQueuePriority", time.Hour)
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
0d46529517022e32f10ee1e18ee21760de34fecf
|
||||
cc79df3e2bb33d684975d23cd008eae93e7dffc2
|
||||
|
|
@ -105,4 +105,5 @@ func (s *Server) feed(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
log.Println(i)
|
||||
s.error(w, r, errors.New("not impl"))
|
||||
// TODO
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue