func convertPrograms(db data.Data) error { log.Printf("Allocating %d goroutines", maxGoroutines) var programs []model.AcademicProgram programIds, err := db.GetProgramsList() if err != nil { log.Print(err) return err } log.Printf("Fetched %d Program Ids to convert\n", len(programIds)) queue := make(chan string, len(programIds)) done := make(chan data.Program) for i := 0; i < maxGoroutines; i += 1 { go loadAndConvert(db, queue, done) } for _, programId := range programIds { queue <- programId } //wait for goroutines to finish for i := 0; i < len(programIds); i += 1 { program := <-done programs = append(programs, program.AcademicProgram) } log.Println("Converting program list") list, err := convert.ConvertList(programs) if err != nil { log.Print(err) return err } writeProgramList(list) return nil }
func loadAndConvert(data data.Data, programIds chan string, done chan data.Program) { for { select { case programId := <-programIds: log.Printf("%v", programId) program, err := data.GetProgram(programId) if err != nil { log.Fatal(err) return } conv, err := convert.Convert(program) if err != nil { log.Fatal(err) } out, err := json.Marshal(conv) if err != nil { log.Fatal(err) } persistToStorage(programId, out) persistToStorage(conv.ProgramCode, out) done <- program } } }