1
0
forked from YaBL/app

reaper: MORE parallel

This commit is contained in:
Michael 2025-06-22 11:28:17 +03:00
parent d7e1038e8b
commit 8855de53f4

View File

@ -48,7 +48,7 @@ func (a *App) Run() error {
go func() { go func() {
defer dbWg.Done() defer dbWg.Done()
batchSize := 50 batchSize := 150
batch := make([]BookJob, 0, batchSize) batch := make([]BookJob, 0, batchSize)
flush := func() { flush := func() {
addedBooks := 0 addedBooks := 0
@ -104,7 +104,7 @@ func findZipFiles(basePath string) []string {
} }
func processZipFilesParallel(basePath string, zipFiles []string, jobChan chan<- BookJob, processedCount *uint64, totalZips int) { func processZipFilesParallel(basePath string, zipFiles []string, jobChan chan<- BookJob, processedCount *uint64, totalZips int) {
const workers = 4 const workers = 8
var wg sync.WaitGroup var wg sync.WaitGroup
tasks := make(chan string, workers) tasks := make(chan string, workers)
@ -143,32 +143,35 @@ func processZip(basePath string, zipPath string, jobChan chan<- BookJob) {
log.Printf("Failed open zip: %v\n", err) log.Printf("Failed open zip: %v\n", err)
return return
} }
bookcase, err := filepath.Rel(basePath, zipPath)
if err != nil {
log.Printf("Error rel path: %v\n", err)
return
}
defer r.Close() defer r.Close()
var wg sync.WaitGroup
for _, f := range r.File { for _, f := range r.File {
if strings.HasSuffix(strings.ToLower(f.Name), ".fb2") { if strings.HasSuffix(strings.ToLower(f.Name), ".fb2") {
wg.Add(1)
go func() {
defer wg.Done()
rc, err := f.Open() rc, err := f.Open()
if err != nil { if err != nil {
log.Printf("Unable read file from archive: %v\n", err) log.Printf("Unable read file from archive: %v\n", err)
continue return
} }
rawFB2 := reaper.Parse(rc) rawFB2 := reaper.Parse(rc)
_ = rc.Close() _ = rc.Close()
if rawFB2 == nil { if rawFB2 == nil {
// log.Printf("Не удалось распарсить: %s\n", f.Name) // log.Printf("Не удалось распарсить: %s\n", f.Name)
continue return
}
bookcase, err := filepath.Rel(basePath, zipPath)
if err != nil {
log.Printf("Error rel path: %v\n", err)
continue
} }
fb2 := reaper.RawToFB2(*rawFB2, f.FileInfo().Name(), &bookcase, f.UncompressedSize64, nil) fb2 := reaper.RawToFB2(*rawFB2, f.FileInfo().Name(), &bookcase, f.UncompressedSize64, nil)
jobChan <- BookJob{FB2: fb2} jobChan <- BookJob{FB2: fb2}
}()
} }
} }
wg.Wait()
} }