Better log message in update

This commit is contained in:
Radhi Fadlillah 2019-05-23 23:56:38 +07:00
parent 1659943f1c
commit 80f117c337

View file

@ -111,8 +111,12 @@ func updateHandler(cmd *cobra.Command, args []string) {
if !offline { if !offline {
mx := sync.RWMutex{} mx := sync.RWMutex{}
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
chDone := make(chan struct{})
chMessage := make(chan interface{}, 10)
semaphore := make(chan struct{}, 10) semaphore := make(chan struct{}, 10)
cInfo.Println("Downloading article(s)...")
for i, book := range bookmarks { for i, book := range bookmarks {
wg.Add(1) wg.Add(1)
@ -121,7 +125,7 @@ func updateHandler(cmd *cobra.Command, args []string) {
book.URL = url book.URL = url
} }
go func(i int, book model.Bookmark, nData int) { go func(i int, book model.Bookmark) {
// Make sure to finish the WG // Make sure to finish the WG
defer wg.Done() defer wg.Done()
@ -132,11 +136,16 @@ func updateHandler(cmd *cobra.Command, args []string) {
}() }()
// Download article // Download article
cInfo.Printf("[ %d / %d ] Downloading %s\n", i+1, nData, book.URL) resp, err := httpClient.Get(book.URL)
article, err := readability.FromURL(book.URL, time.Minute)
if err != nil { if err != nil {
cError.Printf("[ %d / %d ] Failed to download article: %v\n", i+1, nData, err) chMessage <- fmt.Errorf("Failed to download %s: %v", book.URL, err)
return
}
defer resp.Body.Close()
article, err := readability.FromReader(resp.Body, book.URL)
if err != nil {
chMessage <- fmt.Errorf("Failed to parse %s: %v", book.URL, err)
return return
} }
@ -159,23 +168,44 @@ func updateHandler(cmd *cobra.Command, args []string) {
if imageURL != "" { if imageURL != "" {
imgPath := fp.Join(DataDir, "thumb", fmt.Sprintf("%d", book.ID)) imgPath := fp.Join(DataDir, "thumb", fmt.Sprintf("%d", book.ID))
downloadFile(imageURL, imgPath, time.Minute)
err = downloadFile(imageURL, imgPath, time.Minute)
if err != nil {
cError.Printf("[ %d / %d ] Failed to download image: %v\n", i+1, nData, err)
return
}
} }
// Send success message
chMessage <- fmt.Sprintf("Downloaded %s", book.URL)
// Save parse result to bookmark // Save parse result to bookmark
mx.Lock() mx.Lock()
bookmarks[i] = book bookmarks[i] = book
mx.Unlock() mx.Unlock()
}(i, book, len(bookmarks)) }(i, book)
} }
// Print log message
go func(nBookmark int) {
logIndex := 0
for {
select {
case <-chDone:
cInfo.Println("Download finished")
return
case msg := <-chMessage:
logIndex++
switch msg.(type) {
case error:
cError.Printf("[%d/%d] %v\n", logIndex, nBookmark, msg)
case string:
cInfo.Printf("[%d/%d] %s\n", logIndex, nBookmark, msg)
}
}
}
}(len(bookmarks))
// Wait until all download finished // Wait until all download finished
wg.Wait() wg.Wait()
close(chDone)
} }
// Map which tags is new or deleted from flag --tags // Map which tags is new or deleted from flag --tags