Skip to content

Instantly share code, notes, and snippets.

@brandon-fryslie
Created January 19, 2015 22:01
Show Gist options
  • Save brandon-fryslie/802e51b1ec385b3eb79c to your computer and use it in GitHub Desktop.
Save brandon-fryslie/802e51b1ec385b3eb79c to your computer and use it in GitHub Desktop.
# Start one task
# Returns a promise
start_task = (env) ->
deferred = Q.defer()
if env.start_message
console.log env.start_message.yellow, 'Doing ', "[ #{env.command.join(' ')} ]".green
proc = nexpect.spawn(env.command, [],
stream: 'all'
verbose: env.verbose
env: get_env env.additional_env
cwd: env.cwd)
.wait env.wait_for, (data) ->
data = env.wait_for.exec?(data) ? [data]
deferred.resolve env.callback data, env
console.log "Started #{env.name}!".green
proc.run (err) -> die 'Error: ' + (err?.message ? err)
deferred.promise
# Starts all tasks, waiting until each previous task finishes starting
# before continuing
run_tasks = (tasks) ->
console.log 'Booting realtime...'
console.log 'VERBOSE MODE'.red if opts.verbose
console.log 'running tasks:', tasks.join(' ').cyan
tasks.reduce (previous, task) ->
previous.then((env) ->
# Apply the config to the env passed out of the callback of the previous task
# This allows us to pass data to subsequent tasks in the pipeline, e.g. zookeeper address
task_opts = clone_apply env, task_config[task](env)
start_task task_opts
, (error) -> die 'Error handler:', error
).fail (error) -> die 'Fail handler:', error
, Q initial_env
task_config =
marshmallow: (env) ->
name: 'Marshmallow'
command: ['lein', 'start-infrastructure']
start_message: 'Starting Marshmallow.'
cwd: "#{ROOTDIR}/marshmallow"
wait_for: /ZOOKEEPER INIT:\s*([\w:]+)|(Connection timed out)/
callback: (data, env) ->
[match, zookeeper_address, error] = data
if error
die 'Marshmallow connection timed out: ', data
unless zookeeper_address
die 'Error: could not find zookeeper address in: ', match
console.log 'Zookeeper Address:', zookeeper_address.magenta
new_env = clone_apply env, zookeeper_address: zookeeper_address
new_env
zuul: (env) ->
env.zookeeper_address ?= DEFAULT_ZOOKEEPER_ADDRESS
name: 'Zuul'
command: ['lein', 'run']
start_message: "Starting Zuul (#{'127.0.0.1:3000'.magenta}) with zk address #{env.zookeeper_address.magenta}."
cwd: "#{ROOTDIR}/zuul"
additional_env: ZOOKEEPER_CONNECT: env.zookeeper_address
wait_for: /Server started!|(Connection timed out)|(Address already in use)|(All host pools marked down.)/
callback: (data, env) ->
[match, timeout_error, address_in_use_error, host_pool_down_error] = data
if timeout_error || address_in_use_error || host_pool_down_error
die 'Error: Zuul failed to connect to Marshmallow:', data.input
env
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment