Module: SpatialFeatures::QueuedSpatialProcessing

Extended by:
ActiveSupport::Concern
Included in:
FeatureImport
Defined in:
lib/spatial_features/has_spatial_features/queued_spatial_processing.rb

Defined Under Namespace

Classes: SpatialProcessingJob

Constant Summary collapse

WARNINGS_CACHE_KEY =

Non-fatal messages from the most recent successful feature import (e.g. parts of the source that were skipped). Stored alongside the status cache so they survive job completion, since successful Delayed::Jobs are deleted and can't be read back.

'feature_update_warnings'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.update_cached_status(record, method_name, state) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/spatial_features/has_spatial_features/queued_spatial_processing.rb', line 28

def self.update_cached_status(record, method_name, state)
  return unless record.has_attribute?(:spatial_processing_status_cache)

  cache = record.spatial_processing_status_cache
  cache[method_name] = state
  record.spatial_processing_status_cache = cache
  record.update_column(:spatial_processing_status_cache, cache) if record.will_save_change_to_spatial_processing_status_cache?
end

Instance Method Details

#clear_feature_update_error_statusObject



60
61
62
63
64
# File 'lib/spatial_features/has_spatial_features/queued_spatial_processing.rb', line 60

def clear_feature_update_error_status
  with_lock do
    SpatialFeatures::QueuedSpatialProcessing.update_cached_status(self, :update_features!, nil) if updating_features_failed?
  end
end

#delay_update_features!(*args, priority: priority_offset + 0, **kwargs) ⇒ Object



47
48
49
# File 'lib/spatial_features/has_spatial_features/queued_spatial_processing.rb', line 47

def delay_update_features!(*args, priority: priority_offset + 0, **kwargs)
  queue_spatial_task('update_features!', *args, priority:, **kwargs)
end

#failed_feature_update_jobsObject

Most recent first: a record that has failed more than once must report why the current attempt failed, not whichever row the database happened to return.



129
130
131
# File 'lib/spatial_features/has_spatial_features/queued_spatial_processing.rb', line 129

def failed_feature_update_jobs
  spatial_processing_jobs('update_features!').where.not(failed_at: nil).order(failed_at: :desc)
end

#feature_update_errorObject



115
116
117
# File 'lib/spatial_features/has_spatial_features/queued_spatial_processing.rb', line 115

def feature_update_error
  (failed_feature_update_jobs.first.try(:last_error) || '').split("\n").first
end

#feature_update_warningsObject



75
76
77
78
# File 'lib/spatial_features/has_spatial_features/queued_spatial_processing.rb', line 75

def feature_update_warnings
  return [] unless has_attribute?(:spatial_processing_status_cache)
  Array(spatial_processing_status_cache[WARNINGS_CACHE_KEY])
end

#queue_update_spatial_cache(*args, priority: priority_offset + 1, **kwargs) ⇒ Object



43
44
45
# File 'lib/spatial_features/has_spatial_features/queued_spatial_processing.rb', line 43

def queue_update_spatial_cache(*args, priority: priority_offset + 1, **kwargs)
  queue_spatial_task('update_spatial_cache', *args, priority:, **kwargs)
end

#queued_feature_update_jobsObject



123
124
125
# File 'lib/spatial_features/has_spatial_features/queued_spatial_processing.rb', line 123

def queued_feature_update_jobs
  spatial_processing_jobs('update_features!').where(failed_at: nil, locked_at: nil)
end

#running_feature_update_jobsObject



119
120
121
# File 'lib/spatial_features/has_spatial_features/queued_spatial_processing.rb', line 119

def running_feature_update_jobs
  spatial_processing_jobs('update_features!').where(failed_at: nil).where.not(locked_at: nil)
end

#spatial_processing_jobs(method_name = nil) ⇒ Object



133
134
135
# File 'lib/spatial_features/has_spatial_features/queued_spatial_processing.rb', line 133

def spatial_processing_jobs(method_name = nil)
  Delayed::Job.where('queue LIKE ?', "#{spatial_processing_queue_name}#{method_name}%")
end

#spatial_processing_status(method_name, use_cache: true) ⇒ Object



94
95
96
97
98
99
# File 'lib/spatial_features/has_spatial_features/queued_spatial_processing.rb', line 94

def spatial_processing_status(method_name, use_cache: true)
  if has_attribute?(:spatial_processing_status_cache)
    update_spatial_processing_status(method_name) unless use_cache
    spatial_processing_status_cache[method_name.to_s]&.to_sym
  end
end

#spatial_processing_status_cacheObject



37
38
39
40
41
# File 'lib/spatial_features/has_spatial_features/queued_spatial_processing.rb', line 37

def spatial_processing_status_cache
  value = super
  return {} unless value.is_a?(Hash)
  return value
end

#store_feature_update_warnings(warnings) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/spatial_features/has_spatial_features/queued_spatial_processing.rb', line 80

def store_feature_update_warnings(warnings)
  return unless has_attribute?(:spatial_processing_status_cache)

  cache = spatial_processing_status_cache
  warnings = Array(warnings).reject(&:blank?)
  if warnings.present?
    cache[WARNINGS_CACHE_KEY] = warnings
  else
    cache.delete(WARNINGS_CACHE_KEY)
  end
  self.spatial_processing_status_cache = cache
  update_column(:spatial_processing_status_cache, cache) if persisted? && will_save_change_to_spatial_processing_status_cache?
end

#update_spatial_processing_status(method_name) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/spatial_features/has_spatial_features/queued_spatial_processing.rb', line 101

def update_spatial_processing_status(method_name)
  latest_job = spatial_processing_jobs(method_name).last

  if !latest_job
    SpatialFeatures::QueuedSpatialProcessing.update_cached_status(self, method_name, nil)
  elsif latest_job.failed_at?
    SpatialFeatures::QueuedSpatialProcessing.update_cached_status(self, method_name, :failure)
  elsif latest_job.locked_at?
    SpatialFeatures::QueuedSpatialProcessing.update_cached_status(self, method_name, :processing)
  else
    SpatialFeatures::QueuedSpatialProcessing.update_cached_status(self, method_name, :queued)
  end
end

#updating_features?(**options) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
55
56
57
58
# File 'lib/spatial_features/has_spatial_features/queued_spatial_processing.rb', line 51

def updating_features?(**options)
  case spatial_processing_status(:update_features!, **options)
  when :queued, :processing
    true
  else
    false
  end
end

#updating_features_failed?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/spatial_features/has_spatial_features/queued_spatial_processing.rb', line 66

def updating_features_failed?
  spatial_processing_status(:update_features!) == :failure
end