k.site.job / runJob / getJob
CRUD management, manual triggering, and status queries for site scheduled jobs (Admin: Scheduled Jobs)
| Execution Context | Prerequisites | Typical Use |
|---|---|---|
| KScript API / Server scripts | Current 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:
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 / Property | Description | Returns |
|---|---|---|
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 ID | SiteJob | 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 ID | void |
k.site.runJob(name) | Triggers the specified job asynchronously for an immediate run | void |
k.site.getJob(name) | Queries live isRunning status and lastExecuteTime | SiteJobStatus |
Detailed Methods
k.site.job Repository CRUD
Use k.site.job to list, inspect, create, update, or remove scheduled jobs at runtime:
| Method | Parameter | Returns | Description |
|---|---|---|---|
list() | None | SiteJob[] | Returns all scheduled jobs in the current site (ordered by creation date descending) |
get(nameOrId) | nameOrId: string | SiteJob | null | Gets a job by name or ID; returns null / undefined when not found |
add(job) | job: Partial<SiteJob> & { name: string; code: string } | void | Creates a new scheduled job (name is required and must be unique) |
update(job) | job: SiteJob | void | Overwrites code, active, repeat, frequence, frequenceUnit, and startTime, and resets finish = false (throws if the job does not exist) |
delete(nameOrId) | nameOrId: string | void | Deletes 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().
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.
| Parameter | Type | Required | Description |
|---|---|---|---|
name | string | yes | Job name or job ID (GUID string) |
Returns: void
// 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.
| Parameter | Type | Required | Description |
|---|---|---|---|
name | string | yes | Job 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).
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 | |
|---|---|---|
| Management | Admin Develop -> Scheduled Jobs or k.site.job | Admin Develop -> Code or k.site.codes |
| Trigger | Scheduler / k.site.runJob | HTTP routes, events, page references |
| Code storage | Inline code field on the job | body 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
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;
}