Class: Spree::UpdateCheck

Inherits:
Object
  • Object
show all
Defined in:
app/services/spree/update_check.rb

Overview

Asks spreecloud.io which Spree releases are newer than the running one, and reports usage with the same request. The answer is cached for a day, so at most one admin request a day waits on it, for no more than the two one-second timeouts. A failed check is cached for an hour, so a passing outage costs one slow request an hour and never hides a release for long.

The version, environment, url and install_id query keys are what Spree 5 sent, and the endpoint still serves 5.x installs with them. They are never renamed; new keys only ever go beside them.

Constant Summary collapse

ENDPOINT =
'https://spreecloud.io/updates.json'.freeze
RELEASES_URL =
'https://github.com/spree/spree/releases/tag/'.freeze
RELEASE_TAG =
/\Av\d+(\.[0-9a-z]+)*\z/
CACHE_TTL =
1.day
FAILURE_TTL =
1.hour
OPEN_TIMEOUT =
1
READ_TIMEOUT =
1
TOTAL_TIMEOUT =

The read timeout applies to each read, not the whole response, so a slowly trickling answer needs an overall deadline as well.

2

Class Method Summary collapse

Class Method Details

.enabled? ⇒ Boolean

Always false in the test environment and on CI servers (which set CI), so no test run ever reaches spreecloud.io; specs stub the request instead.

Returns:

  • (Boolean) —

    false when the operator turned the check off



31
32
33
34
35
36
# File 'app/services/spree/update_check.rb', line 31

def enabled?
  !Rails.env.test? &&
    !env_flag?('CI') &&
    Spree::Config.update_check_enabled &&
    !env_flag?('SPREE_UPDATE_CHECK_DISABLED')
end

.query ⇒ Hash

Returns the query string the endpoint receives.

Returns:

  • (Hash) —

    the query string the endpoint receives



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/services/spree/update_check.rb', line 59

def query
  return { version: Spree.version } unless telemetry_enabled?

  {
    version: Spree.version,
    environment: Rails.env,
    url: Spree::Store.default&.storefront_url,
    install_id: Spree.install_id,
    ruby: RUBY_VERSION,
    rails: Rails.version,
    database: Diagnostics::Stack.database,
    gems: Diagnostics::Stack.spree_gems.keys.join(','),
    dashboard: Diagnostics::Stack.dashboard_served?,
    stores: Diagnostics::Stack.bucket(Spree::Store.count),
    products: Diagnostics::Stack.bucket(Spree::Product.count),
    orders: Diagnostics::Stack.bucket(Spree::Order.complete.count),
    sellers: Diagnostics::Stack.seller_bucket
  }.compact
end

.status ⇒ Spree::UpdateStatus

The cached answer, fetched from the endpoint when there is none.

Returns:



50
51
52
53
54
55
56
# File 'app/services/spree/update_check.rb', line 50

def status
  return Spree::UpdateStatus.new(current_version: Spree.version, enabled: false) unless enabled?

  result = Rails.cache.read(cache_key) || refresh

  Spree::UpdateStatus.new(current_version: Spree.version, checked_at: result[:checked_at], releases: result[:releases])
end

.telemetry_enabled? ⇒ Boolean

When false, the check still runs but sends the running version only.

Returns:

  • (Boolean)


41
42
43
44
45
# File 'app/services/spree/update_check.rb', line 41

def telemetry_enabled?
  Spree::Config.telemetry_enabled &&
    !env_flag?('SPREE_TELEMETRY_DISABLED') &&
    !env_flag?('DO_NOT_TRACK')
end