master
cheetah 2 years ago
parent 34207a199c
commit f004a8eb3e

@ -160,7 +160,7 @@ func main() {
} }
} }
func undoBatch(undoBatch *bool, gmaID string, fileIDs []string, gma2FileIDs []string) (err error) { func undoBatch(undoBatch bool, gmaID string, fileIDs []string, gma2FileIDs []string) (err error) {
fmt.Printf("undoBatch(%x, %s)\n", undoBatch, gmaID) fmt.Printf("undoBatch(%x, %s)\n", undoBatch, gmaID)
_, err = colGMA.RemoveDocument(arangoCTX, gmaID) _, err = colGMA.RemoveDocument(arangoCTX, gmaID)
if err != nil { if err != nil {
@ -178,9 +178,8 @@ func undoBatch(undoBatch *bool, gmaID string, fileIDs []string, gma2FileIDs []st
} }
func ProcessGMA(filePath string) (err error) { func ProcessGMA(filePath string) (err error) {
var ( var (
fileIDs []string fileIDs []string
gma2FileIDs []string gma2FileIDs []string
failedProcessing = true
) )
dboGMA := common.DB_GMA{} dboGMA := common.DB_GMA{}
dboGMA.BatchID = uuid.NewV4().String() // use this for rapid unscheduled dissassembly dboGMA.BatchID = uuid.NewV4().String() // use this for rapid unscheduled dissassembly
@ -314,12 +313,11 @@ func ProcessGMA(filePath string) (err error) {
dboGMA.ProcessingEnd = time.Now() dboGMA.ProcessingEnd = time.Now()
dboGMA.ProcessingDuration = dboGMA.ProcessingEnd.Sub(dboGMA.ProcessingStart).Milliseconds() dboGMA.ProcessingDuration = dboGMA.ProcessingEnd.Sub(dboGMA.ProcessingStart).Milliseconds()
// if anything fails, lets undo the documents we imported
defer undoBatch(&failedProcessing, dboGMA.ID, fileIDs, gma2FileIDs)
// TODO: Calculate dboGMA.OptimizedSize // TODO: Calculate dboGMA.OptimizedSize
dboGMA.OptimizedSize = 0 dboGMA.OptimizedSize = 0
_, err = colGMA.CreateDocument(arangoCTX, dboGMA) _, err = colGMA.CreateDocument(arangoCTX, dboGMA)
if err != nil { if err != nil {
undoBatch(true, dboGMA.ID, fileIDs, gma2FileIDs)
return err return err
} }
@ -354,6 +352,7 @@ func ProcessGMA(filePath string) (err error) {
for _, createError := range errorSlice { for _, createError := range errorSlice {
if createError != nil && strings.Contains(createError.Error(), "unique constraint violated - in index primary of type primary over '_key'") { if createError != nil && strings.Contains(createError.Error(), "unique constraint violated - in index primary of type primary over '_key'") {
} else if createError != nil { } else if createError != nil {
undoBatch(true, dboGMA.ID, fileIDs, gma2FileIDs)
return createError return createError
} }
} }
@ -382,6 +381,7 @@ func ProcessGMA(filePath string) (err error) {
Complete: true, // will make it fail if any error occurs (and hopefully reverse the trans-action) Complete: true, // will make it fail if any error occurs (and hopefully reverse the trans-action)
}) })
if err != nil { if err != nil {
undoBatch(true, dboGMA.ID, fileIDs, gma2FileIDs)
return fmt.Errorf("ImportDocuments File fail: %v", err) return fmt.Errorf("ImportDocuments File fail: %v", err)
} }
//fmt.Printf("Code: %d, Created: %d, Ignored: %d, Errors: %d", statsImportGMA2File.Code, statsImportGMA2File.Created, statsImportGMA2File.Ignored, statsImportGMA2File.Errors) //fmt.Printf("Code: %d, Created: %d, Ignored: %d, Errors: %d", statsImportGMA2File.Code, statsImportGMA2File.Created, statsImportGMA2File.Ignored, statsImportGMA2File.Errors)
@ -397,6 +397,7 @@ func ProcessGMA(filePath string) (err error) {
//fmt.Printf("Uploading %s (local %s) to Storage\n", dboGMA2File.UploadID, dboGMA2File.LocalFileName) //fmt.Printf("Uploading %s (local %s) to Storage\n", dboGMA2File.UploadID, dboGMA2File.LocalFileName)
err = common.MultipartUpload(httpClient, fmt.Sprintf("http://127.0.0.1:13371/stash/%s/%d", dboGMA2File.UploadID, dboGMA2File.FileSize), dboGMA2File.LocalFileName) err = common.MultipartUpload(httpClient, fmt.Sprintf("http://127.0.0.1:13371/stash/%s/%d", dboGMA2File.UploadID, dboGMA2File.FileSize), dboGMA2File.LocalFileName)
if err != nil { if err != nil {
undoBatch(true, dboGMA.ID, fileIDs, gma2FileIDs)
return err return err
} }
} }
@ -411,11 +412,13 @@ func ProcessGMA(filePath string) (err error) {
err := os.MkdirAll(dir, os.ModePerm) err := os.MkdirAll(dir, os.ModePerm)
if err != nil { if err != nil {
undoBatch(true, dboGMA.ID, fileIDs, gma2FileIDs)
return err return err
} }
gmaWriter, err := gma.NewWriter(destPath) gmaWriter, err := gma.NewWriter(destPath)
if err != nil { if err != nil {
undoBatch(true, dboGMA.ID, fileIDs, gma2FileIDs)
return err return err
} }
defer gmaWriter.Close() defer gmaWriter.Close()
@ -423,10 +426,12 @@ func ProcessGMA(filePath string) (err error) {
//fmt.Printf("Writing Header with FormatVersion: %d\n", dboGMA.Header.FormatVersion) //fmt.Printf("Writing Header with FormatVersion: %d\n", dboGMA.Header.FormatVersion)
err = gmaWriter.WriteHeader(dboGMA.Header) err = gmaWriter.WriteHeader(dboGMA.Header)
if err != nil { if err != nil {
undoBatch(true, dboGMA.ID, fileIDs, gma2FileIDs)
return err return err
} }
err = gmaWriter.WriteFirstType(dboGMA.FirstType) err = gmaWriter.WriteFirstType(dboGMA.FirstType)
if err != nil { if err != nil {
undoBatch(true, dboGMA.ID, fileIDs, gma2FileIDs)
return err return err
} }
@ -435,6 +440,7 @@ func ProcessGMA(filePath string) (err error) {
//fmt.Printf("WriteFileIndex for %s number %d\n", dboGMA2File.FileName, dboGMA2File.FileNumber) //fmt.Printf("WriteFileIndex for %s number %d\n", dboGMA2File.FileName, dboGMA2File.FileNumber)
err = gmaWriter.WriteFileIndex(dboGMA2File.FileName, dboGMA2File.FileSize, dboGMA2File.CRC, dboGMA2File.NextType) err = gmaWriter.WriteFileIndex(dboGMA2File.FileName, dboGMA2File.FileSize, dboGMA2File.CRC, dboGMA2File.NextType)
if err != nil { if err != nil {
undoBatch(true, dboGMA.ID, fileIDs, gma2FileIDs)
return err return err
} }
} }
@ -446,11 +452,13 @@ func ProcessGMA(filePath string) (err error) {
fmt.Printf("WriteFile for %s number %d = %s\n", dboGMA2File.FileName, dboGMA2File.FileNumber, dboGMA2File.UploadID) fmt.Printf("WriteFile for %s number %d = %s\n", dboGMA2File.FileName, dboGMA2File.FileNumber, dboGMA2File.UploadID)
resp, err := httpClient.Get(fmt.Sprintf("http://127.0.0.1:13371/fetch/%s", dboGMA2File.UploadID)) resp, err := httpClient.Get(fmt.Sprintf("http://127.0.0.1:13371/fetch/%s", dboGMA2File.UploadID))
if err != nil { if err != nil {
undoBatch(true, dboGMA.ID, fileIDs, gma2FileIDs)
return err return err
} }
defer resp.Body.Close() defer resp.Body.Close()
err = gmaWriter.WriteFile(resp.Body) err = gmaWriter.WriteFile(resp.Body)
if err != nil { if err != nil {
undoBatch(true, dboGMA.ID, fileIDs, gma2FileIDs)
return err return err
} }
} }
@ -461,6 +469,7 @@ func ProcessGMA(filePath string) (err error) {
gmaWriter.FileHandle.Seek(0, 0) gmaWriter.FileHandle.Seek(0, 0)
writeHash, err := gmaWriter.GetSHA256() writeHash, err := gmaWriter.GetSHA256()
if err != nil { if err != nil {
undoBatch(true, dboGMA.ID, fileIDs, gma2FileIDs)
return err return err
} }
@ -469,15 +478,18 @@ func ProcessGMA(filePath string) (err error) {
fmt.Println() fmt.Println()
writeStat, err := os.Stat(destPath) writeStat, err := os.Stat(destPath)
if err != nil { if err != nil {
undoBatch(true, dboGMA.ID, fileIDs, gma2FileIDs)
return err return err
} }
writeSize := writeStat.Size() writeSize := writeStat.Size()
if writeSize != dboGMA.GMASize { if writeSize != dboGMA.GMASize {
//fail //fail
undoBatch(true, dboGMA.ID, fileIDs, gma2FileIDs)
return fmt.Errorf("RewriteCheck failed, original=%s (%d bytes), rewrite=%s (%d bytes)", dboGMA.GMAHash, dboGMA.GMASize, writeHash, writeSize) return fmt.Errorf("RewriteCheck failed, original=%s (%d bytes), rewrite=%s (%d bytes)", dboGMA.GMAHash, dboGMA.GMASize, writeHash, writeSize)
} }
if writeHash != dboGMA.GMAHash { if writeHash != dboGMA.GMAHash {
//fail //fail
undoBatch(true, dboGMA.ID, fileIDs, gma2FileIDs)
return fmt.Errorf("RewriteCheck failed, original=%s (%d bytes), rewrite=%s (%d bytes)", dboGMA.GMAHash, dboGMA.GMASize, writeHash, writeSize) return fmt.Errorf("RewriteCheck failed, original=%s (%d bytes), rewrite=%s (%d bytes)", dboGMA.GMAHash, dboGMA.GMASize, writeHash, writeSize)
} }
} }
@ -489,9 +501,9 @@ func ProcessGMA(filePath string) (err error) {
_, err = colGMA.UpdateDocument(arangoCTX, dboGMA.ID, dboGMA) _, err = colGMA.UpdateDocument(arangoCTX, dboGMA.ID, dboGMA)
if err != nil { if err != nil {
undoBatch(true, dboGMA.ID, fileIDs, gma2FileIDs)
return err return err
} }
0
failedProcessing = false
return nil return nil
} }

Loading…
Cancel
Save