2022-09-27 13:53:17 +00:00
|
|
|
package controller
|
2022-10-03 06:31:28 +00:00
|
|
|
|
|
|
|
|
import (
|
2022-10-08 11:10:57 +00:00
|
|
|
gm "db_service/gorm_models"
|
2022-10-03 06:31:28 +00:00
|
|
|
"db_service/repositories"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
"log"
|
|
|
|
|
"math/big"
|
|
|
|
|
"net/http"
|
|
|
|
|
"sync/atomic"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func StartProductImport(w http.ResponseWriter, _ *http.Request) {
|
|
|
|
|
if !atomic.CompareAndSwapUint32(&locker, stateUnlocked, stateLocked) {
|
|
|
|
|
w.WriteHeader(http.StatusTooManyRequests)
|
|
|
|
|
err := json.NewEncoder(w).Encode(map[string]string{
|
|
|
|
|
"msg": "Product import in progress!",
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
log.Println(err)
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
defer atomic.StoreUint32(&locker, stateUnlocked)
|
|
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
|
|
r := new(big.Int)
|
|
|
|
|
fmt.Println("start import", r.Binomial(1000, 10))
|
|
|
|
|
|
|
|
|
|
importRepo, err := repositories.ImporterInstance()
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
//wait until initialization data is loaded to memory
|
|
|
|
|
importRepo.ImportWGroup.Wait()
|
|
|
|
|
|
|
|
|
|
if err = importRepo.Start().Error; err != nil {
|
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2022-10-08 11:10:57 +00:00
|
|
|
elapsed := time.Since(start)
|
|
|
|
|
log.Printf("end import took %s", elapsed)
|
2022-10-03 06:31:28 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
importRepo.ImportWGroup.Wait()
|
2023-01-19 11:32:55 +00:00
|
|
|
//todo delete galan wishlist
|
2022-10-03 06:31:28 +00:00
|
|
|
|
2022-10-08 11:10:57 +00:00
|
|
|
//scout index flush
|
|
|
|
|
if err = gm.Flush(); err != nil {
|
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
|
elapsed := time.Since(start)
|
|
|
|
|
log.Printf("end import took %s", elapsed)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-03 06:31:28 +00:00
|
|
|
elapsed := time.Since(start)
|
|
|
|
|
log.Printf("end import took %s", elapsed)
|
|
|
|
|
http.Error(w, fmt.Sprintf("end import took %s", elapsed), http.StatusOK)
|
|
|
|
|
}
|