ASpellWorker.coffee 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. child_process = require("child_process")
  2. logger = require 'logger-sharelatex'
  3. metrics = require('metrics-sharelatex')
  4. _ = require "underscore"
  5. BATCH_SIZE = 100
  6. class ASpellWorker
  7. constructor: (language) ->
  8. @language = language
  9. @count = 0
  10. @pipe = child_process.spawn("aspell", ["pipe", "-t", "--encoding=utf-8", "-d", language])
  11. logger.log process: @pipe.pid, lang: @language, "starting new aspell worker"
  12. metrics.inc "aspellWorker-start-" + @language
  13. @pipe.on 'exit', () =>
  14. @state = 'killed'
  15. logger.log process: @pipe.pid, lang: @language, "aspell worker has exited"
  16. metrics.inc "aspellWorker-exit-" + @language
  17. @pipe.on 'close', () =>
  18. @state = 'closed' unless @state == 'killed'
  19. if @callback?
  20. logger.err process: @pipe.pid, lang: @language, "aspell worker closed output streams with uncalled callback"
  21. @callback new Error("aspell worker closed output streams with uncalled callback"), []
  22. @callback = null
  23. @pipe.on 'error', (err) =>
  24. @state = 'error' unless @state == 'killed'
  25. logger.log process: @pipe.pid, error: err, stdout: output.slice(-1024), stderr: error.slice(-1024), lang: @language, "aspell worker error"
  26. if @callback?
  27. @callback err, []
  28. @callback = null
  29. @pipe.stdin.on 'error', (err) =>
  30. @state = 'error' unless @state == 'killed'
  31. logger.log process: @pipe.pid, error: err, stdout: output.slice(-1024), stderr: error.slice(-1024), lang: @language, "aspell worker error on stdin"
  32. if @callback?
  33. @callback err, []
  34. @callback = null
  35. output = ""
  36. endMarker = new RegExp("^[a-z][a-z]", "m")
  37. @pipe.stdout.on "data", (chunk) =>
  38. output = output + chunk
  39. # We receive the language code from Aspell as the end of data marker
  40. if chunk.toString().match(endMarker)
  41. if @callback?
  42. @callback(null, output.slice())
  43. @callback = null # only allow one callback in use
  44. else
  45. logger.err process: @pipe.pid, lang: @language, "end of data marker received when callback already used"
  46. @state = 'ready'
  47. output = ""
  48. error = ""
  49. error = ""
  50. @pipe.stderr.on "data", (chunk) =>
  51. error = error + chunk
  52. @pipe.stdout.on "end", () =>
  53. # process has ended
  54. @state = "end"
  55. isReady: () ->
  56. return @state == 'ready'
  57. check: (words, callback) ->
  58. # we will now send data to aspell, and be ready again when we
  59. # receive the end of data marker
  60. @state = 'busy'
  61. if @callback? # only allow one callback in use
  62. logger.err process: @pipe.pid, lang: @language, "CALLBACK ALREADY IN USE"
  63. return @callback new Error("Aspell callback already in use - SHOULD NOT HAPPEN")
  64. @callback = _.once callback # extra defence against double callback
  65. @setTerseMode()
  66. @write(words)
  67. @flush()
  68. write: (words) ->
  69. i = 0
  70. while i < words.length
  71. # batch up the words to check for efficiency
  72. batch = words.slice(i, i + BATCH_SIZE)
  73. @sendWords batch
  74. i += BATCH_SIZE
  75. flush: () ->
  76. # get aspell to send an end of data marker "*" when ready
  77. #@sendCommand("%") # take the aspell pipe out of terse mode so we can look for a '*'
  78. #@sendCommand("^ENDOFSTREAMMARKER") # send our marker which will generate a '*'
  79. #@sendCommand("!") # go back into terse mode
  80. @sendCommand("$$l")
  81. shutdown: (reason) ->
  82. logger.log process: @pipe.pid, reason: reason, 'shutting down'
  83. @state = "closing"
  84. @pipe.stdin.end()
  85. kill: (reason) ->
  86. logger.log process: @pipe.pid, reason: reason, 'killing'
  87. return if @state == 'killed'
  88. @pipe.kill('SIGKILL')
  89. setTerseMode: () ->
  90. @sendCommand("!")
  91. sendWord: (word) ->
  92. @sendCommand("^" + word)
  93. sendWords: (words) ->
  94. # Aspell accepts multiple words to check on the same line
  95. # ^word1 word2 word3 ...
  96. # See aspell.info, writing programs to use Aspell Through A Pipe
  97. @sendCommand("^" + words.join(" "))
  98. @count++
  99. sendCommand: (command) ->
  100. @pipe.stdin.write(command + "\n")
  101. module.exports = ASpellWorker