Class: PuppetLanguageServer::GlobalQueues::SidecarQueue

Inherits:
SingleInstanceQueue show all
Defined in:
lib/puppet-languageserver/global_queues/sidecar_queue.rb

Overview

Module for enqueing and running sidecar jobs asynchronously When adding a job, it will remove any other for the same job in the queue, so that only the latest job needs to be processed.

Instance Method Summary collapse

Methods inherited from SingleInstanceQueue

#drain_queue, #enqueue, #enqueue_job, #execute, #initialize, #new_job, #reset_queue

Constructor Details

This class inherits a constructor from PuppetLanguageServer::GlobalQueues::SingleInstanceQueue

Instance Method Details

#execute_job(job_object) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/puppet-languageserver/global_queues/sidecar_queue.rb', line 33

def execute_job(job_object)
  super
  connection = connection_from_connection_id(job_object.connection_id)
  raise "Connection is not available for connection id #{job_object.connection_id}" if connection.nil?

  sidecar_path = File.expand_path(File.join(__dir__, '..', '..', '..', 'puppet-languageserver-sidecar'))
  args = ['--action', job_object.action].concat(job_object.additional_args).concat(sidecar_args_from_connection(connection))
  cmd = ['ruby', sidecar_path].concat(args)
  PuppetLanguageServer.log_message(:debug, "SidecarQueue Thread: Running sidecar #{cmd}")
  stdout, stderr, status = run_sidecar(cmd)
  PuppetLanguageServer.log_message(:warning, "SidecarQueue Thread: Calling sidecar with #{args.join(' ')} returned exitcode #{status.exitstatus}, #{stderr}")
  return nil unless status.exitstatus.zero?

  # It's possible server has closed the connection while the sidecar is running.
  # So raise if the connection is no longer available
  raise "Connection is no longer available for connection id #{job_object.connection_id}" if connection_from_connection_id(job_object.connection_id).nil?

  session_state = session_state_from_connection(connection)
  raise "Session state is not available for connection id #{job_object.connection_id}" if session_state.nil?

  cache = session_state.object_cache

  # Correctly encode the result as UTF8
  result = stdout.bytes.pack('U*')

  case job_object.action.downcase
  when 'default_aggregate'
    lists = PuppetLanguageServer::Sidecar::Protocol::AggregateMetadata.new.from_json!(result)
    cache.import_sidecar_list!(lists.classes,   :class, :default)
    cache.import_sidecar_list!(lists.datatypes, :datatype, :default)
    cache.import_sidecar_list!(lists.functions, :function, :default)
    cache.import_sidecar_list!(lists.types,     :type, :default)

    lists.each_list do |k, v|
      if v.nil?
        PuppetLanguageServer.log_message(:debug, "SidecarQueue Thread: default_aggregate returned no #{k}")
      else
        PuppetLanguageServer.log_message(:debug, "SidecarQueue Thread: default_aggregate returned #{v.count} #{k}")
      end
    end

  when 'default_classes'
    list = PuppetLanguageServer::Sidecar::Protocol::PuppetClassList.new.from_json!(result)
    cache.import_sidecar_list!(list, :class, :default)
    PuppetLanguageServer.log_message(:debug, "SidecarQueue Thread: default_classes returned #{list.count} items")

  when 'default_datatypes'
    list = PuppetLanguageServer::Sidecar::Protocol::PuppetDataTypeList.new.from_json!(result)
    cache.import_sidecar_list!(list, :datatype, :default)
    PuppetLanguageServer.log_message(:debug, "SidecarQueue Thread: default_datatypes returned #{list.count} items")

  when 'default_functions'
    list = PuppetLanguageServer::Sidecar::Protocol::PuppetFunctionList.new.from_json!(result)
    cache.import_sidecar_list!(list, :function, :default)
    PuppetLanguageServer.log_message(:debug, "SidecarQueue Thread: default_functions returned #{list.count} items")

  when 'default_types'
    list = PuppetLanguageServer::Sidecar::Protocol::PuppetTypeList.new.from_json!(result)
    cache.import_sidecar_list!(list, :type, :default)
    PuppetLanguageServer.log_message(:debug, "SidecarQueue Thread: default_types returned #{list.count} items")

  when 'facts'
    list = PuppetLanguageServer::Sidecar::Protocol::FactList.new.from_json!(result)
    cache.import_sidecar_list!(list, :fact, :default)
    PuppetLanguageServer.log_message(:debug, "SidecarQueue Thread: facts returned #{list.count} items")

  when 'node_graph'
    return PuppetLanguageServer::Sidecar::Protocol::PuppetNodeGraph.new.from_json!(result)

  when 'resource_list'
    return PuppetLanguageServer::Sidecar::Protocol::ResourceList.new.from_json!(result)

  when 'workspace_aggregate'
    lists = PuppetLanguageServer::Sidecar::Protocol::AggregateMetadata.new.from_json!(result)
    cache.import_sidecar_list!(lists.classes,   :class, :workspace)
    cache.import_sidecar_list!(lists.datatypes, :datatype, :workspace)
    cache.import_sidecar_list!(lists.functions, :function, :workspace)
    cache.import_sidecar_list!(lists.types,     :type, :workspace)

    lists.each_list do |k, v|
      if v.nil?
        PuppetLanguageServer.log_message(:debug, "SidecarQueue Thread: workspace_aggregate returned no #{k}")
      else
        PuppetLanguageServer.log_message(:debug, "SidecarQueue Thread: workspace_aggregate returned #{v.count} #{k}")
      end
    end

  when 'workspace_classes'
    list = PuppetLanguageServer::Sidecar::Protocol::PuppetClassList.new.from_json!(result)
    cache.import_sidecar_list!(list, :class, :workspace)
    PuppetLanguageServer.log_message(:debug, "SidecarQueue Thread: workspace_classes returned #{list.count} items")

  when 'workspace_datatypes'
    list = PuppetLanguageServer::Sidecar::Protocol::PuppetDataTypeList.new.from_json!(result)
    cache.import_sidecar_list!(list, :datatype, :workspace)
    PuppetLanguageServer.log_message(:debug, "SidecarQueue Thread: workspace_datatypes returned #{list.count} items")

  when 'workspace_functions'
    list = PuppetLanguageServer::Sidecar::Protocol::PuppetFunctionList.new.from_json!(result)
    cache.import_sidecar_list!(list, :function, :workspace)
    PuppetLanguageServer.log_message(:debug, "SidecarQueue Thread: workspace_functions returned #{list.count} items")

  when 'workspace_types'
    list = PuppetLanguageServer::Sidecar::Protocol::PuppetTypeList.new.from_json!(result)
    cache.import_sidecar_list!(list, :type, :workspace)
    PuppetLanguageServer.log_message(:debug, "SidecarQueue Thread: workspace_types returned #{list.count} items")

  else
    PuppetLanguageServer.log_message(:error, "SidecarQueue Thread: Unknown action #{job_object.action}")
  end

  true
rescue StandardError => e
  raise unless job_object.handle_errors

  PuppetLanguageServer.log_message(:error, "SidecarQueue Thread: Error running action #{job_object.action}. #{e}\n#{e.backtrace}")
  nil
end

#job_classObject



29
30
31
# File 'lib/puppet-languageserver/global_queues/sidecar_queue.rb', line 29

def job_class
  SidecarQueueJob
end

#max_queue_threadsObject



25
26
27
# File 'lib/puppet-languageserver/global_queues/sidecar_queue.rb', line 25

def max_queue_threads
  2
end