-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathuser.rb
More file actions
100 lines (80 loc) · 2.5 KB
/
user.rb
File metadata and controls
100 lines (80 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# frozen_string_literal: true
require 'active_record'
require 'travis/support/encrypted_column'
class User < ActiveRecord::Base
has_one :trial, as: :owner
serialize :github_oauth_token, Travis::EncryptedColumn.new
# These default timeouts, for Users and Organzations, are for limiting workers
# from living forever, and should not be adjusted without checking with
# builders on the infrastructure or reliability teams. Changing them will
# impact our resource usage as well as our ability to rollout new workers.
#
# Note: timeout values are in seconds
#
DEFAULT_SUBSCRIBED_TIMEOUT = 120 * 60
DEFAULT_SPONSORED_TIMEOUT = 50 * 60
def subscription
subs = Subscription.where(owner_id: id, owner_type: 'User')
@subscription ||= subs.where(status: 'subscribed').last || subs.last
end
def subscribed?
subscription.present? && subscription.active?
end
def active_trial?
redis.get("trial:#{login}").to_i > 0
end
def educational?
!!education
end
def paid?
subscribed? || active_trial? || paid_new_plan?
end
def paid_new_plan?
redis_key = "user:#{self.id}:plan"
plan = if redis.exists?(redis_key)
JSON.parse(redis.get(redis_key))
else
billing_client.get_plan(self).to_h
end
return false if plan[:error] || plan['plan_name'].nil?
plan['hybrid'] || !plan['plan_name'].include?('free')
end
def enterprise?
!!context.config[:enterprise]
end
def default_worker_timeout
# When the user is a paid user ("subscribed") or has an active trial, they
# are granted a different default timeout on their jobs.
#
# Note that currently (27/4/18) we are NOT providing timeouts different from
# those enforced by workers themselves, but we plan to sometime in the
# following weeks/months.
#
if enterprise? || paid? || educational?
Travis.logger.info "Default Timeout: DEFAULT_SUBSCRIBED_TIMEOUT for owner=#{id}"
DEFAULT_SUBSCRIBED_TIMEOUT
else
Travis.logger.info "Default Timeout: DEFAULT_SPONSORED_TIMEOUT for owner=#{id}"
DEFAULT_SPONSORED_TIMEOUT
end
end
def preferences
super || {}
end
def keep_netrc?
preferences.key?('keep_netrc') ? preferences['keep_netrc'] : true
end
def uid
"user:#{id}"
end
private
def redis
Travis::Scheduler.context.redis
end
def billing_client
@billing_client ||= Travis::Scheduler::Billing::Client.new(context)
end
def context
Travis::Scheduler.context
end
end