Skip to content

k.site.job / runJob / getJob ​

CRUD management, manual triggering, and status queries for site scheduled jobs (Admin: Scheduled Jobs)

Execution ContextPrerequisitesTypical Use
KScript API / Server scriptsCurrent site context; applies to site-level jobs (not module module.config tasks)Create/update scheduled site jobs programmatically, trigger runs, and query status

Quick Start ​

List all scheduled jobs configured on the current site and inspect their active flags and schedule cadence:

js
k.api.get("jobsOverview", () => {
    const jobs = k.site.job.list()
    return {
        count: jobs.length,
        jobs: jobs.map((j) => ({
            name: j.name,
            active: j.active,
            repeat: j.repeat,
            frequence: j.frequence
        }))
    }
})

Quick Reference ​

Method / PropertyDescriptionReturns
k.site.job.list()Lists all scheduled jobs in the site (created date descending)SiteJob[]
k.site.job.get(nameOrId)Retrieves a single scheduled job by name or GUID IDSiteJob | null
k.site.job.add(job)Creates a scheduled job (name is required and unique)void
k.site.job.update(job)Overwrites an existing job's schedule and code fields (no partial merge; resets finish = false)void
k.site.job.delete(nameOrId)Deletes a scheduled job by name or IDvoid
k.site.runJob(name)Triggers the specified job asynchronously for an immediate runvoid
k.site.getJob(name)Queries live isRunning status and lastExecuteTimeSiteJobStatus

Detailed Methods ​

k.site.job Repository CRUD ​

Use k.site.job to list, inspect, create, update, or remove scheduled jobs at runtime:

MethodParameterReturnsDescription
list()NoneSiteJob[]Returns all scheduled jobs in the current site (ordered by creation date descending)
get(nameOrId)nameOrId: stringSiteJob | nullGets a job by name or ID; returns null / undefined when not found
add(job)job: Partial<SiteJob> & { name: string; code: string }voidCreates a new scheduled job (name is required and must be unique)
update(job)job: SiteJobvoidOverwrites code, active, repeat, frequence, frequenceUnit, and startTime, and resets finish = false (throws if the job does not exist)
delete(nameOrId)nameOrId: stringvoidDeletes a scheduled job by name or ID

Returns: list() returns SiteJob[], get() returns SiteJob | null, and add/update/delete return void

update() does not merge partial objects

update(job) does not perform a partial merge: passing a plain JS object (such as { name: "myJob", active: false }) initializes omitted properties to C# SiteJob defaults (code: null, active: true, repeat: false, frequence: "Day", frequenceUnit: 0, startTime: DateTime.MinValue) and overwrites the stored record, and always resets finish to false. Always fetch the full SiteJob via k.site.job.get(nameOrId), mutate the desired fields, and pass that object back to update().

js
k.api.post("manageJob", () => {
    const name = "doc-job-" + Date.now()

    k.site.job.add({
        name,
        code: "const ok = true;",
        active: false,
        repeat: false,
        frequence: "Hour",
        frequenceUnit: 1,
        startTime: new Date().toISOString()
    })

    const created = k.site.job.get(name)
    created.code = "const ok = false;"
    created.repeat = true
    created.frequence = "Minutes"
    created.frequenceUnit = 15
    k.site.job.update(created)

    const updated = k.site.job.get(name)
    k.site.job.delete(name)

    return {
        createdName: created.name,
        updatedCode: updated.code,
        updatedRepeat: updated.repeat,
        deleted: !k.site.job.get(name)
    }
})

k.site.runJob(name) ​

Triggers the specified job asynchronously once (equivalent to clicking Run in the Admin CMS job list). Throws "Job not found" if the job does not exist.

ParameterTypeRequiredDescription
namestringyesJob name or job ID (GUID string)

Returns: void

js
// Trigger a scheduled job by name or ID
k.site.runJob("nightly_sync")

Relationship with the scheduler

runJob does not replace periodic scheduling; recurring jobs still run automatically according to their startTime and frequence. Manual triggers also work while the site is unpublished during debugging.

k.site.getJob(name) ​

Queries the live execution status of a scheduled job. Throws "Job not found" if the job does not exist.

ParameterTypeRequiredDescription
namestringyesJob name or job ID

Returns: SiteJobStatus (isRunning: boolean and lastExecuteTime?: Date; note that returning the raw object directly in an HTTP JSON response serializes its properties as IsRunning / LastExecuteTime, while script property access supports camelCase isRunning / lastExecuteTime).

js
k.api.get("jobStatus", () => {
    const status = k.site.getJob("nightly_sync")
    return {
        isRunning: status.isRunning,
        lastExecuteTime: status.lastExecuteTime
    }
})

Execution Environment and Module Boundaries ​

Execution Environment ​

  • Runs inside the current site's server-side execution context outside the HTTP request pipeline.
  • Executes the job's saved inline KScript code in Module mode with long-task support.
  • Has access to the same server-side k.* APIs as other backend scripts (k.DB, k.content, k.mail, etc.).
  • Execution logs appear in Develop -> Scheduled Jobs under the Completed / Failed tabs.

Difference from k.site.codes ​

Scheduled Jobs (k.site.job)k.site.codes
ManagementAdmin Develop -> Scheduled Jobs or k.site.jobAdmin Develop -> Code or k.site.codes
TriggerScheduler / k.site.runJobHTTP routes, events, page references
Code storageInline code field on the jobbody field on a Code resource

To share logic between scheduled jobs and API endpoints, place shared functions in a CodeBlock and import them explicitly in your job script.

Module Tasks ​

Scheduled tasks defined in a module package's module.config are handled by k.module and the module scheduler rather than k.site.job / k.site.runJob. See k.module.

TypeScript Definitions ​

ts
type RepeatFrequence = 'Day' | 'Hour' | 'Minutes' | 'Week' | 'Month' | 'Second';

interface JobRepository {
  list(): SiteJob[];
  get(nameOrId: string): SiteJob | null;
  add(job: Partial<SiteJob> & { name: string; code: string }): void;
  update(job: SiteJob): void;
  delete(nameOrId: string): void;
}

interface SiteJobStatus {
  isRunning: boolean;
  lastExecuteTime?: Date;
}

interface SiteJob {
  id: string;
  name: string;
  code: string;
  active: boolean;
  finish: boolean;
  repeat: boolean;
  frequence: RepeatFrequence;
  frequenceUnit: number;
  startTime: Date;
  online: boolean;
  version: number;
  creationDate: Date;
  lastModified: Date;
}