示例#1
0
文件: fdb.go 项目: hugoh/complaints
// Look for new flights that we should add to our database. Invoked by cron.
func scanHandler(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)

	if db, err1 := fdb24.NewFlightDBFr24(urlfetch.Client(c)); err1 != nil {
		c.Errorf(" /mdb/scan: newdb: %v", err1)
		http.Error(w, err1.Error(), http.StatusInternalServerError)

	} else {
		if flights, err2 := db.LookupList(sfo.KBoxSFO120K); err2 != nil {
			c.Errorf(" /mdb/scan: lookup: %v", err2)
			http.Error(w, err2.Error(), http.StatusInternalServerError)
		} else {

			set := ftype.FIFOSet{}
			if err3 := loadFIFOSet(c, &set); err3 != nil {
				c.Errorf(" /mdb/scan: loadcache: %v", err3)
				http.Error(w, err3.Error(), http.StatusInternalServerError)
			}
			new := set.FindNew(flights)
			if err4 := saveFIFOSet(c, set); err4 != nil {
				c.Errorf(" /mdb/scan: savecache: %v", err4)
				http.Error(w, err4.Error(), http.StatusInternalServerError)
			}

			// Enqueue the new flights
			n := 1000
			for i, fs := range new {
				if i >= n {
					break
				}
				if fsStr, err5 := fs.Base64Encode(); err5 != nil {
					http.Error(w, err5.Error(), http.StatusInternalServerError)
					return
				} else {
					url := fmt.Sprintf("/fdb/addflight?deb=%s", fs.F.UniqueIdentifier())
					t := taskqueue.NewPOSTTask(url, map[string][]string{
						"flightsnapshot": {fsStr},
					})

					// We could be smarter about this.
					t.Delay = time.Minute * 45

					if _, err6 := taskqueue.Add(c, t, "addflight"); err6 != nil {
						c.Errorf(" /mdb/scan: enqueue: %v", err6)
						http.Error(w, err6.Error(), http.StatusInternalServerError)
						return
					}
				}
			}

			var params = map[string]interface{}{
				"New":     new,
				"Flights": flights,
			}
			if err7 := templates.ExecuteTemplate(w, "fdb-scan", params); err7 != nil {
				http.Error(w, err7.Error(), http.StatusInternalServerError)
			}
		}
	}
}
示例#2
0
文件: fdb.go 项目: skypies/flightdb
func saveFIFOSet(c context.Context, s ftype.FIFOSet) error {
	s.AgeOut(time.Minute * kFIFOSetMaxAgeMins)
	item := memcache.Item{Key: kMemcacheFIFOSetKey, Object: s}
	if err := memcache.Gob.Set(c, &item); err != nil {
		log.Errorf(c, "error setting item: %v", err)
	}
	return nil
}