expedite 0.0.1 → 0.0.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 492f096e30142c23ad6849f79864355a96fdb042990d43aaaaa85516643a6338
4
- data.tar.gz: 29d32100e9df804748a684fdce1636907fe844996adb702e193369c434ffcd8a
3
+ metadata.gz: d23f0d668e02d87588fa2838e18afe60ca22e42a6df958c28418a5e3629a596c
4
+ data.tar.gz: 99086610fca24d08680ca1824315b179e904b31aade25aba8365f0d0873d6ac5
5
5
  SHA512:
6
- metadata.gz: d5f5fce18a565bbc3ea616bf1a5db621a1708eae3f6d1ce6ae6bb5c32d266859983ed35fefc8905184241602bc1554128627adf4ff3281f256056147351c9d2e
7
- data.tar.gz: f85be9f1dfe698b1875fd0008d9a0c4ed324f5ddeb1b9562b83431864c4eaa664e1c83e748b680fb6877ecdd00c71a8160226613d52ae6f5338e7ec2195d7809
6
+ metadata.gz: 1e26f98230e95ae04828b9500f1e5e1dabd76296fa0d41a18ef55ea653e115b2283190ddfa706f1280e3bb78959e1ab9ab36f4d17ecd09b91f9a6c954ee06789
7
+ data.tar.gz: d46a15f0bf0bbf68e87a0cd58d4df5d9af7850c51619199d1006f6313b44c746f6d3188fc09bed1ef0cefa636a61671a1c283fa8201e0c9bbbf018a775618dac
data/README.md CHANGED
@@ -8,20 +8,24 @@ derivatives to start faster.
8
8
 
9
9
  ## Usage
10
10
 
11
- Register variants and commands in `expedite_helper.rb`. For example:
11
+ To use expedite you need to register variants and commands in an `expedite_helper.rb`
12
+ that is placed in the root directory of your application. The sample discussed
13
+ in this section is in the [examples/simple](examples/simple) folder.
12
14
 
15
+ This is the "parent" variant:
13
16
  ```
14
- Expedite::Variants.register('base' do |name|
15
- puts "Base started"
17
+ # You can pass `keep_alive: true` if you want the variant to restart
18
+ # automatically if it is terminated. This option defaults to false.
19
+ Expedite::Variants.register('parent') do
20
+ $sleep_parent = 1
16
21
  end
17
22
  ```
18
23
 
19
- You can register variants that are based on other variants, and you can also have wildcard
24
+ You can register variants that are based on other variants. You can also have wildcard
20
25
  matchers.
21
26
  ```
22
- Expedite::Variants.register('development/*', parent: 'base') do |name|
23
- customer = File.basename(name)
24
- puts "Starting development for #{customer}"
27
+ Expedite::Variants.register('development/*', parent: 'parent') do |name|
28
+ $sleep_child = name
25
29
  end
26
30
  ```
27
31
 
@@ -37,7 +41,47 @@ Expedite::Commands.register("custom") do
37
41
  end
38
42
  ```
39
43
 
40
- Then you can execute a command in the variant using:
44
+ After registering your variant and commands, you can then use it. In the simple
45
+ example, the `main.rb` calls the `custom` command on the `development/abc`
46
+ variant.
47
+
41
48
  ```
49
+ require 'expedite'
50
+
42
51
  Expedite.v("development/abc").call("custom")
43
52
  ```
53
+
54
+ When you run `main.rb`, the following output is produced. Note that `$sleep_parent`
55
+ comes from teh `parent` variant, and `$sleep_child` comes from the `development/abc`
56
+ variant.
57
+
58
+ ```
59
+ # bundle exec ./main.rb
60
+ [development/abc] sleeping for 5
61
+ $sleep_parent = 1
62
+ $sleep_child = development/abc
63
+ [development/abc] done
64
+ ```
65
+
66
+ Calling `main.rb` automatically started the expedite server in the background.
67
+ In the above example, it does the following:
68
+
69
+ 1. Launch the `base` variant
70
+ 2. Fork from the `base` variant to create the `development/abc` variant
71
+ 3. Fork from the `development/abc` variant
72
+
73
+ To explicitly stop the server and all the variants, you use:
74
+
75
+ ```
76
+ $ bundle exec expedite stop
77
+ ```
78
+
79
+ You can also start the server in the foreground.
80
+
81
+ ```
82
+ $ bundle exec expedite server
83
+ ```
84
+
85
+ ## Acknowledgements
86
+
87
+ Expedite's server core is modified from [Spring](https://github.com/rails/spring)
@@ -6,8 +6,8 @@ require 'socket'
6
6
  require 'expedite/commands'
7
7
  require 'expedite/env'
8
8
  require 'expedite/failsafe_thread'
9
- require 'expedite/load_helper'
10
9
  require 'expedite/signals'
10
+ require 'expedite/variants'
11
11
 
12
12
  module Expedite
13
13
  def self.variant
@@ -22,16 +22,14 @@ module Expedite
22
22
  end
23
23
 
24
24
  class Application
25
- include LoadHelper
26
25
  include Signals
27
26
 
28
27
  attr_reader :variant
29
- attr_reader :manager, :env, :original_env
28
+ attr_reader :manager, :env
30
29
 
31
- def initialize(variant, manager, original_env, env = Env.new)
30
+ def initialize(variant:, manager:, env:)
32
31
  @variant = variant
33
32
  @manager = manager
34
- @original_env = original_env
35
33
  @env = env
36
34
  @mutex = Mutex.new
37
35
  @waiting = Set.new
@@ -48,7 +46,7 @@ module Expedite
48
46
 
49
47
  Signal.trap("TERM") { terminate }
50
48
 
51
- load_helper
49
+ env.load_helper
52
50
  eager_preload if false #if ENV.delete("SPRING_PRELOAD") == "1"
53
51
  run
54
52
  end
@@ -149,9 +147,6 @@ module Expedite
149
147
  ARGV.replace(args)
150
148
  $0 = exec_name
151
149
 
152
- # Delete all env vars which are unchanged from before Spring started
153
- original_env.each { |k, v| ENV.delete k if ENV[k] == v }
154
-
155
150
  # Load in the current env vars, except those which *were* changed when Spring started
156
151
  env.each { |k, v| ENV[k] = v }
157
152
 
@@ -177,7 +172,12 @@ module Expedite
177
172
  log "forked #{pid}"
178
173
  manager.puts pid
179
174
 
180
- wait pid, streams, client
175
+ # Boot makes a new application, so we don't wait for it
176
+ if command.is_a?(Expedite::Command::Boot)
177
+ Process.detach(pid)
178
+ else
179
+ wait pid, streams, client
180
+ end
181
181
  rescue Exception => e
182
182
  log "exception: #{e} at #{e.backtrace.join("\n")}"
183
183
  manager.puts unless pid
@@ -1,9 +1,11 @@
1
1
  require "expedite/application"
2
2
 
3
3
  app = Expedite::Application.new(
4
- ENV['EXPEDITE_VARIANT'],
5
- UNIXSocket.for_fd(3),
6
- {},
7
- Expedite::Env.new(log_file: IO.for_fd(4))
4
+ variant: ENV['EXPEDITE_VARIANT'],
5
+ manager: UNIXSocket.for_fd(3),
6
+ env: Expedite::Env.new(
7
+ root: ENV['EXPEDITE_ROOT'],
8
+ log_file: IO.for_fd(4),
9
+ ),
8
10
  )
9
11
  app.boot
@@ -9,18 +9,20 @@ module Expedite
9
9
  class ApplicationManager
10
10
  include SendJson
11
11
 
12
- attr_reader :pid, :child, :variant, :env, :status
13
-
14
- def initialize(variant, env)
15
- @variant = variant
16
- @env = env
17
- @mutex = Mutex.new
18
- @state = :running
19
- @pid = nil
12
+ attr_reader :pid, :child, :name, :env, :status, :variant
13
+
14
+ def initialize(name, env)
15
+ @name = name
16
+ @env = env
17
+ @mutex = Mutex.new
18
+ @state = :running
19
+ @pid = nil
20
+
21
+ @variant = Expedite::Variants.lookup(@name)
20
22
  end
21
23
 
22
24
  def log(message)
23
- env.log "[application_manager:#{variant}] #{message}"
25
+ env.log "[application_manager:#{name}] #{message}"
24
26
  end
25
27
 
26
28
  # We're not using @mutex.synchronize to avoid the weird "<internal:prelude>:10"
@@ -98,8 +100,12 @@ module Expedite
98
100
  # Don't care
99
101
  end
100
102
 
103
+ def keep_alive
104
+ variant.keep_alive
105
+ end
106
+
101
107
  def parent
102
- Expedite::Variants.lookup(variant).parent
108
+ variant.parent
103
109
  end
104
110
 
105
111
  private
@@ -121,7 +127,7 @@ module Expedite
121
127
  wr.send_io STDERR
122
128
  wr.send_io STDIN
123
129
 
124
- send_json wr, 'args' => ['expedite/boot', variant], 'env' => {}
130
+ send_json wr, 'args' => ['expedite/boot', name], 'env' => {}
125
131
  wr.send_io child_socket
126
132
  wr.send_io env.log_file
127
133
  wr.close
@@ -139,7 +145,8 @@ module Expedite
139
145
  bundler_dir = File.expand_path("../..", $LOADED_FEATURES.grep(/bundler\/setup\.rb$/).first)
140
146
  @pid = Process.spawn(
141
147
  {
142
- "EXPEDITE_VARIANT" => variant,
148
+ "EXPEDITE_VARIANT" => name,
149
+ "EXPEDITE_ROOT" => env.root,
143
150
  },
144
151
  "ruby",
145
152
  *(bundler_dir != RbConfig::CONFIG["rubylibdir"] ? ["-I", bundler_dir] : []),
@@ -171,7 +178,7 @@ module Expedite
171
178
  synchronize {
172
179
  if @pid == pid
173
180
  @pid = nil
174
- restart
181
+ restart if keep_alive
175
182
  end
176
183
  }
177
184
  end
data/lib/expedite/cli.rb CHANGED
@@ -31,6 +31,8 @@ module Expedite
31
31
 
32
32
  def run(args)
33
33
  command(args.first).run(args[1..])
34
+ rescue NotImplementedError
35
+ Cli::Help.new.run([])
34
36
  end
35
37
 
36
38
  def command(cmd)
@@ -7,10 +7,12 @@ module Expedite
7
7
  require "expedite/application"
8
8
 
9
9
  Expedite::Application.new(
10
- variant,
11
- UNIXSocket.for_fd(@child_socket.fileno),
12
- {},
13
- Expedite::Env.new(log_file: @log_file)
10
+ variant: variant,
11
+ manager: UNIXSocket.for_fd(@child_socket.fileno),
12
+ env: Expedite::Env.new(
13
+ root: ENV['EXPEDITE_ROOT'],
14
+ log_file: @log_file,
15
+ ),
14
16
  ).boot
15
17
  end
16
18
 
@@ -25,10 +25,12 @@ module Expedite
25
25
  require "expedite/application"
26
26
 
27
27
  Expedite::Application.new(
28
- variant,
29
- UNIXSocket.for_fd(@child_socket.fileno),
30
- {},
31
- Expedite::Env.new(log_file: @log_file)
28
+ variant: variant,
29
+ manager: UNIXSocket.for_fd(@child_socket.fileno),
30
+ env: Expedite::Env.new(
31
+ root: ENV['EXPEDITE_ROOT'],
32
+ log_file: @log_file,
33
+ ),
32
34
  ).boot
33
35
  end
34
36
 
data/lib/expedite/env.rb CHANGED
@@ -56,5 +56,17 @@ module Expedite
56
56
  def graceful_termination_timeout
57
57
  2
58
58
  end
59
+
60
+ def helper_path
61
+ Pathname.new(root).join("expedite_helper.rb")
62
+ end
63
+
64
+ def load_helper
65
+ path = helper_path
66
+ if path.exist?
67
+ log "loading #{path}"
68
+ load(path)
69
+ end
70
+ end
59
71
  end
60
72
  end
@@ -3,12 +3,10 @@ require 'json'
3
3
  require 'socket'
4
4
  require "expedite/application_manager"
5
5
  require "expedite/env"
6
- require 'expedite/load_helper'
7
6
  require "expedite/signals"
8
7
 
9
8
  module Expedite
10
9
  class Server
11
- include LoadHelper
12
10
  include Signals
13
11
 
14
12
  def self.boot(options = {})
@@ -33,7 +31,7 @@ module Expedite
33
31
  end
34
32
 
35
33
  def boot
36
- load_helper
34
+ env.load_helper
37
35
 
38
36
  write_pidfile
39
37
  set_pgid unless foreground?
@@ -1,13 +1,23 @@
1
1
 
2
2
  module Expedite
3
3
  class Variant
4
+ ##
5
+ # Name of the parent variant. This allows you to create variants from
6
+ # an existing variant.
7
+ # Defaults to nil.
4
8
  attr_accessor :parent
5
9
 
10
+ ##
11
+ # If set to true, variant will be restarted automatically if it is killed.
12
+ # Defaults to false.
13
+ attr_accessor :keep_alive
14
+
6
15
  ##
7
16
  # [parent] Name of parent variant.
8
17
  # [after_fork] Block is executed when variant is first preloaded.
9
- def initialize(parent: nil, &after_fork)
18
+ def initialize(parent: nil, keep_alive: false, &after_fork)
10
19
  @parent = parent
20
+ @keep_alive = keep_alive
11
21
  @after_fork_proc = after_fork
12
22
  end
13
23
 
@@ -82,7 +92,7 @@ module Expedite
82
92
  end
83
93
 
84
94
  def reset
85
- @registrations = {}
95
+ @registrations = []
86
96
  nil
87
97
  end
88
98
  end
@@ -1,3 +1,3 @@
1
1
  module Expedite
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: expedite
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bing-Chang Lai
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-06-12 00:00:00.000000000 Z
11
+ date: 2021-07-03 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Manages Ruby processes that can be used to spawn child processes faster.
14
14
  email: johnny.lai@me.com
@@ -34,7 +34,6 @@ files:
34
34
  - lib/expedite/env.rb
35
35
  - lib/expedite/errors.rb
36
36
  - lib/expedite/failsafe_thread.rb
37
- - lib/expedite/load_helper.rb
38
37
  - lib/expedite/send_json.rb
39
38
  - lib/expedite/server.rb
40
39
  - lib/expedite/signals.rb
@@ -1,11 +0,0 @@
1
- module Expedite
2
- module LoadHelper
3
- def load_helper
4
- helper = "expedite_helper.rb"
5
- if File.exist?(helper)
6
- log "loading #{helper}"
7
- load(helper)
8
- end
9
- end
10
- end
11
- end