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, environment: nil, auth_client: nil) ⇒ JobsClient

Returns a new instance of JobsClient.

Parameters:

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

    API key. When omitted, resolved from SMPLKIT_API_KEY or ~/.smplkit.

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

    Named ~/.smplkit profile section.

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

    Base domain for API requests (default “smplkit.com”).

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

    URL scheme (default “https”).

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

    Enable SDK debug logging.

  • extra_headers (Hash, nil) (defaults to: nil)

    Extra headers attached to every request.

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

    Default environment for environment-scoped operations — the environment a one-off job created through this client is born in, the default a manual run executes in, and the default scope for runs.list. nil leaves these unset (the credential’s permitted environment is implied where unambiguous).

  • auth_client (Object, nil) (defaults to: nil)

    Internal — a pre-built transport supplied by a top-level client so the jobs surface shares one connection pool. Not for direct use.



154
155
156
157
158
159
160
161
162
163
164
# File 'lib/smplkit/jobs/client.rb', line 154

def initialize(api_key = nil, profile: nil, base_domain: nil, scheme: nil,
               debug: nil, extra_headers: nil, environment: 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
  )
  @environment = environment
  @api = SmplkitGeneratedClient::Jobs::JobsApi.new(auth)
  @runs = RunsClient.new(SmplkitGeneratedClient::Jobs::RunsApi.new(auth), environment: environment)
  @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).



136
137
138
# File 'lib/smplkit/jobs/client.rb', line 136

def runs
  @runs
end

Class Method Details

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

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



173
174
175
176
177
178
179
180
# File 'lib/smplkit/jobs/client.rb', line 173

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)


289
290
291
292
293
294
# File 'lib/smplkit/jobs/client.rb', line 289

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), x_smplkit_environment: job.birth_environment) }
  Job.from_resource(resp.data, client: self)
end

#_update_job(job) ⇒ Object

Header values come back in plaintext on the GET path, so a fetched job round-trips through this full-replace PUT with its header values intact — no need to re-enter secrets.

Raises:

  • (ArgumentError)


304
305
306
307
308
309
# File 'lib/smplkit/jobs/client.rb', line 304

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), x_smplkit_environment: @environment) }
  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.



168
169
170
# File 'lib/smplkit/jobs/client.rb', line 168

def close
  nil
end

#delete(id) ⇒ nil

Delete a job by its id.

Parameters:

  • id (String)

    Identifier of the job to delete.

Returns:

  • (nil)


253
254
255
256
# File 'lib/smplkit/jobs/client.rb', line 253

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:



244
245
246
247
# File 'lib/smplkit/jobs/client.rb', line 244

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 (the server-derived roll-up across environments).

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

    1-based page number to return.

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

    Items per page.

Returns:



229
230
231
232
233
234
235
236
237
# File 'lib/smplkit/jobs/client.rb', line 229

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, environments: nil, concurrency_policy: "ALLOW", environment: nil) ⇒ 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”. The schedule is environment-agnostic.

  • configuration (Smplkit::Jobs::HttpConfig)

    The base HTTP request the job performs.

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

    Optional free-text description.

  • environments (Hash{String => Smplkit::Jobs::JobEnvironment, Hash}, nil) (defaults to: nil)

    Per-environment overrides for a recurring job, keyed by environment key — each a Smplkit::Jobs::JobEnvironment, or a plain hash (+{ enabled: true }+, optionally with a :configuration HttpConfig override). A recurring job fires only in environments enabled here. Ignored for a one-off job, which is born in environment below.

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

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

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

    For a one-off job (+“now”+ / datetime schedule), the environment it is born in. Defaults to the client’s configured environment. Ignored for a recurring job.

Returns:



207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/smplkit/jobs/client.rb', line 207

def new(id, name:, schedule:, configuration:, description: nil,
        environments: nil, concurrency_policy: "ALLOW", environment: nil)
  Job.new(
    self,
    id: id,
    name: name,
    schedule: schedule,
    configuration: configuration,
    description: description,
    environments: Jobs.normalize_environments(environments),
    concurrency_policy: concurrency_policy,
    birth_environment: environment.nil? ? @environment : environment
  )
end

#run(id, environment: nil) ⇒ Smplkit::Jobs::Run

Trigger one immediate, manual run of a job, ignoring its schedule.

This starts an ad-hoc run right now in addition to any scheduled runs; it does not alter the job’s schedule. To read or act on existing runs, use client.jobs.runs.

Parameters:

  • id (String)

    Identifier of the job to run.

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

    Environment the manual run executes in. Defaults to the client’s configured environment; when the job is enabled in exactly one environment that environment is used, and a single-environment credential implies it.

Returns:



271
272
273
274
275
# File 'lib/smplkit/jobs/client.rb', line 271

def run(id, environment: nil)
  env = environment.nil? ? @environment : environment
  resp = Jobs.call_api { @api.run_job_now(id, x_smplkit_environment: env) }
  Run.from_resource(resp.data, runs: @runs)
end

#usageSmplkit::Jobs::Usage

Current-period usage counters for the account.



280
281
282
283
# File 'lib/smplkit/jobs/client.rb', line 280

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