un-inception/clients/javascript/async/examples/async_job_polling.js
russell@unturf.com 331cba42aa feat: Complete 6 additional SDK implementations with fixes and examples
Go Async SDK (clients/go/async/):
- Fixed case-sensitive language detection bug (.R for R language)
- Created go.mod for module management
- Added comprehensive test suite
- Created 3 examples with expected output comments
- Added README with full documentation

Java Sync SDK (clients/java/sync/):
- RENAMED: Unsandbox.java -> Un.java (matches naming convention)
- Updated class name from Unsandbox to Un
- Created pom.xml for Maven build
- Added 6 examples (simple + SDK client versions)
- Created test suite with JUnit 5
- Added README with API documentation

JavaScript Async SDK (clients/javascript/async/):
- Fixed unused import
- Created package.json with ES module support
- Added 5 examples covering all async patterns
- Created 71 tests (all passing)
- Added comprehensive README

PHP Sync SDK (clients/php/sync/):
- RENAMED: Unsandbox.php -> un.php (matches naming convention)
- Created composer.json with PSR-4 autoloading
- Created phpunit.xml for testing
- Added 4 examples with expected output comments
- Created 54 tests across 4 test files
- Added README with full documentation

Ruby Sync SDK (clients/ruby/sync/):
- Created Gemfile and un.gemspec
- Created Rakefile with test task
- Updated examples to actually use the SDK
- Added 4 examples (hello_world, async_job, language_detection, snapshots)
- Created comprehensive test suite with 30+ tests
- Added README with documentation

Rust Sync SDK (clients/rust/sync/):
- Updated Cargo.toml with example declarations
- Created 4 examples (hello_world, fibonacci, multi_language, async_polling)
- Added comprehensive README with API reference
- All dependencies verified correct

All SDKs verified:
- HMAC-SHA256 authentication implemented
- 4-tier credential system (args > env > ~/.unsandbox > ./accounts.csv)
- Expected output comments for pipeline validation
- Proper error handling
- Language detection support
2026-01-15 17:32:24 -05:00

79 lines
2 KiB
JavaScript

#!/usr/bin/env node
/**
* Async Job Polling example for unsandbox JavaScript SDK
*
* Demonstrates fire-and-forget execution with manual job polling.
* Shows how to start an async job and poll for its completion.
*
* To run:
* export UNSANDBOX_PUBLIC_KEY="your-public-key"
* export UNSANDBOX_SECRET_KEY="your-secret-key"
* node async_job_polling.js
*
* Expected output:
* Starting async job...
* Job ID: job_abc123
* Polling for completion...
* Poll 1: status = running
* Poll 2: status = completed
* Final result: 42
*/
import {
executeAsync,
getJob,
waitForJob,
CredentialsError,
} from '../src/un_async.js';
async function main() {
try {
// Long-running code to execute
const code = `
import time
time.sleep(0.5) # Simulate some work
print(42)
`;
console.log('Starting async job...');
// Start the job (returns immediately with job_id)
const jobId = await executeAsync('python', code);
console.log(`Job ID: ${jobId}`);
// Option 1: Manual polling
console.log('Polling for completion...');
let pollCount = 0;
let result;
while (true) {
pollCount++;
result = await getJob(jobId);
console.log(`Poll ${pollCount}: status = ${result.status}`);
if (['completed', 'failed', 'timeout', 'cancelled'].includes(result.status)) {
break;
}
// Wait before next poll
await new Promise((resolve) => setTimeout(resolve, 300));
}
console.log(`Final result: ${(result.stdout || '').trim()}`);
return result.status === 'completed' ? 0 : 1;
// Option 2: Use waitForJob (recommended - handles polling automatically)
// const result = await waitForJob(jobId);
// console.log(`Result: ${result.stdout}`);
} catch (e) {
if (e instanceof CredentialsError) {
console.log(`Credentials error: ${e.message}`);
} else {
console.log(`Error: ${e.message}`);
console.error(e);
}
return 1;
}
}
main().then(process.exit);