summaryrefslogtreecommitdiff
path: root/lib/mattermost
diff options
context:
space:
mode:
authorZ.J. van de Weg <git@zjvandeweg.nl>2016-12-20 20:14:27 +0100
committerZ.J. van de Weg <git@zjvandeweg.nl>2016-12-20 20:14:27 +0100
commit0bd8669f3a606479e3f97683e0bfb178856df422 (patch)
tree2759e85fbaad3100dd07e2bd76854b02b405ccf8 /lib/mattermost
parent2393d30da232db04faa8da9e1a958cec22ffb6e8 (diff)
parentdec1e90e505d9ab9e8b088b6a348f5bec293fed1 (diff)
downloadgitlab-ce-0bd8669f3a606479e3f97683e0bfb178856df422.tar.gz
Merge branch 'zj-mattermost-slash-config' of gitlab.com:gitlab-org/gitlab-ce into zj-mattermost-slash-config
Diffstat (limited to 'lib/mattermost')
-rw-r--r--lib/mattermost/error.rb3
-rw-r--r--lib/mattermost/session.rb40
2 files changed, 35 insertions, 8 deletions
diff --git a/lib/mattermost/error.rb b/lib/mattermost/error.rb
new file mode 100644
index 00000000000..014df175be0
--- /dev/null
+++ b/lib/mattermost/error.rb
@@ -0,0 +1,3 @@
+module Mattermost
+ class Error < StandardError; end
+end
diff --git a/lib/mattermost/session.rb b/lib/mattermost/session.rb
index 7a7912d02fc..38a71061097 100644
--- a/lib/mattermost/session.rb
+++ b/lib/mattermost/session.rb
@@ -26,6 +26,8 @@ module Mattermost
include Doorkeeper::Helpers::Controller
include HTTParty
+ LEASE_TIMEOUT = 60
+
base_uri Settings.mattermost.host
attr_accessor :current_resource_owner, :token
@@ -35,14 +37,16 @@ module Mattermost
end
def with_session
- raise NoSessionError unless create
-
- begin
- yield self
- rescue Errno::ECONNREFUSED
- raise NoSessionError
- ensure
- destroy
+ with_lease do
+ raise NoSessionError unless create
+
+ begin
+ yield self
+ rescue Errno::ECONNREFUSED
+ raise NoSessionError
+ ensure
+ destroy
+ end
end
end
@@ -130,5 +134,25 @@ module Mattermost
response.headers['token']
end
end
+
+ def with_lease
+ lease_uuid = lease_try_obtain
+ raise NoSessionError unless lease_uuid
+
+ begin
+ yield
+ ensure
+ Gitlab::ExclusiveLease.cancel(lease_key, lease_uuid)
+ end
+ end
+
+ def lease_key
+ "mattermost:session"
+ end
+
+ def lease_try_obtain
+ lease = ::Gitlab::ExclusiveLease.new(lease_key, timeout: LEASE_TIMEOUT)
+ lease.try_obtain
+ end
end
end