Job Monitoring

The SDK provides utilities for monitoring asynchronous job operations with configurable timeouts and intervals:

import { SpaceportSDK } from '@spaceport/sdk'

async function monitorJobs() {
  const sdk = SpaceportSDK.createWithAccount(account, {
    chain: 'devnet',
    apiKey: process.env.SDK_API_KEY,
  })

  // Monitor a job with custom timeout and polling interval
  const jobStatus = await sdk.utils.pollJobStatus(jobId, {
    timeout: 300000, // 5 minutes timeout
    interval: 3000, // Poll every 3 seconds
  })

  // Handle different job statuses
  if (jobStatus.status === 'completed') {
    console.log('Job completed successfully!')

    // Handle different data structures returned by jobs
    let results: any[] = []
    if (Array.isArray(jobStatus.data)) {
      results = jobStatus.data
    } else if (jobStatus.data?.value && Array.isArray(jobStatus.data.value)) {
      results = jobStatus.data.value
    }

    console.log(`Processed ${results.length} items`)
  } else if (jobStatus.status === 'failed') {
    console.error(`Job failed: ${jobStatus.error || 'Unknown error'}`)
  }

  // Monitor multiple jobs in parallel
  const jobIds = ['job1', 'job2', 'job3']
  const jobStatuses = await Promise.all(jobIds.map((id) => sdk.utils.pollJobStatus(id)))
}

Utility Methods

  • utils.pollJobStatus(jobId, options?): Monitors job status with polling
    • jobId: The job identifier to monitor
    • options.timeout: Maximum time to wait (default: 60 seconds)
    • options.interval: Polling interval (default: 1 second)

Job Status Handling

Jobs can return different data structures depending on the operation:

  • Array format: Direct array of results
  • Wrapped format: { value: [...] } containing the results array
  • Error format: { error: "error message" } for failed jobs

The polling utility automatically handles timeouts and provides detailed status information for debugging and monitoring purposes.