diff --git a/services/session.go b/services/session.go index 71400b3..26f3482 100644 --- a/services/session.go +++ b/services/session.go @@ -23,30 +23,35 @@ func NewSession() (*types.Session, error) { sessions[s.Id] = s // Schedule cleanup of the session - time.AfterFunc(1*time.Minute, func() { + time.AfterFunc(1*time.Hour, func() { + s = GetSession(s.Id) + log.Printf("Starting clean up of session [%s]\n", s.Id) for _, i := range s.Instances { if err := DeleteContainer(i.Name); err != nil { log.Println(err) } } - DeleteNetwork(s.Id) + if err := DeleteNetwork(s.Id); err != nil { + log.Println(err) + } + delete(sessions, s.Id) + log.Printf("Cleaned up session [%s]\n", s.Id) }) if err := CreateNetwork(s.Id); err != nil { return nil, err } - //TODO: Schedule deletion after an hour - return s, nil } func GetSession(sessionId string) *types.Session { //TODO: Use redis - s := sessions[sessionId] - if instances[sessionId] != nil { + s, found := sessions[sessionId] + if found { s.Instances = instances[sessionId] + return s + } else { + return nil } - - return s } diff --git a/www/assets/app.js b/www/assets/app.js index 7daf9c5..0f94a94 100644 --- a/www/assets/app.js +++ b/www/assets/app.js @@ -8,16 +8,16 @@ $scope.instances = []; $scope.selectedInstance = null; - $scope.showAlert = function(title, content) { - $mdDialog.show( - $mdDialog.alert() - .parent(angular.element(document.querySelector('#popupContainer'))) - .clickOutsideToClose(true) - .title(title) - .textContent(content) - .ok('Got it!') - ); - } + $scope.showAlert = function(title, content) { + $mdDialog.show( + $mdDialog.alert() + .parent(angular.element(document.querySelector('#popupContainer'))) + .clickOutsideToClose(true) + .title(title) + .textContent(content) + .ok('Got it!') + ); + } $scope.newInstance = function() { $http({ @@ -48,11 +48,14 @@ if ($scope.instances.length) { $scope.showInstance($scope.instances[0]); } + + // Since session exists, we check it still exists every 10 seconds + $scope.checkHandler = setInterval(checkSession, 10*1000); }, function(response) { - if (response.status == 404) { - document.write('session not found'); - return - } + if (response.status == 404) { + document.write('session not found'); + return + } }); } @@ -79,5 +82,17 @@ } $scope.getSession($scope.sessionId); + + function checkSession() { + $http({ + method: 'GET', + url: '/sessions/' + $scope.sessionId, + }).then(function(response) {}, function(response) { + if (response.status == 404) { + clearInterval($scope.checkHandler); + $scope.showAlert('Session timedout!', 'Your session has expire and all your instances has been deleted.') + } + }); + } }]); })(); diff --git a/www/index.html b/www/index.html index 7bd6c30..68cb19a 100644 --- a/www/index.html +++ b/www/index.html @@ -40,6 +40,9 @@

Add instances to your playground.

+

+ Sessions and all their instances are deleted after 1 hour. +