sprockets-derailleur 1.0.0 → 1.1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 44edd61c8b242defbd08b9e80a68cef7b2f15ebc
4
- data.tar.gz: bbe80f95343fc291abb6138a543769411f51a0fd
3
+ metadata.gz: ccc65b211fb466e3602a25feedd7d969648e2b9e
4
+ data.tar.gz: a9be9ff647af9cba0fdd5167be5c462e58719127
5
5
  SHA512:
6
- metadata.gz: 6ab698ea4f8ae75a34e724c000a07fa7605a0bf8db4f71bb08144440886e9aafa84a622c467754859b26f809b9e6187161b625450b09b13412615cb7ef9a2b54
7
- data.tar.gz: 6e40312aabc329f8d56340105720d3394c5ca0c0d7cf888a6b8cdc013a7695370ea3c2bfe1525c3516f7c7bae490ee25210018cd3efa24129de1992f400a9ad7
6
+ metadata.gz: efa4573354b0e808e81e4346e802f21d47e686b85baa67c451965c6ada3a8fe2c7ab2295d0a8f5f5569a390a0ffc6657edd8f43ffe509a4162183d5154e662df
7
+ data.tar.gz: 3ea74c387c00279abdfb77ff04bcd38680c1d0098718897453ec8db89c392a60aa947a75e17dc7092044bbf53f34923dac416a8de0aa72d28463f111f91e2f56
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # Sprockets::Derailleur
2
2
 
3
- Speed up Manifest::Compile by forking processes
3
+ Speed up Manifest::Compile by forking processes
4
+
5
+ **Warning: Do not use with Sprockets 3+!** (See [#25](https://github.com/steel/sprockets-derailleur/issues/25) for details.)
4
6
 
5
7
  ## Installation
6
8
 
@@ -10,9 +12,38 @@ Speed up Manifest::Compile by forking processes
10
12
 
11
13
  ## Usage
12
14
 
13
- To override the number of processes you can use the
14
- `SPROCKETS_DERAILLEUR_WORKER_COUNT` environment variable. It defaults to the
15
- number of processors on your machine.
15
+ To override the number of processes you can use the `worker_count` configuration
16
+ setting or the `SPROCKETS_DERAILLEUR_WORKER_COUNT` environment variable.
17
+ It defaults to the number of processors on your machine.
18
+
19
+ ### Configuration
20
+
21
+ You are able to configure Sprockets::Derailleur by creating a configure block (an example can be seen below).
22
+ This is entirely optional and will by default use the settings commented out in the example below.
23
+ If you would like to configure Sprockets::Derailleur, putting this configuration in an initializer is a good idea.
24
+
25
+ ```ruby
26
+ # file: config/initializers/sprockets-derailleur.rb
27
+
28
+ SprocketsDerailleur.configure do |config|
29
+ # Override how long the file lock timeout lasts
30
+ # config.file_lock_timeout = 10
31
+
32
+ # Set to true to log the compiled time lines to the info level
33
+ # (usually they are logged to the debug level)
34
+ #config.compile_times_to_info_log = false
35
+
36
+ # Override the number of workers to use
37
+ # If not set will use SPROCKETS_DERAILLEUR_WORKER_COUNT environment variable
38
+ # and if that's also not set, then will use the number of processors
39
+ # config.worker_count = 8
40
+
41
+ # Has the same effect as setting the rails cache file store to be the new
42
+ # thread safe sprockets derailleur one, but monkey patches methods instead of
43
+ # instantiating new object.
44
+ # config.use_sprockets_derailleur_file_store = false
45
+ end
46
+ ```
16
47
 
17
48
  ### Rails 4.0
18
49
 
@@ -27,24 +58,24 @@ Here we need to override some core parts of the sprockets module.
27
58
  ```ruby
28
59
  module Sprockets
29
60
  class StaticCompiler
30
-
61
+
31
62
  alias_method :compile_without_manifest, :compile
32
63
  def compile
33
64
  puts "Multithreading on " + SprocketsDerailleur.worker_count.to_s + " processors"
34
65
  puts "Starting Asset Compile: " + Time.now.getutc.to_s
35
-
66
+
36
67
  # Then initialize the manifest with the workers you just determined
37
68
  manifest = Sprockets::Manifest.new(env, target)
38
69
  manifest.compile paths
39
-
70
+
40
71
  puts "Finished Asset Compile: " + Time.now.getutc.to_s
41
-
72
+
42
73
  end
43
74
  end
44
-
75
+
45
76
  class Railtie < ::Rails::Railtie
46
77
  config.after_initialize do |app|
47
-
78
+
48
79
  config = app.config
49
80
  next unless config.assets.enabled
50
81
 
@@ -58,10 +89,10 @@ module Sprockets
58
89
  manifest = Sprockets::Manifest.new(app, path)
59
90
  config.assets.digests = manifest.assets
60
91
  end
61
-
92
+
62
93
  end
63
94
  end
64
-
95
+
65
96
  end
66
97
  ```
67
98
 
@@ -1,8 +1,22 @@
1
1
  require "sprockets-derailleur/version"
2
2
  require "sprockets-derailleur/manifest"
3
+ require "sprockets-derailleur/file_store_extension"
3
4
  require "sprockets-derailleur/file_store"
5
+ require "sprockets-derailleur/configuration"
4
6
 
5
7
  module SprocketsDerailleur
8
+ class << self
9
+ attr_writer :configuration
10
+ end
11
+
12
+ def self.configuration
13
+ @configuration ||= Configuration.new
14
+ end
15
+
16
+ def self.configure
17
+ yield(configuration)
18
+ end
19
+
6
20
  def self.number_of_processors
7
21
  if RUBY_PLATFORM =~ /linux/
8
22
  return `cat /proc/cpuinfo | grep processor | wc -l`.to_i
@@ -25,10 +39,18 @@ module SprocketsDerailleur
25
39
  end
26
40
 
27
41
  def self.worker_count
28
- worker_count = ENV['SPROCKETS_DERAILLEUR_WORKER_COUNT'].to_i
42
+ worker_count = SprocketsDerailleur.configuration.worker_count || ENV['SPROCKETS_DERAILLEUR_WORKER_COUNT'].to_i
29
43
  return worker_count if worker_count > 0
30
44
  number_of_processors
31
45
  rescue
32
46
  1
33
47
  end
34
- end
48
+
49
+ def self.prepend_file_store_if_required
50
+ if SprocketsDerailleur.configuration.use_sprockets_derailleur_file_store
51
+ Sprockets::Cache::FileStore.class_eval do
52
+ prepend SprocketsDerailleur::FileStoreExtension
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,12 @@
1
+ module SprocketsDerailleur
2
+ class Configuration
3
+ attr_accessor :file_lock_timeout, :compile_times_to_info_log, :worker_count, :use_sprockets_derailleur_file_store
4
+
5
+ def initialize
6
+ @file_lock_timeout = 10
7
+ @compile_times_to_info_log = false
8
+ @worker_count = nil
9
+ @use_sprockets_derailleur_file_store = false
10
+ end
11
+ end
12
+ end
@@ -1,32 +1,7 @@
1
- require 'fileutils'
2
- require 'timeout'
3
-
4
1
  # The Sprockets::Cache::FileStore is not thread/parallel safe.
5
2
  # This one uses file locks to be safe.
6
3
  module SprocketsDerailleur
7
4
  class FileStore < Sprockets::Cache::FileStore
8
- def lock
9
- @lock ||= begin
10
- FileUtils.mkdir_p @root
11
- File.open(@root.join("lock"), File::RDWR|File::CREAT)
12
- end
13
- end
14
-
15
- # Lookup value in cache
16
- def [](key)
17
- with_lock(File::LOCK_SH) { super }
18
- end
19
-
20
- # Save value to cache
21
- def []=(key, value)
22
- with_lock(File::LOCK_EX) { super }
23
- end
24
-
25
- def with_lock(type)
26
- Timeout::timeout(10) { lock.flock(type) }
27
- yield
28
- ensure
29
- lock.flock(File::LOCK_UN)
30
- end
5
+ prepend SprocketsDerailleur::FileStoreExtension
31
6
  end
32
7
  end
@@ -0,0 +1,30 @@
1
+ require 'fileutils'
2
+ require 'timeout'
3
+
4
+ module SprocketsDerailleur
5
+ module FileStoreExtension
6
+ def lock
7
+ @lock ||= begin
8
+ FileUtils.mkdir_p @root
9
+ File.open(@root.join("lock"), File::RDWR|File::CREAT)
10
+ end
11
+ end
12
+
13
+ # Lookup value in cache
14
+ def [](key)
15
+ with_lock(File::LOCK_SH) { super }
16
+ end
17
+
18
+ # Save value to cache
19
+ def []=(key, value)
20
+ with_lock(File::LOCK_EX) { super }
21
+ end
22
+
23
+ def with_lock(type)
24
+ Timeout::timeout(SprocketsDerailleur.configuration.file_lock_timeout) { lock.flock(type) }
25
+ yield
26
+ ensure
27
+ lock.flock(File::LOCK_UN)
28
+ end
29
+ end
30
+ end
@@ -4,6 +4,8 @@ module Sprockets
4
4
  class Manifest
5
5
  alias_method :compile_with_workers, :compile
6
6
  def compile(*args)
7
+ SprocketsDerailleur::prepend_file_store_if_required
8
+
7
9
  worker_count = SprocketsDerailleur::worker_count
8
10
  paths_with_errors = {}
9
11
 
@@ -121,7 +123,11 @@ module Sprockets
121
123
  end
122
124
  end
123
125
 
124
- logger.debug "Compiled #{path} (#{(time.real * 1000).round}ms, pid #{Process.pid})"
126
+ if SprocketsDerailleur.configuration.compile_times_to_info_log
127
+ logger.info "Compiled #{path} (#{(time.real * 1000).round}ms, pid #{Process.pid})"
128
+ else
129
+ logger.debug "Compiled #{path} (#{(time.real * 1000).round}ms, pid #{Process.pid})"
130
+ end
125
131
  end
126
132
  ensure
127
133
  child_read.close
@@ -1,5 +1,5 @@
1
1
  module Sprockets
2
2
  module Derailleur
3
- VERSION = "1.0.0"
3
+ VERSION = "1.1.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sprockets-derailleur
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steel Fu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-08-12 00:00:00.000000000 Z
11
+ date: 2017-04-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sprockets
@@ -37,7 +37,9 @@ files:
37
37
  - README.md
38
38
  - Rakefile
39
39
  - lib/sprockets-derailleur.rb
40
+ - lib/sprockets-derailleur/configuration.rb
40
41
  - lib/sprockets-derailleur/file_store.rb
42
+ - lib/sprockets-derailleur/file_store_extension.rb
41
43
  - lib/sprockets-derailleur/manifest.rb
42
44
  - lib/sprockets-derailleur/version.rb
43
45
  - sprockets-derailleur.gemspec
@@ -60,7 +62,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
60
62
  version: '0'
61
63
  requirements: []
62
64
  rubyforge_project:
63
- rubygems_version: 2.2.3
65
+ rubygems_version: 2.6.8
64
66
  signing_key:
65
67
  specification_version: 4
66
68
  summary: Multi process sprockets compiling