« get me outta code hell

Metadata (stored, throttle, status, and more) - mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
path: root/metadata-readers.js
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2019-02-25 12:06:27 -0400
committerFlorrie <towerofnix@gmail.com>2019-02-25 12:06:27 -0400
commite9ccfa2fd4221ddff4950d5180ee5c8fb0bf8117 (patch)
tree917fb878b8ba0166bb324f3781a756d516a639ca /metadata-readers.js
parent75251bb2309505c20dc7500117a17649d41412d8 (diff)
Metadata (stored, throttle, status, and more)
Diffstat (limited to 'metadata-readers.js')
-rw-r--r--metadata-readers.js24
1 files changed, 22 insertions, 2 deletions
diff --git a/metadata-readers.js b/metadata-readers.js
index 1e6eb1b..64f413a 100644
--- a/metadata-readers.js
+++ b/metadata-readers.js
@@ -1,8 +1,28 @@
 const { promisifyProcess } = require('./general-util')
 const { spawn } = require('child_process')
 
+// Some probers are sorta inconsistent; this function lets them try again if
+// they fail the first time.
+const tryAgain = function(times, func) {
+  return async function(...args) {
+    let n = 0
+    let ret
+    while (!ret && n < times) {
+      try {
+        ret = await func(...args)
+      } catch (error) {
+        if (n + 1 === times) {
+          throw error
+        }
+      }
+      n++
+    }
+    return ret
+  }
+}
+
 const metadataReaders = {
-  ffprobe: async filePath => {
+  ffprobe: tryAgain(6, async filePath => {
     const ffprobe = spawn('ffprobe', [
       '-print_format', 'json',
       '-show_entries', 'stream=codec_name:format',
@@ -36,7 +56,7 @@ const metadataReaders = {
       fileSize: parseInt(data.format.size),
       bitrate: parseInt(data.format.bit_rate)
     }
-  },
+  }),
 
   getMetadataReaderFor: arg => {
     return metadataReaders.ffprobe