sprockets-derailleur 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -4,23 +4,21 @@ Speed up Manifest::Compile by forking processes
4
4
 
5
5
  ## Installation
6
6
 
7
- Add this line to your application's Gemfile:
7
+ 1. Add this line to your application's Gemfile: `gem 'sprockets-derailleur'`
8
+ 2. And then execute: `$ bundle`, or install it yourself as: `$ gem install sprockets-derailleur`
9
+ 3. Require `sprockets-derailleur` in environment file: `require 'sprockets-derailleur'`
8
10
 
9
- gem 'sprockets-derailleur'
10
-
11
- And then execute:
11
+ ## Usage
12
12
 
13
- $ bundle
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.
14
16
 
15
- Or install it yourself as:
17
+ ### Rails 4.0
16
18
 
17
- $ gem install sprockets-derailleur
19
+ Just drop in the Gemfile, nothing more.
18
20
 
19
- Require `sprockets-derailleur` in environment file:
20
-
21
- require 'sprockets-derailleur'
22
-
23
- ## Usage
21
+ ### Rails 3.2
24
22
 
25
23
  To install to an existing rails 3.2 project, first create a new file, 'sprockets_derailleur.rb' in config/initializers.
26
24
 
@@ -32,15 +30,11 @@ module Sprockets
32
30
 
33
31
  alias_method :compile_without_manifest, :compile
34
32
  def compile
35
-
36
- # Determine how many workers you want to use first. Determine the number of physical CPUs this way
37
- processes = SprocketsDerailleur::number_of_processors rescue 1
38
-
39
- puts "Multithreading on " + processes.to_s + " processors"
33
+ puts "Multithreading on " + SprocketsDerailleur.worker_count + " processors"
40
34
  puts "Starting Asset Compile: " + Time.now.getutc.to_s
41
35
 
42
36
  # Then initialize the manifest with the workers you just determined
43
- manifest = Sprockets::Manifest.new(env, target, processes)
37
+ manifest = Sprockets::Manifest.new(env, target)
44
38
  manifest.compile paths
45
39
 
46
40
  puts "Finished Asset Compile: " + Time.now.getutc.to_s
@@ -95,6 +89,28 @@ rake assets:precompile:primary RAILS_ENV=production
95
89
 
96
90
  This skips the non-digest compile, hence doubling speed (especially useful if syncing assets with a remote server).
97
91
 
92
+ ## Troubleshooting
93
+
94
+ In some situations (like if you are using compass sprites) you may see
95
+ occasional exceptions such as:
96
+
97
+ ```
98
+ lib/active_support/core_ext/marshal.rb:6:in `load': end of file reached (EOFError)
99
+
100
+ AND/OR
101
+
102
+ lib/sprockets-derailleur/manifest.rb:127:in `write': Broken pipe (Errno::EPIPE)
103
+ ```
104
+
105
+ The default cache is not safe for parallel, you can override it by adding the
106
+ following to `config/application.rb`:
107
+
108
+ ```ruby
109
+ config.assets.configure do |env|
110
+ env.cache = SprocketsDerailleur::FileStore.new("tmp/cache/assets")
111
+ end
112
+ ```
113
+
98
114
  ## Contributing
99
115
 
100
116
  1. Fork it
@@ -0,0 +1,32 @@
1
+ require 'fileutils'
2
+ require 'timeout'
3
+
4
+ # The Sprockets::Cache::FileStore is not thread/parallel safe.
5
+ # This one uses file locks to be safe.
6
+ module SprocketsDerailleur
7
+ 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(1) { lock.flock(type) }
27
+ yield
28
+ ensure
29
+ lock.flock(File::LOCK_UN)
30
+ end
31
+ end
32
+ end
@@ -2,16 +2,9 @@ require "sprockets"
2
2
 
3
3
  module Sprockets
4
4
  class Manifest
5
- attr_reader :workers
6
-
7
- alias_method :old_initialize, :initialize
8
- def initialize(environment, path, workers=1)
9
- @workers = workers
10
- old_initialize(environment, path)
11
- end
12
-
13
5
  alias_method :compile_with_workers, :compile
14
6
  def compile(*args)
7
+ worker_count = SprocketsDerailleur::worker_count
15
8
  paths_with_errors = {}
16
9
 
17
10
  time = Benchmark.measure do
@@ -30,10 +23,10 @@ module Sprockets
30
23
  end
31
24
  end
32
25
 
33
- logger.warn "Initializing #{@workers} workers"
26
+ logger.warn "Initializing #{worker_count} workers"
34
27
 
35
28
  workers = []
36
- @workers.times do
29
+ worker_count.times do
37
30
  workers << worker(paths)
38
31
  end
39
32
 
@@ -145,4 +138,4 @@ module Sprockets
145
138
  {:read => parent_read, :write => parent_write, :pid => pid}
146
139
  end
147
140
  end
148
- end
141
+ end
@@ -1,5 +1,5 @@
1
1
  module Sprockets
2
2
  module Derailleur
3
- VERSION = "0.0.7"
3
+ VERSION = "0.0.8"
4
4
  end
5
5
  end
@@ -1,5 +1,6 @@
1
1
  require "sprockets-derailleur/version"
2
2
  require "sprockets-derailleur/manifest"
3
+ require "sprockets-derailleur/file_store"
3
4
 
4
5
  module SprocketsDerailleur
5
6
  def self.number_of_processors
@@ -11,7 +12,7 @@ module SprocketsDerailleur
11
12
  # this works for windows 2000 or greater
12
13
  require 'win32ole'
13
14
  wmi = WIN32OLE.connect("winmgmts://")
14
- wmi.ExecQuery("select * from Win32_ComputerSystem").each do |system|
15
+ wmi.ExecQuery("select * from Win32_ComputerSystem").each do |system|
15
16
  begin
16
17
  processors = system.NumberOfLogicalProcessors
17
18
  rescue
@@ -22,4 +23,12 @@ module SprocketsDerailleur
22
23
  end
23
24
  raise "can't determine 'number_of_processors' for '#{RUBY_PLATFORM}'"
24
25
  end
26
+
27
+ def self.worker_count
28
+ worker_count = ENV['SPROCKETS_DERAILLEUR_WORKER_COUNT'].to_i
29
+ return worker_count if worker_count > 0
30
+ number_of_processors
31
+ rescue
32
+ 1
33
+ end
25
34
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sprockets-derailleur
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-04 00:00:00.000000000 Z
12
+ date: 2014-01-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sprockets
@@ -40,6 +40,7 @@ files:
40
40
  - README.md
41
41
  - Rakefile
42
42
  - lib/sprockets-derailleur.rb
43
+ - lib/sprockets-derailleur/file_store.rb
43
44
  - lib/sprockets-derailleur/manifest.rb
44
45
  - lib/sprockets-derailleur/version.rb
45
46
  - sprockets-derailleur.gemspec