Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added heartbeat internal method with tests
  • Loading branch information
tombh committed Feb 23, 2013
1 parent 77b2cfb commit 1a42dd3
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 0 deletions.
9 changes: 9 additions & 0 deletions apiserver/api/internal.js
Expand Up @@ -139,5 +139,14 @@ module.exports = {

cb();
}
},

// Increment an app's heartbeat by 1
incrementHeartbeat: {
routePath : '/internal/incrementHeartbeat',
payloadSource: 'body',
method: 'POST',
okayCode: 200,
errorCode: 404
}
};
33 changes: 33 additions & 0 deletions postgres/openruko_api/functions/increment_heartbeat.pgsql
@@ -0,0 +1,33 @@
CREATE OR REPLACE FUNCTION increment_heartbeat(p_instance_id text)
RETURNS SETOF integer AS
$BODY$
DECLARE
v_heartbeats integer;
v_app_id integer;
BEGIN

SELECT app_id FROM instance WHERE id = p_instance_id
LIMIT 1
INTO v_app_id;

SELECT heartbeats FROM app WHERE id = v_app_id
LIMIT 1
INTO v_heartbeats;

IF v_heartbeats IS NOT null THEN
UPDATE app
SET heartbeats = heartbeats + 1
WHERE id = v_app_id;
ELSE
UPDATE app
SET heartbeats = 1
WHERE id = v_app_id;
END IF;

RETURN QUERY SELECT app.heartbeats FROM app WHERE id = v_app_id;

END;
$BODY$
LANGUAGE plpgsql VOLATILE
-- vim: set filetype=pgsql :

1 change: 1 addition & 0 deletions postgres/openruko_data/tables/0.1.1.do.app.pgsql
@@ -0,0 +1 @@
ALTER TABLE app ADD COLUMN heartbeats integer;
14 changes: 14 additions & 0 deletions test/internal-provisionJob.js
Expand Up @@ -226,6 +226,20 @@ describe('internal provisionJob', function(){
}, 30);
});

it('it should handle heartbeats and increment an app\'s total heartbeats', function(done){
var instance_id;
preReceiveMock('myApp', function(){
dynohostMock.getJobs(function(err, data){
if(err) return done(err);
instance_id = data[0].instance_id;
dynohostMock.incrementHeartbeat(instance_id, function(err, body){
expect(body.rows[0].increment_heartbeat).to.be.greaterThan(0);
done();
});
});
});
})

describe('when updating the repo', function(){
beforeEach(function(done){
preReceiveMock('myApp', done);
Expand Down
11 changes: 11 additions & 0 deletions test/mock/dynohost.js
Expand Up @@ -15,6 +15,17 @@ exports.updateState = function(appId, dynoId, instanceId, state, cb){
}, cb);
};

exports.incrementHeartbeat = function(instanceId, cb){
request.post({
url: base + '/internal/incrementHeartbeat',
json: {
instanceId: instanceId
}
}, function(err, resp, body){
cb(err, body);
});
};

exports.getJobs = function(cb){
request({
url: base + '/internal/getjobs',
Expand Down

0 comments on commit 1a42dd3

Please sign in to comment.