Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 31 additions & 21 deletions healthcare/fhir/importFhirResources.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const main = (
auth: new google.auth.GoogleAuth({
scopes: ['https://www.googleapis.com/auth/cloud-platform'],
}),
responseType: 'json',
});
const sleep = ms => {
return new Promise(resolve => setTimeout(resolve, ms));
Expand All @@ -50,33 +51,42 @@ const main = (
},
},
};
try {
const operation =
await healthcare.projects.locations.datasets.fhirStores.import(request);
const operationName = operation.data.name;

const operation =
await healthcare.projects.locations.datasets.fhirStores.import(request);
const operationName = operation.data.name;
console.log(`Import operation started: ${operationName}`);

const operationRequest = {name: operationName};
// --- POLLING STRATEGY ---
let done = false;
let operationStatus;

// Wait twenty seconds for the LRO to finish.
await sleep(20000);
while (!done) {
console.log('Waiting for import operation to complete...');
await sleep(5000); // Wait 5 seconds between polls

// Check the LRO's status
const operationStatus =
await healthcare.projects.locations.datasets.operations.get(
operationRequest
);
operationStatus =
await healthcare.projects.locations.datasets.operations.get({
name: operationName,
});

const success = operationStatus.data.metadata.counter.success;
done = operationStatus.data.done;
}

if (typeof success !== 'undefined') {
console.log(
`Import FHIR resources succeeded. ${success} resources imported.`
);
} else {
console.log(
'Imported FHIR resources failed. Details available in Cloud Logging at the following URL:\n',
operationStatus.data.metadata.logsUrl
);
if (operationStatus.data.error) {
console.error(
'Import FHIR resources failed:',
operationStatus.data.error
);
} else {
const successCount = operationStatus.data.metadata.counter.success || 0;
console.log(
`Import FHIR resources succeeded. ${successCount} resources imported.`
);
}
Comment on lines +62 to +87
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The polling loop for the long-running operation could potentially run indefinitely if the operation never reaches a 'done' state. To improve robustness, it's good practice to add a timeout mechanism, such as a maximum number of polling attempts.

      // --- POLLING STRATEGY ---
      let done = false;
      let operationStatus;
      const maxAttempts = 24; // Poll for 2 minutes (24 * 5s)
      let attempt = 0;

      while (!done && attempt < maxAttempts) {
        console.log('Waiting for import operation to complete...');
        await sleep(5000); // Wait 5 seconds between polls
        attempt++;

        operationStatus =
          await healthcare.projects.locations.datasets.operations.get({
            name: operationName,
          });

        done = operationStatus.data.done;
      }

      if (!done) {
        console.error('The import operation timed out.');
        return;
      }

      if (operationStatus.data.error) {
        console.error(
          'Import FHIR resources failed:',
          operationStatus.data.error
        );
      } else {
        const successCount = operationStatus.data.metadata.counter.success || 0;
        console.log(
          `Import FHIR resources succeeded. ${successCount} resources imported.`
        );
      }

} catch (error) {
console.error('Error initiating import:', error.message || error);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The current error message "Error initiating import:" is misleading. If an error occurs while polling for the operation's status, this message would be incorrect. A more generic error message would be more appropriate to cover all potential failure points within the try block.

      console.error('An error occurred during the import process:', error.message || error);

}
};

Expand Down
Loading