예제 #1
0
/*
 * The Core functionality of this application.
 * - Get RK Activities since timestamp
 * - Get STV Activities since timestamp
 * - compare and calc difference
 * - store in STV
 * - update timestamp for record
 */
func startSync() {
	repo := sync.CreateSyncDbRepo(DbConnectionString)
	err := repo.CreateTableIfNotExist()
	if err != nil {
		log.Fatal("Error checking or creating the Sync database table: %s", err)
	}

	allSyncs, err := repo.RetrieveAllSyncTasks()
	log.Printf("Retrieved %d sync tasks", len(allSyncs))
	if err != nil {
		//retrieval failed, we log and return
		log.Print("ERROR: error retrieving Sync Tasks, db down?")
		return
	}
	for _, syncer := range allSyncs {
		//que SyncTask for worker
		log.Printf("Now syncing for task: %s, %s, %s", syncer.StravaToken, syncer.RunkeeperToken, time.Unix(int64(syncer.LastSeenTimestamp), 0))
		err := queueSyncTask(syncer)
		if err != nil {
			log.Fatal("Error enqueuing job for sync: %s", err)
		}

		//We might need to move this to the SyncTask itself now that we run on workers.
		log.Print("Updating last seen timestamp")
		//subtract 45 minutes to prevent activites being missed
		syncer.LastSeenTimestamp = int(time.Now().Add(time.Duration(tsDelta) * time.Minute).Unix())
		rowsUpdated, err := repo.UpdateSyncTask(syncer)
		if err != nil || rowsUpdated != 1 {
			log.Fatal("Error updating the SyncTask record with a new timestamp")
		}
	}
}
예제 #2
0
func main() {
	//Load config vars
	var err error
	DbConnectionString = os.Getenv("CLEARDB_DATABASE_URL")
	dbURL := os.Getenv("DATABASE_URL")

	log.Printf("Starting SyncMySport-trigger with config: MysqlDBString: %s, PostgresDBString: %s",
		DbConnectionString, dbURL)

	success := false
	i := 0
	for i < 10 && !success {
		pgxpool, qc, err = shared.Setup(dbURL)
		if err != nil {
			if i == 4 {
				log.WithField("DATABASE_URL", dbURL).Fatal("Unable to setup queue / database")
			} else {
				log.Print("Waiting 1 second for retry. I = %d", i)
				time.Sleep(1000 * time.Millisecond)
				i++
			}
		} else {
			success = true
		}
	}

	defer pgxpool.Close()
	startSync()
}