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)
_, err = colGMA.RemoveDocument(arangoCTX, gmaID)
if err != nil {
@ -178,9 +178,8 @@ func undoBatch(undoBatch *bool, gmaID string, fileIDs []string, gma2FileIDs []st
}
func ProcessGMA(filePath string) (err error) {
var (
fileIDs []string
gma2FileIDs []string
failedProcessing = true
fileIDs []string
gma2FileIDs []string
)
dboGMA := common.DB_GMA{}
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.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
dboGMA.OptimizedSize = 0
_, err = colGMA.CreateDocument(arangoCTX, dboGMA)
if err != nil {
undoBatch(true, dboGMA.ID, fileIDs, gma2FileIDs)
return err
}
@ -354,6 +352,7 @@ func ProcessGMA(filePath string) (err error) {
for _, createError := range errorSlice {
if createError != nil && strings.Contains(createError.Error(), "unique constraint violated - in index primary of type primary over '_key'") {
} else if createError != nil {
undoBatch(true, dboGMA.ID, fileIDs, gma2FileIDs)
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)
})
if err != nil {
undoBatch(true, dboGMA.ID, fileIDs, gma2FileIDs)
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)
@ -397,6 +397,7 @@ func ProcessGMA(filePath string) (err error) {
//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)
if err != nil {
undoBatch(true, dboGMA.ID, fileIDs, gma2FileIDs)
return err
}
}
@ -411,11 +412,13 @@ func ProcessGMA(filePath string) (err error) {
err := os.MkdirAll(dir, os.ModePerm)
if err != nil {
undoBatch(true, dboGMA.ID, fileIDs, gma2FileIDs)
return err
}
gmaWriter, err := gma.NewWriter(destPath)
if err != nil {
undoBatch(true, dboGMA.ID, fileIDs, gma2FileIDs)
return err
}
defer gmaWriter.Close()
@ -423,10 +426,12 @@ func ProcessGMA(filePath string) (err error) {
//fmt.Printf("Writing Header with FormatVersion: %d\n", dboGMA.Header.FormatVersion)
err = gmaWriter.WriteHeader(dboGMA.Header)
if err != nil {
undoBatch(true, dboGMA.ID, fileIDs, gma2FileIDs)
return err
}
err = gmaWriter.WriteFirstType(dboGMA.FirstType)
if err != nil {
undoBatch(true, dboGMA.ID, fileIDs, gma2FileIDs)
return err
}
@ -435,6 +440,7 @@ func ProcessGMA(filePath string) (err error) {
//fmt.Printf("WriteFileIndex for %s number %d\n", dboGMA2File.FileName, dboGMA2File.FileNumber)
err = gmaWriter.WriteFileIndex(dboGMA2File.FileName, dboGMA2File.FileSize, dboGMA2File.CRC, dboGMA2File.NextType)
if err != nil {
undoBatch(true, dboGMA.ID, fileIDs, gma2FileIDs)
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)
resp, err := httpClient.Get(fmt.Sprintf("http://127.0.0.1:13371/fetch/%s", dboGMA2File.UploadID))
if err != nil {
undoBatch(true, dboGMA.ID, fileIDs, gma2FileIDs)
return err
}
defer resp.Body.Close()
err = gmaWriter.WriteFile(resp.Body)
if err != nil {
undoBatch(true, dboGMA.ID, fileIDs, gma2FileIDs)
return err
}
}
@ -461,6 +469,7 @@ func ProcessGMA(filePath string) (err error) {
gmaWriter.FileHandle.Seek(0, 0)
writeHash, err := gmaWriter.GetSHA256()
if err != nil {
undoBatch(true, dboGMA.ID, fileIDs, gma2FileIDs)
return err
}
@ -469,15 +478,18 @@ func ProcessGMA(filePath string) (err error) {
fmt.Println()
writeStat, err := os.Stat(destPath)
if err != nil {
undoBatch(true, dboGMA.ID, fileIDs, gma2FileIDs)
return err
}
writeSize := writeStat.Size()
if writeSize != dboGMA.GMASize {
//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)
}
if writeHash != dboGMA.GMAHash {
//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)
}
}
@ -489,9 +501,9 @@ func ProcessGMA(filePath string) (err error) {
_, err = colGMA.UpdateDocument(arangoCTX, dboGMA.ID, dboGMA)
if err != nil {
undoBatch(true, dboGMA.ID, fileIDs, gma2FileIDs)
return err
}
failedProcessing = false
0
return nil
}

Loading…
Cancel
Save