cxJob
Auto-generated documentation from JSDoc comments
cxJob¶
Push a job to the ScriptForge job queue and manage job execution.
Example¶
import cxJob from "cxJob";
export async function main() {
// Payload for the job
const payload = { message: "Hello, ScriptForge!" };
// Queue the job
const job = await cxJob.add(
process.env.SCRIPTFORGE_ID, // This runs the same scriptforge file, you can replace this with the ID of another file
'simpleJob', // Function that will run
payload // Data to pass
);
// Waiting for the result of the job is optional if you dont need to know when it completes or its result
// Wait for the job to complete and return the result
return await job.result;
}
// Job function: does something simple
export async function simpleJob(data) {
console.log("Job started with data:", data);
// Simulate some work
await new Promise(resolve => setTimeout(resolve, 1000));
console.log("Job completed");
// Optionally return some result, this will replace the job data, you may wish to merge this with data
return { status: "done", processed: data.message };
}
Kind: global class
- cxJob
- .add(id, [fnName], [data], [opts]) ⇒
Promise.<object> - .setData(data) ⇒
Promise.<void> - .setProgress(progress) ⇒
Promise.<void>
- .add(id, [fnName], [data], [opts]) ⇒
cxJob.add(id, [fnName], [data], [opts]) ⇒ Promise.<object>¶
Add Job
Kind: static method of cxJob
Returns: Promise.<object> - Job object with a result Promise that resolves when the job completes
| Param | Type | Default | Description |
|---|---|---|---|
| id | number |
ScriptForge ID of the script to execute | |
| [fnName] | string |
"'main'" |
Function name to execute in the target script |
| [data] | object |
{} |
Data payload to pass to the job function |
| [opts] | object |
{} |
Job options |
| [opts.delay] | number |
Delay in milliseconds before the job starts | |
| [opts.priority] | number |
Job priority (higher runs first) | |
| [opts.attempts] | number |
Number of retry attempts on failure |
Example
// Call a different function but in the same ScriptForge File
const job = await cxJob.add(process.env.SCRIPTFORGE_ID, 'myExecFn', {key: 'value'});
const result = await job.result;
cxJob.setData(data) ⇒ Promise.<void>¶
Set Job Data Updates the data associated with the current job. Call this from within a job function to store intermediate results or state.
Kind: static method of cxJob
| Param | Type | Description |
|---|---|---|
| data | object |
Data to store with the job |
Example
// Inside a job function
export async function myJob(data) {
await setData({ processedItems: 100, totalItems: 500 });
}
cxJob.setProgress(progress) ⇒ Promise.<void>¶
Set Job Progress Updates the progress percentage of the current job. Call this from within a job function to report progress to any listeners.
Kind: static method of cxJob
| Param | Type | Description |
|---|---|---|
| progress | number |
Progress value (0-100) |
Example
// Inside a job function
export async function myJob(data) {
for (let i = 0; i <= 100; i += 10) {
await setProgress(i);
// ... do work ...
}
}