Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(GH-209) Refactor the session state to be a class and pass that instead of global modules #210

Merged
merged 19 commits into from
Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
7a99d98
(GH-209) Create a session_state namespace
glennsarti Nov 28, 2019
051a28b
(GH-209) Create session_state and refactor DocumentStore
glennsarti Nov 28, 2019
1c41cf1
(maint) Return nil if the connection does not exist
glennsarti Dec 2, 2019
1884907
(maint) Remove unused protocol object
glennsarti Mar 31, 2020
f1c9fa4
(GH-209) Refactor ValidationQueue into a class
glennsarti Nov 30, 2019
33ed345
(GH-209) Refactor Crash Dump to use session state
glennsarti Dec 2, 2019
d3a4242
(GH-209) Refactor Sidecar queue
glennsarti Dec 2, 2019
19ce735
(maint) Add acceptance test debugging
glennsarti Mar 31, 2020
467f7b0
(GH-209) Refactor cache loaders and state detection
glennsarti Dec 3, 2019
3e43d18
(GH-209) Decouple session_state and connection id
glennsarti Dec 4, 2019
83aa365
(GH-209) Refactor node_graph and puppet_resource helpers
glennsarti Dec 4, 2019
7f1934a
(GH-209) Refactor helper for type information
glennsarti Dec 4, 2019
59756e4
(GH-209) Refactor helper for function information
glennsarti Dec 5, 2019
3ca893e
(GH-209) Refactor helper for class information
glennsarti Dec 5, 2019
b677506
(GH-209) Refactor helper for fact information
glennsarti Feb 1, 2020
9de7b51
(GH-209) Refactor helper for datatype information
glennsarti Dec 5, 2019
2dfb2e2
(GH-209) Refactor references to DocumentStore to use session_state
glennsarti Dec 5, 2019
1bd7e71
(GH-209) Refactor single instance queue tests
glennsarti Dec 6, 2019
7ebd435
(GH-209) Refactor documents in the Document Store
glennsarti Dec 9, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 155 additions & 0 deletions lib/puppet-languageserver/client_session_state.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
# frozen_string_literal: true

require 'puppet-languageserver/session_state/document_store'
require 'puppet-languageserver/session_state/language_client'
require 'puppet-languageserver/session_state/object_cache'

module PuppetLanguageServer
class ClientSessionState
attr_reader :documents

attr_reader :language_client

attr_reader :object_cache

attr_reader :connection_id

def initialize(message_handler, options = {})
@documents = options[:documents].nil? ? PuppetLanguageServer::SessionState::DocumentStore.new : options[:documents]
@language_client = options[:language_client].nil? ? PuppetLanguageServer::SessionState::LanguageClient.new(message_handler) : options[:language_client]
@object_cache = options[:object_cache].nil? ? PuppetLanguageServer::SessionState::ObjectCache.new : options[:object_cache]
@connection_id = options[:connection_id].nil? ? message_handler.protocol.connection.id : options[:connection_id]
end

# Helper methods to know the state of the object cache
def default_classes_loaded?
object_cache.section_in_origin_exist?(:class, :default)
end

def default_datatypes_loaded?
object_cache.section_in_origin_exist?(:datatype, :default)
end

def default_functions_loaded?
object_cache.section_in_origin_exist?(:function, :default)
end

def default_types_loaded?
object_cache.section_in_origin_exist?(:type, :default)
end

def facts_loaded?
object_cache.section_in_origin_exist?(:fact, :default)
end

def static_data_loaded?
object_cache.origin_exist?(:bolt)
end

# Loaders for object cache information
def load_default_data!(async = true)
if PuppetLanguageServer.featureflag?('puppetstrings')
PuppetLanguageServer.log_message(:info, "Loading Default Data via aggregate #{'(Async)' if async}...")
if async
sidecar_queue.enqueue('default_aggregate', [], false, connection_id)
sidecar_queue.enqueue('facts', [], false, connection_id)
else
sidecar_queue.execute('default_aggregate', [], false, connection_id)
sidecar_queue.execute('facts', [], false, connection_id)
end
else
# Order is (somewhat) important here. Try to get the most common information first
[
{ :name => 'Puppet Types', :action => 'default_types' },
{ :name => 'Facter', :action => 'facts' },
{ :name => 'Functions', :action => 'default_functions' },
{ :name => 'Classes', :action => 'default_classes' },
{ :name => 'DataTypes', :action => 'default_datatypes' }
].each do |item|
PuppetLanguageServer.log_message(:info, "Loading #{item[:name]} #{'(Async)' if async}...")
if async
sidecar_queue.enqueue(item[:action], [], false, connection_id)
else
sidecar_queue.execute(item[:action], [], false, connection_id)
end
end
end
true
end

def load_static_data!(async = true)
if async
Thread.new do
PuppetLanguageServer.log_message(:info, 'Loading static data (Async)...')
load_static_data_impl
end
else
PuppetLanguageServer.log_message(:info, 'Loading static data...')
load_static_data_impl
end
true
end

def load_workspace_data!(async = true)
return true if documents.store_root_path.nil?
action_args = ['--local-workspace', documents.store_root_path]
if PuppetLanguageServer.featureflag?('puppetstrings')
PuppetLanguageServer.log_message(:info, "Loading Workspace Data via aggregate #{'(Async)' if async}...")
if async
sidecar_queue.enqueue('workspace_aggregate', action_args, false, connection_id)
else
sidecar_queue.execute('workspace_aggregate', action_args, false, connection_id)
end
else
# Order is (somewhat) important here. Try to get the most common information first
[
{ :name => 'Puppet Types', :action => 'workspace_types' },
{ :name => 'Functions', :action => 'workspace_functions' },
{ :name => 'Classes', :action => 'workspace_classes' },
{ :name => 'DataTypes', :action => 'workspace_datatypes' }
].each do |item|
PuppetLanguageServer.log_message(:info, "Loading #{item[:name]} #{'(Async)' if async}...")
if async
sidecar_queue.enqueue(item[:action], action_args, false, connection_id)
else
sidecar_queue.execute(item[:action], action_args, false, connection_id)
end
end
end
end

def purge_workspace_data!
object_cache.remove_origin!(:workspace)
end

private

def sidecar_queue
PuppetLanguageServer::GlobalQueues.sidecar_queue
end

def load_static_data_impl
bolt_static_data = PuppetLanguageServer::Sidecar::Protocol::AggregateMetadata.new
Dir.glob(File.join(PuppetLanguageServer.static_data_dir, 'bolt-*.json')) do |path|
PuppetLanguageServer.log_message(:debug, "Importing static data file #{path}...")
# No need to catch errors here. As this is static data and is tested in rspec
# Sure, we could have corrupt/missing files on disk, but then we have bigger issues
data = PuppetLanguageServer::Sidecar::Protocol::AggregateMetadata.new.from_json!(File.open(path, 'rb:UTF-8') { |f| f.read })
data.each_list { |_, list| bolt_static_data.concat!(list) }
end

object_cache.import_sidecar_list!(bolt_static_data.classes, :class, :bolt)
object_cache.import_sidecar_list!(bolt_static_data.datatypes, :datatype, :bolt)
object_cache.import_sidecar_list!(bolt_static_data.functions, :function, :bolt)
object_cache.import_sidecar_list!(bolt_static_data.types, :type, :bolt)

bolt_static_data.each_list do |k, v|
if v.nil?
PuppetLanguageServer.log_message(:debug, "Static bolt data returned no #{k}")
else
PuppetLanguageServer.log_message(:debug, "Static bolt data returned #{v.count} #{k}")
end
end
end
end
end
6 changes: 3 additions & 3 deletions lib/puppet-languageserver/crash_dump.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def self.default_crash_file
File.join(Dir.tmpdir, 'puppet_language_server_crash.txt')
end

def self.write_crash_file(err, filename = nil, additional = {})
def self.write_crash_file(err, session_state, filename = nil, additional = {})
# Create the crash text

puppet_version = Puppet.version rescue 'Unknown' # rubocop:disable Lint/RescueWithoutErrorClass, Style/RescueModifier
Expand All @@ -33,8 +33,8 @@ def self.write_crash_file(err, filename = nil, additional = {})
# rubocop:enable Layout/IndentHeredoc, Layout/ClosingHeredocIndentation, Style/FormatStringToken

# Append the documents in the cache
PuppetLanguageServer::DocumentStore.document_uris.each do |uri|
crashtext += "Document - #{uri}\n---\n#{PuppetLanguageServer::DocumentStore.document(uri)}\n\n"
session_state.documents.document_uris.each do |uri|
crashtext += "Document - #{uri}\n---\n#{session_state.documents.document_content(uri)}\n\n"
end
# Append additional objects from the crash
additional.each do |k, v|
Expand Down
184 changes: 0 additions & 184 deletions lib/puppet-languageserver/document_store.rb

This file was deleted.

Loading