mirror of https://github.com/matrix-org/go-neb.git
Browse Source
Get polling working
Get polling working
Currently never stops polling so clobbering service IDs does the wrong thing, and deleting services doesn't stop polling at all!pull/76/head
Kegan Dougal
8 years ago
4 changed files with 124 additions and 9 deletions
-
6src/github.com/matrix-org/go-neb/goneb.go
-
44src/github.com/matrix-org/go-neb/polling/polling.go
-
61src/github.com/matrix-org/go-neb/services/rss/rss.go
-
22src/github.com/matrix-org/go-neb/types/types.go
@ -0,0 +1,44 @@ |
|||
package polling |
|||
|
|||
import ( |
|||
log "github.com/Sirupsen/logrus" |
|||
"github.com/matrix-org/go-neb/database" |
|||
"github.com/matrix-org/go-neb/types" |
|||
"time" |
|||
) |
|||
|
|||
var shouldPoll = make(map[string]bool) // Service ID => yay/nay
|
|||
|
|||
// Start polling already existing services
|
|||
func Start() error { |
|||
log.Print("Start polling") |
|||
// Work out which service types require polling
|
|||
for serviceType, poller := range types.PollersByType() { |
|||
if poller == nil { |
|||
continue |
|||
} |
|||
// Query for all services with said service type
|
|||
srvs, err := database.GetServiceDB().LoadServicesByType(serviceType) |
|||
if err != nil { |
|||
return err |
|||
} |
|||
for _, s := range srvs { |
|||
shouldPoll[s.ServiceID()] = true |
|||
go StartPolling(s, poller) |
|||
} |
|||
} |
|||
return nil |
|||
} |
|||
|
|||
// StartPolling begins the polling loop for this service. Does not return, so call this
|
|||
// as a goroutine!
|
|||
func StartPolling(service types.Service, poller types.Poller) { |
|||
for { |
|||
if !shouldPoll[service.ServiceID()] { |
|||
log.WithField("service_id", service.ServiceID()).Info("Terminating poll.") |
|||
break |
|||
} |
|||
poller.OnPoll(service) |
|||
time.Sleep(time.Duration(poller.IntervalSecs()) * time.Second) |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue