Skip to content

Commit

Permalink
Merge pull request #74 from vtex/feature/kill-websocket
Browse files Browse the repository at this point in the history
Replace websocket with http keep-alive
  • Loading branch information
amoreira committed Nov 5, 2015
2 parents b693e0a + 8f697f0 commit 546aa0c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 34 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@
"vtexsay": "^1.0.3",
"webpack": "^1.12.2",
"webpack-dev-middleware": "^1.2.0",
"webpack-hot-middleware": "^2.4.1",
"signalr-client": "0.0.16"
"webpack-hot-middleware": "^2.4.1"
},
"preferGlobal": true,
"devDependencies": {
Expand Down
78 changes: 46 additions & 32 deletions src/lib/watch.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ fileManager = require './file-manager'
tinylr = require 'tiny-lr'
crypto = require 'crypto'
net = require 'net'
signalR = require 'signalr-client'

class Watcher
ChangeAction:
Expand All @@ -20,14 +19,27 @@ class Watcher

constructor: (@app, @vendor, @credentials, @isServerSet) ->
@endpoint = 'http://api.beta.vtex.com'
@acceptHeader = 'application/vnd.vtex.gallery.v0+json'
@acceptHeader = 'application/vnd.vtex.workspaces.v0+json'
@sandbox = @credentials.email
@workspace = "sb_#{@credentials.email}"
@lrRun 35729

if process.platform is 'win32'
rl = require('readline').createInterface
input: process.stdin
output: process.stdout

rl.on 'SIGINT', ->
process.emit 'SIGINT'

process.on 'SIGINT', =>
console.log '\nExiting...'
@deactivateSandbox()

watch: =>
root = process.cwd()
usePolling = (process.platform is 'win32') ? false
@connectToSignalR()
@activateSandbox()
fileManager.listFiles().then (result) =>
deferred = Q.defer()
@endpoint = result.endpoint if result.endpoint
Expand Down Expand Up @@ -256,41 +268,43 @@ class Watcher
@lr.listen port
.listen port

connectToSignalR: =>
client = new signalR.client 'http://workspaces.beta.vtex.com/signalr', ['SandboxStateHub'], 2, true

client.headers['Authorization'] = "token #{@credentials.token}"
client.headers['x-vtex-app'] = "#{@vendor}.#{@app}"
client.headers['x-vtex-account'] = @credentials.account
client.headers['x-vtex-user'] = @credentials.email

client.on 'SandboxStateHub', 'Abort', (msg) ->
console.log msg
client.end()
activateSandbox: =>
deferred = Q.defer()
options =
url: "#{@endpoint}/#{@credentials.account}/workspaces/#{@workspace}/" +
"sandboxes/#{@vendor}/#{@credentials.email}/apps/#{@app}"
method: 'PUT'
headers:
Authorization: "token #{@credentials.token}"
Accept: @acceptHeader
'Content-Type': 'application/json'

client.serviceHandlers.connected = (conn) ->
return true
request options, (error, response) ->
if error or response.statusCode isnt 200
deferred.reject()
console.log error or response.body.message
process.exit 1

if process.platform is 'win32'
rl = require('readline').createInterface
input: process.stdin,
output: process.stdout
deferred.resolve()

rl.on 'SIGINT', ->
process.emit 'SIGINT'
setTimeout @activateSandbox, 30000
deferred.promise

process.on 'SIGINT', =>
client.end()
@exitAfterDisconnection client
deactivateSandbox: =>
options =
url: "#{@endpoint}/#{@credentials.account}/workspaces/#{@workspace}/" +
"sandboxes/#{@vendor}/#{@credentials.email}/apps/#{@app}"
method: 'DELETE'
headers:
Authorization: "token #{@credentials.token}"
Accept: @acceptHeader
'Content-Type': 'application/json'

client.start()
request options, (error, response) ->
if error or response.statusCode isnt 204
console.log error or response.body.message

exitAfterDisconnection: (client, counter = 0) =>
if client.state.desc is 'disconnected' or counter is 10
process.exit()
else
if counter is 0 then console.log '\nExiting...'
setTimeout @exitAfterDisconnection, 300, client, counter + 1
process.exit 1

module.exports = Watcher

0 comments on commit 546aa0c

Please sign in to comment.