Class: Ci::JobRedisState
- Inherits:
-
Object
- Object
- Ci::JobRedisState
- Includes:
- ActiveModel::Attributes, ActiveModel::Model
- Defined in:
- app/models/ci/job_redis_state.rb
Defined Under Namespace
Classes: RedisBool
Constant Summary collapse
- UnpersistedJobError =
Class.new(StandardError)
- REDIS_TTL =
300- REDIS_KEY =
"ci_job_redis_state:{%{project_id}}:%{job_id}"
Class Method Summary collapse
- .find_or_initialize_by(job:) ⇒ Object
- .redis_key(project_id, job_id) ⇒ Object
- .with_redis(&block) ⇒ Object
Instance Method Summary collapse
- #enqueue_immediately? ⇒ Boolean
-
#initialize(attributes = {}) ⇒ JobRedisState
constructor
A new instance of JobRedisState.
-
#save ⇒ Object
We need a job_id to save the record in Redis.
- #update(values = {}) ⇒ Object
Constructor Details
#initialize(attributes = {}) ⇒ JobRedisState
Returns a new instance of JobRedisState.
44 45 46 47 |
# File 'app/models/ci/job_redis_state.rb', line 44 def initialize(attributes = {}) @job = attributes.delete(:job) super end |
Class Method Details
.find_or_initialize_by(job:) ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'app/models/ci/job_redis_state.rb', line 25 def self.find_or_initialize_by(job:) with_redis do |redis| redis_attributes = redis.hgetall(redis_key(job.project_id, job.id)) deserialized_attrs = redis_attributes.each.with_object({}) do |(key, value), result| result[key] = attribute_types[key].deserialize(value) end new(deserialized_attrs.merge(job: job)) end end |
.redis_key(project_id, job_id) ⇒ Object
36 37 38 |
# File 'app/models/ci/job_redis_state.rb', line 36 def self.redis_key(project_id, job_id) format(REDIS_KEY, project_id: project_id, job_id: job_id) end |
.with_redis(&block) ⇒ Object
40 41 42 |
# File 'app/models/ci/job_redis_state.rb', line 40 def self.with_redis(&block) ::Gitlab::Redis::SharedState.with(&block) end |
Instance Method Details
#enqueue_immediately? ⇒ Boolean
68 69 70 |
# File 'app/models/ci/job_redis_state.rb', line 68 def enqueue_immediately? enqueue_immediately end |
#save ⇒ Object
We need a job_id to save the record in Redis
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'app/models/ci/job_redis_state.rb', line 50 def save raise UnpersistedJobError unless job.persisted? with_redis do |redis| redis.multi do |transaction| transaction.hset(redis_key, attributes_for_redis) transaction.expire(redis_key, REDIS_TTL) end end true end |
#update(values = {}) ⇒ Object
63 64 65 66 |
# File 'app/models/ci/job_redis_state.rb', line 63 def update(values = {}) assign_attributes(values) save end |