graphmind 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails/railtie"
4
+ require "active_support/ordered_options"
5
+
6
+ module Graphmind
7
+ # A deliberately small Railtie. It does three things a plain initializer
8
+ # cannot do as well:
9
+ #
10
+ # 1. names the app after the Rails application, so the viewer shows
11
+ # "shopify-support" instead of "ruby";
12
+ # 2. clears the thread's run context when Rails finishes a unit of work.
13
+ # Puma reuses threads, so a run left behind by a request that died
14
+ # mid-flight would otherwise adopt the *next* request's events;
15
+ # 3. releases held gates and closes the socket on shutdown.
16
+ #
17
+ # Configure it from `config/application.rb` or an initializer:
18
+ #
19
+ # config.graphmind.enabled = Rails.env.development?
20
+ # config.graphmind.app = "support-agent"
21
+ # config.graphmind.url = "ws://127.0.0.1:4747/ingest"
22
+ #
23
+ # Every key is passed straight to Graphmind.configure. Setting
24
+ # `config.graphmind.autoconfigure = false` leaves everything to you.
25
+ class Railtie < ::Rails::Railtie
26
+ config.graphmind = ActiveSupport::OrderedOptions.new if defined?(ActiveSupport::OrderedOptions)
27
+
28
+ initializer "graphmind.configure" do |app|
29
+ options = app.config.respond_to?(:graphmind) ? app.config.graphmind : nil
30
+ next if options.nil?
31
+ next if options[:autoconfigure] == false
32
+
33
+ settings = options.to_h.reject { |key, _| key == :autoconfigure }
34
+ settings[:app] ||= app.class.name.to_s.split("::").first.to_s.downcase
35
+ next if Graphmind.configured?
36
+
37
+ begin
38
+ Graphmind.configure(**settings)
39
+ rescue StandardError => e
40
+ # A debugger must never take an application down at boot. A typo in
41
+ # config.graphmind is a warning, not a failed deploy.
42
+ warn "[graphmind] ignoring config.graphmind (#{e.class}: #{e.message}); " \
43
+ "GraphMind will use its defaults"
44
+ Graphmind.configure(app: settings[:app]) unless Graphmind.configured?
45
+ end
46
+ end
47
+
48
+ initializer "graphmind.executor" do |app|
49
+ # `to_complete` runs after every request, job or console block — the
50
+ # right place to drop a thread-local that must not outlive it.
51
+ app.executor.to_complete do
52
+ Graphmind.clear_run_context if Graphmind.configured?
53
+ rescue StandardError
54
+ nil
55
+ end
56
+ end
57
+
58
+ config.after_initialize do
59
+ at_exit do
60
+ Graphmind.dispose if Graphmind.configured?
61
+ rescue StandardError
62
+ nil
63
+ end
64
+ end
65
+ end
66
+ end