示例#1
0
func processResults(dbMan db.DatabaseManager) {
	for {
		log.Println("waiting for job")
		job := WorkerPool.WaitForJob()
		if job == nil {
			backoff += 5 // add 5 seconds every time there are no jobs to process
			time.Sleep(backoff * time.Second)
			continue
		} else {
			backoff = 5 // reset backoff
			log.Println("got job")
		}

		if job.Result == nil {
			log.Println("job result in nil:", job.Err)
		} else {
			result := job.Result.(feedFetchResult)
			if result.err != nil {
				log.Println("got error: ", result.err)
			} else {
				if job.Args[0].(string) == "kill" {
					break
				}
				url := job.Args[0].(string)
				// send out notifications
				ids := dbMan.GetSubscribers(url)
				for _, id := range ids {
					log.Print("send notification to: ", id, result.feed)
				}
				log.Printf("found: %s %q\n", url, result.feed)
			}
		}
	}
}
示例#2
0
// GetFeedByID returns a feed by it's guid
func GetFeedByID(dbManager db.DatabaseManager) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		feedID := r.URL.Query().Get("id")
		// get from db
		dbManager.GetFeed(feedID)
	})
}
示例#3
0
// AddFeed add a feed by its url
func AddFeed(dbManager db.DatabaseManager) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		// get raw via http get, parse, then add into db
		decoder := json.NewDecoder(r.Body)
		var reqPayload subscribeStruct
		err := decoder.Decode(&reqPayload)
		// TODO: validate url is an actual http address and nothing else, maybe regex?
		if err != nil {
			// throw some error
			log.Println(err)
			sendError(w, "Error: I don't understand your url :(", http.StatusInternalServerError)
		} else {
			log.Println(reqPayload.URL)
			if dbManager.SubscribeToFeed(reqPayload.GcmClientID, reqPayload.URL) {
				// TODO: send push notification when this feed is available
				WorkerPool.Add(retrieveFeed, reqPayload.URL, reqPayload.GcmClientID)
				w.WriteHeader(http.StatusAccepted)
			} else {
				log.Println("user already subscribed")
				sendError(w, "Error: user already subscribed", http.StatusConflict)
			}
		}
	})
}