error handling inside goroutines
This commit is contained in:
parent
2a88e5c900
commit
25eef75018
@ -41,6 +41,10 @@ func DownloadBook(outDir string, size string, id string, logger Logger) error {
|
|||||||
semaphore := make(chan struct{}, MAX_DOWNLOADS)
|
semaphore := make(chan struct{}, MAX_DOWNLOADS)
|
||||||
|
|
||||||
logger.Info.Println("downloading pages...")
|
logger.Info.Println("downloading pages...")
|
||||||
|
|
||||||
|
ec := make(chan error)
|
||||||
|
finished := make(chan bool, 1)
|
||||||
|
|
||||||
for index, pageUrl := range pageUrls {
|
for index, pageUrl := range pageUrls {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
|
|
||||||
@ -52,18 +56,31 @@ func DownloadBook(outDir string, size string, id string, logger Logger) error {
|
|||||||
<-semaphore
|
<-semaphore
|
||||||
}()
|
}()
|
||||||
|
|
||||||
downloadUrl, err:= getSizeLink(pageUrl, size)
|
downloadUrl, err := getSizeLink(pageUrl, size)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
//return err
|
ec <- err
|
||||||
}
|
}
|
||||||
|
|
||||||
filename := fmt.Sprintf("%06d.jpg", index)
|
filename := fmt.Sprintf("%06d.jpg", index)
|
||||||
downloadToFile(filename, dir, downloadUrl)
|
err = downloadToFile(filename, dir, downloadUrl)
|
||||||
|
if err != nil {
|
||||||
|
ec <- err
|
||||||
|
}
|
||||||
}(index, pageUrl)
|
}(index, pageUrl)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
close(finished)
|
||||||
|
}()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-finished:
|
||||||
|
case err := <-ec:
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
err = os.MkdirAll(outDir, os.ModePerm)
|
err = os.MkdirAll(outDir, os.ModePerm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -77,7 +94,7 @@ func DownloadBook(outDir string, size string, id string, logger Logger) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Find the download link for a page of a specific size.
|
// Find the download link for a page of a specific size.
|
||||||
func getSizeLink(pageUrl string , size string) (string, error) {
|
func getSizeLink(pageUrl string, size string) (string, error) {
|
||||||
doc, err := fetchDocument(pageUrl)
|
doc, err := fetchDocument(pageUrl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
|
Loading…
Reference in New Issue
Block a user