Class: Smplkit::Jobs::JobsClient

Inherits:
Object
  • Object
show all
Defined in:
lib/smplkit/jobs/client.rb

Overview

The active-record entry point is #new: instantiate a draft, mutate fields, then call Smplkit::Jobs::Job#save. Run history and the cancel / rerun run actions live on #runs.

Reachable as client.jobs (Smplkit::Client) or constructed directly —JobsClient.new resolves credentials from ~/.smplkit / env vars.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key = nil, profile: nil, base_domain: nil, scheme: nil, debug: nil, extra_headers: nil, auth_client: nil) ⇒ JobsClient

Returns a new instance of JobsClient.



114
115
116
117
118
119
120
121
122
123
# File 'lib/smplkit/jobs/client.rb', line 114

def initialize(api_key = nil, profile: nil, base_domain: nil, scheme: nil,
               debug: nil, extra_headers: nil, auth_client: nil)
  auth = auth_client || Jobs.jobs_transport(
    api_key: api_key, profile: profile, base_domain: base_domain,
    scheme: scheme, debug: debug, extra_headers: extra_headers
  )
  @api = SmplkitGeneratedClient::Jobs::JobsApi.new(auth)
  @runs = RunsClient.new(SmplkitGeneratedClient::Jobs::RunsApi.new(auth))
  @usage_api = SmplkitGeneratedClient::Jobs::UsageApi.new(auth)
end

Instance Attribute Details

#runsRunsClient (readonly)

Returns Run history and run actions (client.jobs.runs).

Returns:

  • (RunsClient)

    Run history and run actions (client.jobs.runs).



112
113
114
# File 'lib/smplkit/jobs/client.rb', line 112

def runs
  @runs
end

Class Method Details

.open(*args, **kwargs) ⇒ Object

Construct, yield to the block, and close on exit.



132
133
134
135
136
137
138
139
# File 'lib/smplkit/jobs/client.rb', line 132

def self.open(*args, **kwargs)
  client = new(*args, **kwargs)
  begin
    yield client
  ensure
    client.close
  end
end

Instance Method Details

#_create_job(job) ⇒ Object

Raises:

  • (ArgumentError)


226
227
228
229
230
231
# File 'lib/smplkit/jobs/client.rb', line 226

def _create_job(job)
  raise ArgumentError, "Job.id is required on create (caller-supplied key)" if job.id.nil? || job.id.empty?

  resp = Jobs.call_api { @api.create_job(build_create_body(job)) }
  Job.from_resource(resp.data, client: self)
end

#_update_job(job) ⇒ Object

Header values must be re-supplied as plaintext; the GET path redacts them, so a PUT body containing the redacted placeholder would persist that literal. Track real header values client-side and round-trip them.

Raises:

  • (ArgumentError)


239
240
241
242
243
244
# File 'lib/smplkit/jobs/client.rb', line 239

def _update_job(job)
  raise ArgumentError, "cannot update a Job with no id" if job.id.nil?

  resp = Jobs.call_api { @api.update_job(job.id, build_body(job)) }
  Job.from_resource(resp.data, client: self)
end

#closeObject

The generated ApiClient owns Faraday connections that release on GC; there is no explicit shutdown to call.



127
128
129
# File 'lib/smplkit/jobs/client.rb', line 127

def close
  nil
end

#delete(id) ⇒ nil

Soft-delete a job.

Parameters:

  • id (String)

Returns:

  • (nil)


201
202
203
204
# File 'lib/smplkit/jobs/client.rb', line 201

def delete(id)
  Jobs.call_api { @api.delete_job(id) }
  nil
end

#get(id) ⇒ Smplkit::Jobs::Job

Fetch a single job by id. The returned instance is bound to this client, so job.save and job.delete work.

Parameters:

  • id (String)

Returns:



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

def get(id)
  resp = Jobs.call_api { @api.get_job(id) }
  Job.from_resource(resp.data, client: self)
end

#list(enabled: nil, page_number: nil, page_size: nil) ⇒ Array<Smplkit::Jobs::Job>

List jobs for the authenticated account.

Parameters:

  • enabled (Boolean, nil) (defaults to: nil)

    Filter to jobs matching this enabled state.

  • page_number (Integer, nil) (defaults to: nil)

    1-based page number to return.

  • page_size (Integer, nil) (defaults to: nil)

    Items per page.

Returns:



177
178
179
180
181
182
183
184
185
# File 'lib/smplkit/jobs/client.rb', line 177

def list(enabled: nil, page_number: nil, page_size: nil)
  opts = {}
  opts[:filter_enabled] = enabled unless enabled.nil?
  opts[:page_number] = page_number unless page_number.nil?
  opts[:page_size] = page_size unless page_size.nil?

  resp = Jobs.call_api { @api.list_jobs(opts) }
  (resp.data || []).map { |r| Job.from_resource(r, client: self) }
end

#new(id, name:, schedule:, configuration:, description: nil, enabled: true, concurrency_policy: "ALLOW") ⇒ Smplkit::Jobs::Job

Construct an unsaved Smplkit::Jobs::Job bound to this client. Call #save on the returned instance to create it.

Parameters:

  • id (String)

    Caller-supplied unique identifier for the job. Unique within the account and immutable; the service returns 409 if another live job already uses this id.

  • name (String)

    Human-readable name for the job.

  • schedule (String)

    An ISO-8601 datetime, a 5-field UTC cron expression, or the literal “now”.

  • configuration (Smplkit::Jobs::HttpConfig)

    The HTTP request the job performs.

  • description (String, nil) (defaults to: nil)

    Optional free-text description.

  • enabled (Boolean) (defaults to: true)

    Whether the job schedules runs. Defaults true.

  • concurrency_policy (String) (defaults to: "ALLOW")

    How overlapping runs are handled. Defaults to “ALLOW”.

Returns:



157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/smplkit/jobs/client.rb', line 157

def new(id, name:, schedule:, configuration:, description: nil,
        enabled: true, concurrency_policy: "ALLOW")
  Job.new(
    self,
    id: id,
    name: name,
    schedule: schedule,
    configuration: configuration,
    description: description,
    enabled: enabled,
    concurrency_policy: concurrency_policy
  )
end

#run(id) ⇒ Smplkit::Jobs::Run

Trigger one immediate MANUAL run of the job.

Parameters:

  • id (String)

Returns:



210
211
212
213
# File 'lib/smplkit/jobs/client.rb', line 210

def run(id)
  resp = Jobs.call_api { @api.run_job_now(id) }
  Run.from_resource(resp.data)
end

#usageSmplkit::Jobs::Usage

Current-period usage counters for the account.



218
219
220
221
# File 'lib/smplkit/jobs/client.rb', line 218

def usage
  resp = Jobs.call_api { @usage_api.get_usage }
  Usage.from_resource(resp.data)
end