Class: Smplkit::Jobs::Job
- Inherits:
-
Object
- Object
- Smplkit::Jobs::Job
- Defined in:
- lib/smplkit/jobs/models.rb
Overview
A scheduled unit of work: an HTTP request run on a schedule.
Active-record style: instantiate via client.jobs.new(…), mutate fields directly, and call #save to persist or #delete to remove. Header values in configuration.headers are returned in plaintext on reads, so fetching a job, mutating it, and calling #save preserves its header values without re-entering secrets.
Instance Attribute Summary collapse
-
#concurrency_policy ⇒ String
How overlapping runs are handled.
-
#configuration ⇒ HttpConfig
The HTTP request to perform when the job fires.
-
#created_at ⇒ String?
ISO-8601 timestamp of first persist.
-
#deleted_at ⇒ String?
Timestamp when the job was deleted;
nilfor live jobs. -
#description ⇒ String?
Free-text description.
-
#enabled ⇒ Boolean
Whether the job is scheduling runs.
-
#id ⇒ String
Caller-supplied unique identifier for the job (the resource
id). -
#name ⇒ String
Human-readable name for the job.
-
#next_run_at ⇒ String?
The next scheduled fire time.
-
#schedule ⇒ String
When the job runs: an ISO-8601 datetime (a one-off run), a 5-field cron expression evaluated in UTC (recurring), or the literal “now” (run once, as soon as possible).
-
#type ⇒ String
Job type.
-
#updated_at ⇒ String?
ISO-8601 timestamp of the most recent mutation.
-
#version ⇒ Integer?
Monotonic version counter, bumped on every server-side write.
Class Method Summary collapse
-
.from_resource(resource, client: nil) ⇒ Job
The hydrated job.
Instance Method Summary collapse
- #_apply(other) ⇒ Object private
-
#delete ⇒ nil
(also: #delete!)
Delete this job on the server.
-
#initialize(client = nil, id:, name:, schedule:, configuration:, description: nil, enabled: true, type: "http", concurrency_policy: "ALLOW", next_run_at: nil, created_at: nil, updated_at: nil, deleted_at: nil, version: nil) ⇒ Job
constructor
A new instance of Job.
-
#save ⇒ self
(also: #save!)
Create this job, or full-replace it if it already exists.
Constructor Details
#initialize(client = nil, id:, name:, schedule:, configuration:, description: nil, enabled: true, type: "http", concurrency_policy: "ALLOW", next_run_at: nil, created_at: nil, updated_at: nil, deleted_at: nil, version: nil) ⇒ Job
Returns a new instance of Job.
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/smplkit/jobs/models.rb', line 237 def initialize(client = nil, id:, name:, schedule:, configuration:, description: nil, enabled: true, type: "http", concurrency_policy: "ALLOW", next_run_at: nil, created_at: nil, updated_at: nil, deleted_at: nil, version: nil) @client = client @id = id @name = name @description = description @enabled = enabled @type = type @schedule = schedule @configuration = configuration @concurrency_policy = concurrency_policy @next_run_at = next_run_at @created_at = created_at @updated_at = updated_at @deleted_at = deleted_at @version = version end |
Instance Attribute Details
#concurrency_policy ⇒ String
Returns How overlapping runs are handled. “ALLOW” (the only value) permits them.
216 217 218 |
# File 'lib/smplkit/jobs/models.rb', line 216 def concurrency_policy @concurrency_policy end |
#configuration ⇒ HttpConfig
Returns The HTTP request to perform when the job fires.
212 213 214 |
# File 'lib/smplkit/jobs/models.rb', line 212 def configuration @configuration end |
#created_at ⇒ String?
Returns ISO-8601 timestamp of first persist. nil for an unsaved instance.
224 225 226 |
# File 'lib/smplkit/jobs/models.rb', line 224 def created_at @created_at end |
#deleted_at ⇒ String?
Returns Timestamp when the job was deleted; nil for live jobs.
231 232 233 |
# File 'lib/smplkit/jobs/models.rb', line 231 def deleted_at @deleted_at end |
#description ⇒ String?
Returns Free-text description. nil when unset.
196 197 198 |
# File 'lib/smplkit/jobs/models.rb', line 196 def description @description end |
#enabled ⇒ Boolean
Returns Whether the job is scheduling runs. false pauses without deleting.
200 201 202 |
# File 'lib/smplkit/jobs/models.rb', line 200 def enabled @enabled end |
#id ⇒ String
Caller-supplied unique identifier for the job (the resource id). Unique within the account and immutable; the service returns 409 if another live job already uses this id.
190 191 192 |
# File 'lib/smplkit/jobs/models.rb', line 190 def id @id end |
#name ⇒ String
Returns Human-readable name for the job.
193 194 195 |
# File 'lib/smplkit/jobs/models.rb', line 193 def name @name end |
#next_run_at ⇒ String?
Returns The next scheduled fire time. nil once a one-off job has fired.
220 221 222 |
# File 'lib/smplkit/jobs/models.rb', line 220 def next_run_at @next_run_at end |
#schedule ⇒ String
Returns When the job runs: an ISO-8601 datetime (a one-off run), a 5-field cron expression evaluated in UTC (recurring), or the literal “now” (run once, as soon as possible). A datetime or “now” job disables itself after it fires.
209 210 211 |
# File 'lib/smplkit/jobs/models.rb', line 209 def schedule @schedule end |
#type ⇒ String
Returns Job type. Only “http” is supported today.
203 204 205 |
# File 'lib/smplkit/jobs/models.rb', line 203 def type @type end |
#updated_at ⇒ String?
Returns ISO-8601 timestamp of the most recent mutation.
227 228 229 |
# File 'lib/smplkit/jobs/models.rb', line 227 def updated_at @updated_at end |
#version ⇒ Integer?
Returns Monotonic version counter, bumped on every server-side write.
235 236 237 |
# File 'lib/smplkit/jobs/models.rb', line 235 def version @version end |
Class Method Details
.from_resource(resource, client: nil) ⇒ Job
Returns The hydrated job.
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
# File 'lib/smplkit/jobs/models.rb', line 307 def self.from_resource(resource, client: nil) a = resource.attributes new( client, id: resource.id, name: a.name, description: a.description, enabled: a.enabled.nil? || a.enabled, type: a.type || "http", schedule: a.schedule, configuration: HttpConfig.from_wire(a.configuration), concurrency_policy: a.concurrency_policy || "ALLOW", next_run_at: a.next_run_at, created_at: a.created_at, updated_at: a.updated_at, deleted_at: a.deleted_at, version: a.version ) end |
Instance Method Details
#_apply(other) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/smplkit/jobs/models.rb', line 285 def _apply(other) @id = other.id @name = other.name @description = other.description @enabled = other.enabled @type = other.type @schedule = other.schedule @configuration = other.configuration @concurrency_policy = other.concurrency_policy @next_run_at = other.next_run_at @created_at = other.created_at @updated_at = other.updated_at @deleted_at = other.deleted_at @version = other.version end |
#delete ⇒ nil Also known as: delete!
Delete this job on the server.
277 278 279 280 281 |
# File 'lib/smplkit/jobs/models.rb', line 277 def delete raise "Job was constructed without a client or id; cannot delete" if @client.nil? || @id.nil? @client.delete(@id) end |
#save ⇒ self Also known as: save!
Create this job, or full-replace it if it already exists.
Upsert behavior is driven by #created_at: a job with no created_at is created (POST); otherwise it’s full-replace updated (PUT). After the call, every field is refreshed from the server response (including newly-assigned created_at, version, next_run_at).
265 266 267 268 269 270 271 |
# File 'lib/smplkit/jobs/models.rb', line 265 def save raise "Job was constructed without a client; cannot save" if @client.nil? updated = @created_at.nil? ? @client._create_job(self) : @client._update_job(self) _apply(updated) self end |