diff --git a/controllers/ImportController.go b/controllers/ImportController.go index 0cee275..f5db534 100644 --- a/controllers/ImportController.go +++ b/controllers/ImportController.go @@ -13,6 +13,7 @@ import ( "os" "strconv" "sync" + "sync/atomic" "time" "gorm.io/driver/mysql" @@ -26,9 +27,25 @@ var ( families []gm.AttributeFamily sellers = make(map[string]gm.MarketplaceSeller) AttributesMap = make(map[string]gm.Attribute) + locker = stateUnlocked +) + +const ( + stateUnlocked uint32 = iota + stateLocked ) func StartImport(w http.ResponseWriter, route *http.Request) { + // lock the request + if !atomic.CompareAndSwapUint32(&locker, stateUnlocked, stateLocked) { + w.WriteHeader(http.StatusTooManyRequests) + json.NewEncoder(w).Encode(map[string]string{ + "msg": "Scraper or Updater in progress!", + }) + + return + } + defer atomic.StoreUint32(&locker, stateUnlocked) start := time.Now() r := new(big.Int)