litestack 0.2.3 → 0.2.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,47 +4,66 @@ require 'sqlite3'
4
4
  require 'logger'
5
5
  require 'oj'
6
6
  require 'yaml'
7
+ require 'pathname'
8
+ require 'fileutils'
7
9
 
8
10
  module Litesupport
9
11
 
10
12
  class Error < StandardError; end
11
13
 
12
- # cache the environment we are running in
13
- # it is an error to change the environment for a process
14
- # or for a child forked from that process
15
- def self.environment
16
- @env ||= detect_environment
17
- end
18
-
19
14
  def self.max_contexts
20
- return 50 if environment == :fiber || environment == :polyphony
15
+ return 50 if scheduler == :fiber || scheduler == :polyphony
21
16
  5
22
17
  end
23
-
24
- # identify which environment we are running in
25
- # we currently support :fiber, :polyphony, :iodine & :threaded
26
- # in the future we might want to expand to other environments
18
+
19
+ # Detect the Rack or Rails environment.
27
20
  def self.detect_environment
21
+ if defined? Rails
22
+ Rails.env
23
+ elsif ENV["RACK_ENV"]
24
+ ENV["RACK_ENV"]
25
+ elsif ENV["APP_ENV"]
26
+ ENV["RACK_ENV"]
27
+ else
28
+ "development"
29
+ end
30
+ end
31
+
32
+ def self.environment
33
+ @environment ||= detect_environment
34
+ end
35
+
36
+ # cache the scheduler we are running in
37
+ # it is an error to change the scheduler for a process
38
+ # or for a child forked from that process
39
+ def self.scheduler
40
+ @scehduler ||= detect_scheduler
41
+ end
42
+
43
+ # identify which scheduler we are running in
44
+ # we currently support :fiber, :polyphony, :iodine & :threaded
45
+ # in the future we might want to expand to other schedulers
46
+ def self.detect_scheduler
28
47
  return :fiber if Fiber.scheduler
29
48
  return :polyphony if defined? Polyphony
30
49
  return :iodine if defined? Iodine
31
- return :threaded # fall back for all other environments
50
+ return :threaded # fall back for all other schedulers
32
51
  end
33
52
 
34
53
  # spawn a new execution context
35
54
  def self.spawn(&block)
36
- if self.environment == :fiber
55
+ if self.scheduler == :fiber
37
56
  Fiber.schedule(&block)
38
- elsif self.environment == :polyphony
57
+ elsif self.scheduler == :polyphony
39
58
  spin(&block)
40
- elsif self.environment == :threaded or self.environment == :iodine
59
+ elsif self.scheduler == :threaded or self.scheduler == :iodine
41
60
  Thread.new(&block)
42
61
  end
43
62
  # we should never reach here
44
63
  end
45
-
64
+
46
65
  def self.context
47
- if environment == :fiber || environment == :poylphony
66
+ if scheduler == :fiber || scheduler == :poylphony
48
67
  Fiber.current.storage
49
68
  else
50
69
  Thread.current
@@ -52,7 +71,7 @@ module Litesupport
52
71
  end
53
72
 
54
73
  def self.current_context
55
- if environment == :fiber || environment == :poylphony
74
+ if scheduler == :fiber || scheduler == :poylphony
56
75
  Fiber.current
57
76
  else
58
77
  Thread.current
@@ -61,10 +80,10 @@ module Litesupport
61
80
 
62
81
  # switch the execution context to allow others to run
63
82
  def self.switch
64
- if self.environment == :fiber
83
+ if self.scheduler == :fiber
65
84
  Fiber.scheduler.yield
66
85
  true
67
- elsif self.environment == :polyphony
86
+ elsif self.scheduler == :polyphony
68
87
  Fiber.current.schedule
69
88
  Thread.current.switch_fiber
70
89
  true
@@ -85,7 +104,7 @@ module Litesupport
85
104
  # they must send (true) as a parameter to this method
86
105
  # else it is a no-op for fibers
87
106
  def self.synchronize(fiber_sync = false, &block)
88
- if self.environment == :fiber or self.environment == :polyphony
107
+ if self.scheduler == :fiber or self.scheduler == :polyphony
89
108
  yield # do nothing, just run the block as is
90
109
  else
91
110
  self.mutex.synchronize(&block)
@@ -103,7 +122,30 @@ module Litesupport
103
122
  end
104
123
  db
105
124
  end
106
-
125
+
126
+ # Databases will be stored by default at this path.
127
+ def self.root
128
+ @root ||= ensure_root_volume detect_root
129
+ end
130
+
131
+ # Default path where we'll store all of the databases.
132
+ def self.detect_root
133
+ path = if ENV["LITESTACK_DATA_PATH"]
134
+ ENV["LITESTACK_DATA_PATH"]
135
+ elsif defined? Rails
136
+ "./db"
137
+ else
138
+ "."
139
+ end
140
+
141
+ Pathname.new(path).join(Litesupport.environment)
142
+ end
143
+
144
+ def self.ensure_root_volume(path)
145
+ FileUtils.mkdir_p path unless path.exist?
146
+ path
147
+ end
148
+
107
149
  class Mutex
108
150
 
109
151
  def initialize
@@ -111,7 +153,7 @@ module Litesupport
111
153
  end
112
154
 
113
155
  def synchronize(&block)
114
- if Litesupport.environment == :threaded || Litesupport.environment == :iodine
156
+ if Litesupport.scheduler == :threaded || Litesupport.scheduler == :iodine
115
157
  @mutex.synchronize{ block.call }
116
158
  else
117
159
  block.call
@@ -188,7 +230,7 @@ module Litesupport
188
230
  def _fork(*args)
189
231
  ppid = Process.pid
190
232
  result = super
191
- if Process.pid != ppid && [:threaded, :iodine].include?(Litesupport.environment)
233
+ if Process.pid != ppid && [:threaded, :iodine].include?(Litesupport.scheduler)
192
234
  ForkListener.listeners.each{|l| l.call }
193
235
  end
194
236
  result
@@ -201,6 +243,11 @@ module Litesupport
201
243
  include Forkable
202
244
 
203
245
  # close, setup, run_stmt and run_sql assume a single connection was created
246
+
247
+ def options
248
+ @options
249
+ end
250
+
204
251
  def close
205
252
  @running = false
206
253
  @conn.acquire do |q|
@@ -209,6 +256,22 @@ module Litesupport
209
256
  end
210
257
  end
211
258
 
259
+ def size
260
+ run_sql("SELECT size.page_size * count.page_count FROM pragma_page_size() AS size, pragma_page_count() AS count")[0][0].to_f / (1024*1024)
261
+ end
262
+
263
+ def journal_mode
264
+ run_method(:journal_mode)
265
+ end
266
+
267
+ def synchronous
268
+ run_method(:synchronous)
269
+ end
270
+
271
+ def path
272
+ run_method(:filename)
273
+ end
274
+
212
275
  private # all methods are private
213
276
 
214
277
  def init(options = {})
@@ -224,22 +287,14 @@ module Litesupport
224
287
  Litesupport::ForkListener.listen do
225
288
  setup
226
289
  end
227
- end
228
-
290
+ end
291
+
229
292
  def configure(options = {})
230
- # detect environment (production, development, etc.)
231
- env = "development"
232
- if defined? Rails
233
- env = ENV["RAILS_ENV"]
234
- elsif ENV["RACK_ENV"]
235
- env = ENV["RACK_ENV"]
236
- elsif ENV["APP_ENV"]
237
- env = ENV["RACK_ENV"]
238
- end
293
+ # detect enviornment (production, development, etc.)
239
294
  defaults = self.class::DEFAULT_OPTIONS rescue {}
240
295
  @options = defaults.merge(options)
241
296
  config = YAML.load_file(@options[:config_path]) rescue {} # an empty hash won't hurt
242
- config = config[env] if config[env] # if there is a config for the current environment defined then use it, otherwise use the top level declaration
297
+ config = config[Litesupport.environment] if config[Litesupport.environment] # if there is a config for the current enviornment defined then use it, otherwise use the top level declaration
243
298
  config.keys.each do |k| # symbolize keys
244
299
  config[k.to_sym] = config[k]
245
300
  config.delete k
@@ -266,7 +321,7 @@ module Litesupport
266
321
  def exit_callback
267
322
  close
268
323
  end
269
-
324
+
270
325
  def run_stmt(stmt, *args)
271
326
  @conn.acquire{|q| q.stmts[stmt].execute!(*args) }
272
327
  end
@@ -275,6 +330,10 @@ module Litesupport
275
330
  @conn.acquire{|q| q.execute(sql, *args) }
276
331
  end
277
332
 
333
+ def run_method(method, *args)
334
+ @conn.acquire{|q| q.send(method, *args)}
335
+ end
336
+
278
337
  def create_pooled_connection(count = 1)
279
338
  Litesupport::Pool.new(1){create_connection}
280
339
  end
@@ -0,0 +1,10 @@
1
+ require "rails/railtie"
2
+
3
+ module Litestack
4
+ class Railtie < ::Rails::Railtie
5
+ initializer :disable_production_sqlite_warning do |app|
6
+ # The whole point of this gem is to use sqlite3 in production.
7
+ app.config.active_record.sqlite3_production_warning = false
8
+ end
9
+ end
10
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Litestack
4
- VERSION = "0.2.3"
4
+ VERSION = "0.2.6"
5
5
  end
data/lib/litestack.rb CHANGED
@@ -16,6 +16,7 @@ require_relative "./railties/rails/commands/dbconsole" if defined? Rails && defi
16
16
  require_relative "./active_support/cache/litecache" if defined? ActiveSupport
17
17
  require_relative "./active_job/queue_adapters/litejob_adapter" if defined? ActiveJob
18
18
  require_relative "./action_cable/subscription_adapter/litecable" if defined? ActionCable
19
+ require_relative "./litestack/railtie" if defined? Rails
19
20
 
20
21
  module Litestack
21
22
  class NotImplementedError < Exception; end
@@ -15,7 +15,7 @@ module Sequel
15
15
 
16
16
  def connect(server)
17
17
 
18
- Sequel.extension :fiber_concurrency if [:fiber, :polyphony].include? Litesupport.environment
18
+ Sequel.extension :fiber_concurrency if [:fiber, :polyphony].include? Litesupport.scheduler
19
19
 
20
20
  opts = server_opts(server)
21
21
  opts[:database] = ':memory:' if blank_object?(opts[:database])
data/template.rb ADDED
@@ -0,0 +1,7 @@
1
+ # Run `rails new my-app -m https://raw.githubusercontent.com/bradgessler/litestack/master/template.rb`
2
+ # to create a new Rails app with Litestack pre-installed.
3
+ gem "litestack", github: "bradgessler/litestack"
4
+
5
+ after_bundle do
6
+ generate "litestack:install"
7
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: litestack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mohamed Hassan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-05-18 00:00:00.000000000 Z
11
+ date: 2023-07-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sqlite3
@@ -38,10 +38,67 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rack
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: hanami-router
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: tilt
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: erubi
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
41
97
  description:
42
98
  email:
43
99
  - oldmoe@gmail.com
44
- executables: []
100
+ executables:
101
+ - liteboard
45
102
  extensions: []
46
103
  extra_rdoc_files: []
47
104
  files:
@@ -67,11 +124,21 @@ files:
67
124
  - bench/rails_job.rb
68
125
  - bench/skjob.rb
69
126
  - bench/uljob.rb
127
+ - bin/liteboard
70
128
  - lib/action_cable/subscription_adapter/litecable.rb
71
129
  - lib/active_job/queue_adapters/litejob_adapter.rb
72
130
  - lib/active_record/connection_adapters/litedb_adapter.rb
73
131
  - lib/active_support/cache/litecache.rb
132
+ - lib/generators/litestack/install/USAGE
133
+ - lib/generators/litestack/install/install_generator.rb
134
+ - lib/generators/litestack/install/templates/cable.yml
135
+ - lib/generators/litestack/install/templates/database.yml
74
136
  - lib/litestack.rb
137
+ - lib/litestack/liteboard/liteboard.rb
138
+ - lib/litestack/liteboard/views/event.erb
139
+ - lib/litestack/liteboard/views/index.erb
140
+ - lib/litestack/liteboard/views/layout.erb
141
+ - lib/litestack/liteboard/views/topic.erb
75
142
  - lib/litestack/litecable.rb
76
143
  - lib/litestack/litecable.sql.yml
77
144
  - lib/litestack/litecache.rb
@@ -82,15 +149,17 @@ files:
82
149
  - lib/litestack/litejobqueue.rb
83
150
  - lib/litestack/litemetric.rb
84
151
  - lib/litestack/litemetric.sql.yml
152
+ - lib/litestack/litemetric_collector.sql.yml
85
153
  - lib/litestack/litequeue.rb
86
154
  - lib/litestack/litequeue.sql.yml
87
155
  - lib/litestack/litesupport.rb
88
- - lib/litestack/metrics_app.rb
156
+ - lib/litestack/railtie.rb
89
157
  - lib/litestack/version.rb
90
158
  - lib/railties/rails/commands/dbconsole.rb
91
159
  - lib/sequel/adapters/litedb.rb
92
160
  - lib/sequel/adapters/shared/litedb.rb
93
161
  - samples/ultrajob.yaml
162
+ - template.rb
94
163
  homepage: http://github.com/oldmoe/litestack
95
164
  licenses:
96
165
  - MIT
@@ -115,7 +184,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
115
184
  - !ruby/object:Gem::Version
116
185
  version: '0'
117
186
  requirements: []
118
- rubygems_version: 3.4.8
187
+ rubygems_version: 3.4.17
119
188
  signing_key:
120
189
  specification_version: 4
121
190
  summary: A SQLite based, lightning fast, super efficient and dead simple to setup
@@ -1,5 +0,0 @@
1
- require_relative '../lib/litestack/litemetrics'
2
-
3
- metric = Litemetrics.instance
4
-
5
- puts metric.ids