|
| 1 | +module PuppetLanguageServer |
| 2 | + # Module for enqueing and running document level validation asynchronously |
| 3 | + # When adding a document to be validation, it will remove any validation requests for the same |
| 4 | + # document in the queue so that only the latest document needs to be processed. |
| 5 | + # |
| 6 | + # It will also ignore sending back validation results to the client if the document is |
| 7 | + # updated during the validation process |
| 8 | + module ValidationQueue |
| 9 | + @queue = [] |
| 10 | + @queue_mutex = Mutex.new |
| 11 | + @queue_thread = nil |
| 12 | + |
| 13 | + # Enqueue a file to be validated |
| 14 | + def self.enqueue(file_uri, doc_version, workspace, connection_object) |
| 15 | + document_type = connection_object.document_type(file_uri) |
| 16 | + |
| 17 | + unless %i[manifest epp].include?(document_type) |
| 18 | + # Can't validate these types so just emit an empty validation result |
| 19 | + connection_object.reply_diagnostics(file_uri, []) |
| 20 | + return |
| 21 | + end |
| 22 | + |
| 23 | + @queue_mutex.synchronize do |
| 24 | + @queue.reject! { |item| item['file_uri'] == file_uri } |
| 25 | + |
| 26 | + @queue << { |
| 27 | + 'file_uri' => file_uri, |
| 28 | + 'doc_version' => doc_version, |
| 29 | + 'document_type' => document_type, |
| 30 | + 'workspace' => workspace, |
| 31 | + 'connection_object' => connection_object |
| 32 | + } |
| 33 | + end |
| 34 | + |
| 35 | + if @queue_thread.nil? || !@queue_thread.alive? |
| 36 | + @queue_thread = Thread.new do |
| 37 | + begin |
| 38 | + worker |
| 39 | + rescue => err # rubocop:disable Style/RescueStandardError |
| 40 | + PuppetLanguageServer.log_message(:error, "Error in ValidationQueue Thread: #{err}") |
| 41 | + raise |
| 42 | + end |
| 43 | + end |
| 44 | + end |
| 45 | + |
| 46 | + nil |
| 47 | + end |
| 48 | + |
| 49 | + # Synchronously validate a file |
| 50 | + def self.validate_sync(file_uri, doc_version, workspace, connection_object) |
| 51 | + document_type = connection_object.document_type(file_uri) |
| 52 | + content = documents.document(file_uri, doc_version) |
| 53 | + return nil if content.nil? |
| 54 | + result = validate(document_type, content, workspace) |
| 55 | + |
| 56 | + # Send the response |
| 57 | + connection_object.reply_diagnostics(file_uri, result) |
| 58 | + end |
| 59 | + |
| 60 | + # Helper method to the Document Store |
| 61 | + def self.documents |
| 62 | + PuppetLanguageServer::DocumentStore |
| 63 | + end |
| 64 | + |
| 65 | + # Wait for the queue to become empty |
| 66 | + def self.drain_queue |
| 67 | + return if @queue_thread.nil? || !@queue_thread.alive? |
| 68 | + @queue_thread.join |
| 69 | + nil |
| 70 | + end |
| 71 | + |
| 72 | + # Testing helper resets the queue and prepopulates it with |
| 73 | + # a known arbitrary configuration. |
| 74 | + # ONLY USE THIS FOR TESTING! |
| 75 | + def self.reset_queue(initial_state = []) |
| 76 | + @queue_mutex.synchronize do |
| 77 | + @queue = initial_state |
| 78 | + end |
| 79 | + end |
| 80 | + |
| 81 | + # Validate a document |
| 82 | + def self.validate(document_type, content, workspace) |
| 83 | + # Perform validation |
| 84 | + case document_type |
| 85 | + when :manifest |
| 86 | + PuppetLanguageServer::DocumentValidator.validate(content, workspace) |
| 87 | + when :epp |
| 88 | + PuppetLanguageServer::DocumentValidator.validate_epp(content, workspace) |
| 89 | + else |
| 90 | + [] |
| 91 | + end |
| 92 | + end |
| 93 | + private_class_method :validate |
| 94 | + |
| 95 | + # Thread worker which processes all jobs in the queue and validates each document |
| 96 | + # serially |
| 97 | + def self.worker |
| 98 | + work_item = nil |
| 99 | + loop do |
| 100 | + @queue_mutex.synchronize do |
| 101 | + return if @queue.empty? |
| 102 | + work_item = @queue.shift |
| 103 | + end |
| 104 | + return if work_item.nil? |
| 105 | + |
| 106 | + file_uri = work_item['file_uri'] |
| 107 | + doc_version = work_item['doc_version'] |
| 108 | + connection_object = work_item['connection_object'] |
| 109 | + document_type = work_item['document_type'] |
| 110 | + workspace = work_item['workspace'] |
| 111 | + |
| 112 | + # Check if the document is the latest version |
| 113 | + content = documents.document(file_uri, doc_version) |
| 114 | + if content.nil? |
| 115 | + PuppetLanguageServer.log_message(:debug, "ValidationQueue Thread: Ignoring #{work_item['file_uri']} as it is not the latest version or has been removed") |
| 116 | + return |
| 117 | + end |
| 118 | + |
| 119 | + # Perform validation |
| 120 | + result = validate(document_type, content, workspace) |
| 121 | + |
| 122 | + # Check if the document is still latest version |
| 123 | + current_version = documents.document_version(file_uri) |
| 124 | + if current_version != doc_version |
| 125 | + PuppetLanguageServer.log_message(:debug, "ValidationQueue Thread: Ignoring #{work_item['file_uri']} as has changed version from #{doc_version} to #{current_version}") |
| 126 | + return |
| 127 | + end |
| 128 | + |
| 129 | + # Send the response |
| 130 | + connection_object.reply_diagnostics(file_uri, result) |
| 131 | + end |
| 132 | + end |
| 133 | + private_class_method :worker |
| 134 | + end |
| 135 | +end |
0 commit comments