Пример #1
0
// Update the database if the lastPullTime is more than 6 hours before the current time
func updateDB(db *sql.DB, ctx context.Context, lastPullTime int64) int64 {
	// If the last pull was more than 6 hours ago
	if lastPull < time.Now().Add(-1*timeout).Unix() {
		log.Infof(ctx, "Updating database")

		// Remove deleted questions from the database
		log.Infof(ctx, "Removing deleted questions from db")
		if err := backend.RemoveDeletedQuestions(db, ctx); err != nil {
			log.Warningf(ctx, "Error removing deleted questions: %v", err.Error())
			return lastPullTime
		}

		// Setting time frame to get new questions.
		toDate := time.Now()
		fromDate := time.Unix(lastPull, 0)

		// Collect new questions from SO
		questions, err := backend.GetNewQns(fromDate, toDate)
		if err != nil {
			log.Warningf(ctx, "Error getting new questions: %v", err.Error())
			return lastPullTime
		}

		// Add new questions to database
		log.Infof(ctx, "Adding new questions to db")
		if err := backend.AddQuestions(db, ctx, questions); err != nil {
			log.Warningf(ctx, "Error adding new questions: %v", err.Error())
			return lastPullTime
		}

		lastPullTime = time.Now().Unix()
		log.Infof(ctx, "New questions added")
	}
	return lastPullTime
}
Пример #2
0
//The app engine will run its own main function and imports this code as a package
//So no main needs to be defined
//All routes go in to init
func init() {

	recentChangedQns = []string{}

	// Initialising stackongo session
	backend.NewSession()

	// goroutine to collect the questions from SO and add them to the database
	go func(db *sql.DB) {
		// Iterate over ([SPECIFIED DURATION])
		for _ = range time.NewTicker(timeout).C {
			toDate := time.Now()
			fromDate := toDate.Add(-1 * timeout)
			// Collect new questions from SO
			log.Println("Getting new questions")
			questions, err := backend.GetNewQns(fromDate, toDate)
			if err != nil {
				log.Printf("Error getting new questions: %v", err.Error())
				continue
			}

			log.Println("Adding questions to db")
			// Add new questions to database
			if err = backend.AddQuestions(db, questions); err != nil {
				log.Printf("Error updating database: %v", err.Error())
				continue
			}
		}
	}(db)

	log.Println("Initial cache download")
	initCacheDownload()

	// // goroutine to update local cache if there has been any change to database
	// count := 1
	// go func(count int) {
	// 	for {
	// 		if checkDBUpdateTime("questions", mostRecentUpdate) {
	// 			log.Printf("Refreshing cache %v", count)
	// 			refreshLocalCache()
	// 			count++
	// 		}
	// 	}
	// }(count)

	http.HandleFunc("/login", authHandler)
	http.HandleFunc("/", handler)
	http.HandleFunc("/tag", handler)
	http.HandleFunc("/user", handler)
	http.HandleFunc("/viewTags", handler)
	http.HandleFunc("/viewUsers", handler)
	http.HandleFunc("/userPage", handler)
	http.HandleFunc("/dbUpdated", updateHandler)
	http.HandleFunc("/search", handler)
}