// getOfferData gather all data for offer and return filled object to channel func getOfferData(dbConn *db.DB, offerId int, specialsMap db.SpecialsMap, offersChan chan *xml.Offer) { var ( err error ) offer := db.NewOffer(dbConn, offerId) utils.LogErrf(offer.Get(), "Get offer (%d)", offerId) // contact and owner data personId := offer.StringAt(db.ID_WPROWADZAJACEGO) contactId := offer.StringAt(db.ID_WLASCICIELA) person := db.NewPerson(dbConn) utils.LogErrf(person.Get(personId), "Get person (%s) for offer (%d)", personId, offerId) contact := db.NewPerson(dbConn) utils.LogErrf(contact.Get(contactId), "Get contact (%s) for offer (%d)", personId, offerId) // additionals data add := db.NewAdditional(dbConn) utils.LogErrf(add.Get(offerId), "Get additional data for offer (%d)", offerId) // xmlOffer := fillOffer(offer, add, person, contact) sid := offer.StrId() var imgIds []string // images and specials images := db.NewImages(dbConn, sid) if imgIds, err = images.FileNames(); err != nil { utils.LogErrf(err, "Images ids for offer (%d)", offerId) } go getImages(imagesChan, images, workDir) xmlOffer.Pictures = xml.NewListElem("zdjecia", "zdjecie") xmlOffer.Pictures.AddMany(imgIds...) xmlOffer.Specials = xml.NewListElem("wyroznienia", "wyroznienie") xmlOffer.Specials.AddMany(specialsMap[sid]...) offersChan <- xmlOffer }
func dumpAsXML(conf *config) (count int, err error) { workDir := conf.workDir exportId := fmt.Sprintf("%s_00.xml", time.Now().Format(DateFormat)) exportXMLName := filepath.Join(workDir, exportId) // sync channels imagesChan = make(chan error) inOffersChan := make(chan *db.Offer, 5) outOffersChan := make(chan *xml.Offer) // connect to db dbConn := &db.DB{} if err = dbConn.Connect(DbUser, DbPass, DbHost, DbName); err != nil { err = utils.Errorf("Database connection %v", err) return } defer dbConn.Close() // specials are common for all offers specials := db.NewSpecials(dbConn) if err = specials.GetAll(); err != nil { return } specialsMap := specials.AsMap() // start workers for i := 0; i < noOfWorkers; i++ { go worker(inOffersChan, outOffersChan, specialsMap) } // start getting offers offers := db.NewOffers(dbConn) if err = offers.GetAllIds(); err != nil { return } go func() { for _, offerId := range offers.Ids() { offer := db.NewOffer(dbConn, offerId) inOffersChan <- offer } }() step := 25 total := len(offers.Ids()) counter := 0 // collect offers from workers xmlOffers := xml.NewOffers() for range offers.Ids() { xmlOffer := <-outOffersChan xmlOffers.Add(xmlOffer) counter += 1 if (counter % step) == 0 { log.Printf("Completed %d/%d\n", counter, total) } } log.Printf("Completed %d/%d\n", counter, total) log.Printf("Wait for images\n") // wait for all images finish download for range offers.Ids() { <-imagesChan } system := xml.NewSystem("1", "date", "hour", "www") head := xml.NewHeader(system, xmlOffers) if err = xml.Write(head, exportXMLName); err != nil { return } if err = ioutil.WriteFile(filepath.Join(workDir, "last.id"), []byte(exportId), os.ModePerm); err != nil { return } count = len(offers.Ids()) return }