Class: Sprockets::Manifest

Inherits:
Object
  • Object
show all
Defined in:
lib/sprockets-derailleur/manifest.rb

Instance Method Summary collapse

Instance Method Details

#compile(*args) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/sprockets-derailleur/manifest.rb', line 6

def compile(*args)
  worker_count = SprocketsDerailleur::worker_count
  paths_with_errors = {}

  time = Benchmark.measure do
    paths = environment.each_logical_path(*args).to_a +
      args.flatten.select { |fn| Pathname.new(fn).absolute? if fn.is_a?(String)}

    # Skip all files without extensions, see
    # https://github.com/sstephenson/sprockets/issues/347 for more info
    paths = paths.select do |path|

      if File.extname(path) == ""
        logger.info "Skipping #{path} since it has no extension"
        false
      else
        true
      end
    end

    logger.warn "Initializing #{worker_count} workers"

    workers = []
    worker_count.times do
      workers << worker(paths)
    end

    reads = workers.map{|worker| worker[:read]}
    writes = workers.map{|worker| worker[:write]}

    index = 0
    finished = 0

    loop do
      break if finished >= paths.size

      ready = IO.select(reads, writes)

      ready[0].each do |readable|
        data = Marshal.load(readable)
        assets.merge! data["assets"]
        files.merge! data["files"]
        paths_with_errors.merge! data["errors"]

        finished += 1
      end

      ready[1].each do |write|
        break if index >= paths.size

        Marshal.dump(index, write)
        index += 1
      end
    end

    logger.debug "Cleaning up workers"

    workers.each do |worker|
      worker[:read].close
      worker[:write].close
    end

    workers.each do |worker|
      Process.wait worker[:pid]
    end

    save
  end

  logger.warn "Completed compiling assets (#{(time.real * 100).round / 100.0}s)"

  unless paths_with_errors.empty?
    logger.warn "Asset paths with errors:"

    paths_with_errors.each do |path, message|
      logger.warn "\t#{path}: #{message}"
    end
  end
end

#compile_with_workersObject



5
# File 'lib/sprockets-derailleur/manifest.rb', line 5

alias_method :compile_with_workers, :compile

#worker(paths) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/sprockets-derailleur/manifest.rb', line 86

def worker(paths)
  child_read, parent_write = IO.pipe
  parent_read, child_write = IO.pipe

  pid = fork do
    begin
      parent_write.close
      parent_read.close

      while !child_read.eof?
        path = paths[Marshal.load(child_read)]

        time = Benchmark.measure do
          data = {'assets' => {}, 'files' => {}, 'errors' => {}}

          if asset = find_asset(path)

            data['files'][asset.digest_path] = {
              'logical_path' => asset.logical_path,
              'mtime'        => asset.mtime.iso8601,
              'size'         => asset.length,
              'digest'       => asset.digest
            }
            data['assets'][asset.logical_path] = asset.digest_path

            target = File.join(dir, asset.digest_path)

            if File.exist?(target)
              logger.debug "Skipping #{target}, already exists"
            else
              logger.debug "Writing #{target}"
              asset.write_to target
              asset.write_to "#{target}.gz" if asset.is_a?(BundledAsset)
            end

            Marshal.dump(data, child_write)
          else
            data['errors'][path] = "Not found"
            Marshal.dump(data, child_write)
          end
        end

        logger.debug "Compiled #{path} (#{(time.real * 1000).round}ms, pid #{Process.pid})"
      end
    ensure
      child_read.close
      child_write.close
    end
  end

  child_read.close
  child_write.close

  {:read => parent_read, :write => parent_write, :pid => pid}
end