Automatically migrated from Gitolite
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.2 KiB

7 years ago
  1. chokidar = require "chokidar"
  2. nodemailer = require "nodemailer"
  3. path = require "path"
  4. fs = require "fs"
  5. watcher = chokidar.watch "./errors", depth: 0, ignoreInitial: true
  6. mailer = nodemailer.createTransport()
  7. processFile = (filePath) ->
  8. fs.readFile filePath, (err, data) ->
  9. try
  10. parsedData = JSON.parse(data)
  11. catch error
  12. console.log "Error report not complete yet, retrying #{filePath} in 1 second..."
  13. setTimeout (->
  14. processFile(filePath)
  15. ), 1000
  16. return
  17. errorMessage = parsedData?.message ? "UNKNOWN ERROR"
  18. textStack = parsedData?.stack?.replace(/\u001b(?:\[\??(?:\d\;*)*[A-HJKSTfminsulh])?/g, "") ? ""
  19. message = """
  20. A failure occurred. #{filePath} is attached.
  21. #{textStack}
  22. """
  23. htmlMessage = """
  24. A failure occurred. #{filePath} is attached.
  25. <pre>#{textStack}</pre>
  26. """.replace(/\n/g, "<br>")
  27. mailer.sendMail
  28. from: "ops@pdf.yt"
  29. to: "admin@cryto.net"
  30. subject: "Automatic failure report: #{errorMessage}"
  31. text: message
  32. html: htmlMessage
  33. attachments: [
  34. filename: path.basename(filePath)
  35. path: filePath
  36. contentType: "application/json"
  37. ]
  38. watcher.on "add", (filePath) ->
  39. console.log "PANIC! Sending report:", filePath
  40. processFile(filePath)
  41. console.log "Running..."