Skip to content

Instantly share code, notes, and snippets.

@lusis
Created May 19, 2012 07:35
Show Gist options
  • Select an option

  • Save lusis/2729889 to your computer and use it in GitHub Desktop.

Select an option

Save lusis/2729889 to your computer and use it in GitHub Desktop.
The stupid hacky chef-listener I used in my talk at #chefconf
require 'webmachine'
require 'celluloid'
require 'json'
class ChefRunner
include Celluloid
def initialize(name)
@to_run = 0
@running = false
`say "Yo dawg. Yer Chef daemon is starting"`
end
def run_chef
if running?
`say Chef already running`
queue_run
return
end
@running = true
queue_run
while @to_run > 0
`say Running chef`
sleep 10
`say Chef run done`
dequeue_run
end
if @to_run == 0
`say Queue empty. Waiting for work`
end
end
def running?
@running ||= false
end
def queue_run
`say Queueing Chef run`
@to_run += 1
`say Current queue size is: #{@to_run.to_s}`
end
def dequeue_run
`say De-Queueing Chef run`
@to_run = @to_run - 1
`say Current queue size is: #{@to_run.to_s}`
end
end
class ChefResource < Webmachine::Resource
def content_types_provided
[["application/json", :to_json]]
end
def content_types_accepted
[["application/json", :from_json]]
end
def to_json
'{"foo":"bar"}'
end
def from_json
JSON.parse(request.body.to_s)
end
def allowed_methods
%w{POST GET}
end
def allow_missing_post?
true
end
def process_post
puts from_json
Celluloid::Actor[:chef_runner].run_chef!
true
end
end
Webmachine::Configuration.new do |config|
config.port = 8080
end
Webmachine.application.dispatcher.add_route([], ChefResource)
ChefRunner.supervise_as :chef_runner, "ChefRunner"
Webmachine.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment