-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.rb
executable file
·74 lines (61 loc) · 1.82 KB
/
worker.rb
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
#!/usr/bin/env ruby
# coding: utf-8
require 'sinatra'
require 'json'
require 'eventmachine'
require 'logger'
class MyApp < Sinatra::Base
@@jobs = []
@@current_job = ""
dir = File.dirname(__FILE__)
COMMAND_SCRIPT = "#{dir}/commands"
set :environment, :production
set :port, 9292
configure do
enable :logging
file = File.new("#{settings.root}/log/#{settings.environment}.log", 'a+')
file.sync = true
use Rack::CommonLogger, file
end
get "/" do
tasks = @@jobs.map {|x| x[:app]}
erb "現在処理中のジョブ:#{@@current_job} <br> 処理待ち:#{tasks}"
end
post "/" do
params = JSON.parse( request.body.read )
logger.info params.to_s
if params["pull_request"]
@app = params["pull_request"]["head"]["repo"]["name"]
@url = params["pull_request"]["head"]["repo"]["ssh_url"]
@ref = "refs/heads/" + params["pull_request"]["head"]["ref"]
else
@app = params["repository"]["name"]
@url = params["repository"]["ssh_url"]
@ref = params["ref"]
end
command = if params["action"] == "closed" or params["deleted"] == "true"
"#{COMMAND_SCRIPT} delete #{@app} #{@ref} #{@url}"
elsif %w( opened reopened synchronize ).include?(params["action"])
"#{COMMAND_SCRIPT} webhook #{@app} #{@ref} #{@url}"
end
@@jobs.push({ :command => command, :app => @ref.sub(/refs\/heads\//, "") }) unless command.nil?
@@jobs.uniq!
logger.info @@jobs.to_s
status 200 && return
end
EM::defer do
loop do
sleep 5
next if @@jobs.empty?
job = @@jobs.shift ## ジョブ1つ取り出す
## job処理する
begin
@@current_job = job[:app]
system(job[:command])
ensure
@@current_job = nil
end
end
end
run! if app_file == $0
end