This repository provides a template for creating JavaScript jobs for the SGNL Job Service.
- Use this template to create a new repository
- Clone your new repository locally
- Install dependencies:
npm install - Modify
src/script.mjswith your job logic - Update
metadata.yamlwith your job schema - Test locally:
npm run dev - Run tests:
npm test - Build:
npm run build - Release: Create a git tag and push
# Run the script locally with mock data
npm run dev
# Run unit tests
npm test
# Watch mode for development
npm run test:watch
npm run build:watch
# Validate metadata
npm run validate
# Lint code
npm run lint
npm run lint:fixsrc/script.mjs- Main job implementation (⚠️ Edit this!)metadata.yaml- Job schema and configuration (⚠️ Edit this!)tests/script.test.js- Unit testsdist/index.js- Built script (generated bynpm run build)scripts/- Development utilities
- Update job name and description in
metadata.yaml - Define input parameters in
metadata.yaml - Define output schema in
metadata.yaml - Implement
invokehandler insrc/script.mjs - Update test mock data in
tests/script.test.js - Update README with job-specific documentation
- Implement
errorhandler for error recovery - Implement
halthandler for graceful shutdown - Add additional test cases
- Customize development runner in
scripts/dev-runner.js
Your script must export a default object with these handlers:
Main execution logic for your job.
invoke: async (params, context) => {
// Your job logic here
return {
status: 'success',
// ... other outputs
};
}Error recovery logic when invoke fails.
error: async (params, context) => {
// params.error contains the original error
// Attempt recovery or cleanup
return {
status: 'recovered',
// ... recovery results
};
}Graceful shutdown when job is cancelled or times out.
halt: async (params, context) => {
// params.reason contains halt reason
// Clean up resources, save partial progress
return {
status: 'halted',
cleanup_completed: true
};
}The context parameter provides access to:
{
env: {
ENVIRONMENT: "production",
// ... other environment variables
},
secrets: {
API_KEY: "secret-key",
// ... other secrets
},
outputs: {
"previous-job-step": {
// ... outputs from previous jobs in workflow
}
}
}Tests are in tests/script.test.js. Update the mock data to match your job's inputs:
const params = {
target: 'your-target',
action: 'your-action'
// ... other inputs
};Use npm run dev to test your script locally with mock data. Update scripts/dev-runner.js to customize the test parameters.
- Ensure tests pass:
npm test - Validate metadata:
npm run validate - Build distribution:
npm run build - Create git tag:
git tag v1.0.0 - Push to GitHub:
git push origin v1.0.0
Reference your job in a JobSpec:
{
"id": "my-job-123",
"type": "nodejs-22",
"script": {
"repository": "github.com/your-org/your-job-repo",
"version": "v1.0.0",
"type": "nodejs"
},
"script_inputs": {
"target": "[email protected]",
"action": "create"
},
"environment": {
"ENVIRONMENT": "production"
}
}