-
-
Notifications
You must be signed in to change notification settings - Fork 751
Expand file tree
/
Copy pathexternal_pipeline.rb
More file actions
89 lines (68 loc) · 2.46 KB
/
external_pipeline.rb
File metadata and controls
89 lines (68 loc) · 2.46 KB
1
2
3
4
5
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
85
86
87
88
89
class Middleman::Extensions::ExternalPipeline < ::Middleman::Extension
self.supports_multiple_instances = true
option :name, nil, 'The name of the pipeline', required: true
option :command, nil, 'The command to initialize', required: true
option :source, nil, 'Path to merge into sitemap', required: true
option :latency, 0.25, 'Latency between refreshes of source'
option :disable_background_execution, false, "Don't run the command in a separate background thread"
option :ignore_exit_code, false, 'Ignore exit code for restart or stop of a command'
def initialize(app, config={}, &block)
super
return if app.mode?(:config)
require 'servolux'
require 'thread'
require 'fileutils'
source_path = File.expand_path(options[:source], app.root)
# Make sure it exists, or `listen` will explode.
::FileUtils.mkdir_p(source_path)
@watcher = app.files.watch :source,
path: source_path,
latency: options[:latency],
frontmatter: false
@current_thread = nil
app.reload(&method(:reload!))
logger.info "== Executing: `#{options[:command]}`"
if app.build? || options[:disable_background_execution]
watch_command!(false)
@watcher.poll_once!
else
watch_command!(true)
end
end
def reload!
if @current_thread
logger.info "== Stopping: `#{options[:command]}`"
@current_thread.stop
@current_thread = nil
end
end
def watch_command!(async)
@current_thread = ::Servolux::Child.new(
command: options[:command],
suspend: 2
)
@current_thread.start
watch_thread = Thread.new do
while buf = @current_thread.io.gets
without_newline = buf.sub(/\n$/, '')
logger.info "== External: #{without_newline}" unless without_newline.empty?
end
@current_thread.wait
if !options[:ignore_exit_code] && !@current_thread.exitstatus.nil? && @current_thread.exitstatus != 0
logger.error '== External: Command failed with non-zero exit status'
exit(1)
end
end
watch_thread.join unless async
rescue ::Errno::ENOENT => e
logger.error "== External: Command failed with message: #{e.message}"
exit(1)
end
private
def print_command(stdout)
while buf = stdout.gets
without_newline = buf.sub(/\n$/, '')
logger.info "== External: #{without_newline}" unless without_newline.empty?
end
end
end