Class: Stellwerk::License

Inherits:
Object
  • Object
show all
Defined in:
lib/stellwerk/license.rb

Overview

License validation and caching

Validates API keys against the Stellwerk server and caches the result. Supports offline grace periods when the server is unreachable.

Thread-safe via Mutex.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize ⇒ License

Returns a new instance of License.



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/stellwerk/license.rb', line 15

def initialize
  @mutex = Mutex.new
  @validated = false
  @valid = false
  @plan = nil
  @execution_limit = 0
  @executions_used = 0
  @expires_at = nil
  @offline_since = nil
  @offline_executions = 0
end

Instance Attribute Details

#execution_limit ⇒ Object (readonly)

Cached license data attributes



13
14
15
# File 'lib/stellwerk/license.rb', line 13

def execution_limit
  @execution_limit
end

#executions_used ⇒ Object (readonly)

Cached license data attributes



13
14
15
# File 'lib/stellwerk/license.rb', line 13

def executions_used
  @executions_used
end

#expires_at ⇒ Object (readonly)

Cached license data attributes



13
14
15
# File 'lib/stellwerk/license.rb', line 13

def expires_at
  @expires_at
end

#plan ⇒ Object (readonly)

Cached license data attributes



13
14
15
# File 'lib/stellwerk/license.rb', line 13

def plan
  @plan
end

Instance Method Details

#offline? ⇒ Boolean

Check if in offline mode

Returns:

  • (Boolean)


97
98
99
100
101
# File 'lib/stellwerk/license.rb', line 97

def offline?
  @mutex.synchronize do
    !@offline_since.nil?
  end
end

#record_offline_execution ⇒ Object

Record an offline execution during grace period



79
80
81
82
83
# File 'lib/stellwerk/license.rb', line 79

def record_offline_execution
  @mutex.synchronize do
    @offline_executions += 1
  end
end

#refresh_if_needed ⇒ Boolean

Refresh the license if TTL has expired

Returns:

  • (Boolean) —

    Whether the license is valid after refresh



54
55
56
57
58
59
60
# File 'lib/stellwerk/license.rb', line 54

def refresh_if_needed
  @mutex.synchronize do
    return @valid if within_cache_ttl?

    validate_internal!
  end
end

#reset! ⇒ Object

Reset the license state



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/stellwerk/license.rb', line 104

def reset!
  @mutex.synchronize do
    @validated = false
    @valid = false
    @plan = nil
    @execution_limit = 0
    @executions_used = 0
    @expires_at = nil
    @offline_since = nil
    @offline_executions = 0
  end
end

#update_executions_used(count) ⇒ Object

Update execution count from server response

Parameters:

  • count (Integer) —

    Current execution count from server



88
89
90
91
92
# File 'lib/stellwerk/license.rb', line 88

def update_executions_used(count)
  @mutex.synchronize do
    @executions_used = count
  end
end

#valid? ⇒ Boolean

Check if the current license is valid (cached)

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
# File 'lib/stellwerk/license.rb', line 41

def valid?
  @mutex.synchronize do
    return false unless @validated
    return @valid if within_cache_ttl?
    return in_offline_grace? if @offline_since

    @valid
  end
end

#validate! ⇒ Boolean

Validate the API key against the server

Returns:

  • (Boolean) —

    Whether the license is valid

Raises:

  • (LicenseError) —

    If API key is missing

  • (ApiError) —

    On server communication failure (after grace period)



32
33
34
35
36
# File 'lib/stellwerk/license.rb', line 32

def validate!
  @mutex.synchronize do
    validate_internal!
  end
end

#within_limits?(pending_count = 1) ⇒ Boolean

Check if executions are within allowed limits

Parameters:

  • pending_count (Integer) (defaults to: 1) —

    Number of pending executions to check

Returns:

  • (Boolean)


66
67
68
69
70
71
72
73
74
75
76
# File 'lib/stellwerk/license.rb', line 66

def within_limits?(pending_count = 1)
  @mutex.synchronize do
    return false unless @valid || in_offline_grace?

    if @offline_since
      @offline_executions + pending_count <= offline_grace_executions
    else
      @executions_used + pending_count <= @execution_limit
    end
  end
end