Class: Smplkit::Jobs::Job

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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_policyString

Returns How overlapping runs are handled. “ALLOW” (the only value) permits them.

Returns:

  • (String)

    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

#configurationHttpConfig

Returns The HTTP request to perform when the job fires.

Returns:

  • (HttpConfig)

    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_atString?

Returns ISO-8601 timestamp of first persist. nil for an unsaved instance.

Returns:

  • (String, nil)

    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_atString?

Returns Timestamp when the job was deleted; nil for live jobs.

Returns:

  • (String, nil)

    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

#descriptionString?

Returns Free-text description. nil when unset.

Returns:

  • (String, nil)

    Free-text description. nil when unset.



196
197
198
# File 'lib/smplkit/jobs/models.rb', line 196

def description
  @description
end

#enabledBoolean

Returns Whether the job is scheduling runs. false pauses without deleting.

Returns:

  • (Boolean)

    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

#idString

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.

Returns:

  • (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

#nameString

Returns Human-readable name for the job.

Returns:

  • (String)

    Human-readable name for the job.



193
194
195
# File 'lib/smplkit/jobs/models.rb', line 193

def name
  @name
end

#next_run_atString?

Returns The next scheduled fire time. nil once a one-off job has fired.

Returns:

  • (String, nil)

    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

#scheduleString

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.

Returns:

  • (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). 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

#typeString

Returns Job type. Only “http” is supported today.

Returns:

  • (String)

    Job type. Only “http” is supported today.



203
204
205
# File 'lib/smplkit/jobs/models.rb', line 203

def type
  @type
end

#updated_atString?

Returns ISO-8601 timestamp of the most recent mutation.

Returns:

  • (String, nil)

    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

#versionInteger?

Returns Monotonic version counter, bumped on every server-side write.

Returns:

  • (Integer, nil)

    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.

Parameters:

  • resource (Object)

    The JSON:API resource (id + attributes).

  • client (JobsClient, nil) (defaults to: nil)

    Client to bind the job to.

Returns:

  • (Job)

    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

#deletenil Also known as: delete!

Delete this job on the server.

Returns:

  • (nil)


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

#saveself 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).

Returns:

  • (self)


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