|
29 | 29 | return print("Usage: php k_map.php CALLING_PROJECT DATA_PROJECT TOPIC SUBSCRIPTION DATASET TABLE REGION_CODE QUASI_ID_NAMES INFO_TYPES\n"); |
30 | 30 | } |
31 | 31 | list($_, $callingProjectId, $dataProjectId, $topicId, $subscriptionId, $datasetId, $tableId, $regionCode, $quasiIdNames, $infoTypes) = $argv; |
| 32 | + |
32 | 33 | // Convert comma-separated lists to arrays |
33 | 34 | $quasiIdNames = explode(',', $quasiIdNames); |
34 | 35 | $infoTypes = explode(',', $infoTypes); |
|
37 | 38 | /** |
38 | 39 | * Computes the k-map risk estimation of a column set in a Google BigQuery table. |
39 | 40 | */ |
40 | | -use Google\Cloud\Core\ExponentialBackoff; |
41 | 41 | use Google\Cloud\Dlp\V2\DlpServiceClient; |
42 | 42 | use Google\Cloud\Dlp\V2\InfoType; |
43 | 43 | use Google\Cloud\Dlp\V2\RiskAnalysisJobConfig; |
|
128 | 128 | ]); |
129 | 129 |
|
130 | 130 | // Poll Pub/Sub using exponential backoff until job finishes |
131 | | -$backoff = new ExponentialBackoff(30); |
132 | | -$backoff->execute(function () use ($subscription, $dlp, &$job) { |
133 | | - printf('Waiting for job to complete' . PHP_EOL); |
| 131 | +// Consider using an asynchronous execution model such as Cloud Functions |
| 132 | +$attempt = 1; |
| 133 | +$startTime = time(); |
| 134 | +do { |
134 | 135 | foreach ($subscription->pull() as $message) { |
135 | 136 | if (isset($message->attributes()['DlpJobName']) && |
136 | 137 | $message->attributes()['DlpJobName'] === $job->getName()) { |
|
139 | 140 | do { |
140 | 141 | $job = $dlp->getDlpJob($job->getName()); |
141 | 142 | } while ($job->getState() == JobState::RUNNING); |
142 | | - return true; |
| 143 | + break 2; // break from parent do while |
143 | 144 | } |
144 | 145 | } |
145 | | - throw new Exception('Job has not yet completed'); |
146 | | -}); |
| 146 | + printf('Waiting for job to complete' . PHP_EOL); |
| 147 | + // Exponential backoff with max delay of 60 seconds |
| 148 | + sleep(min(60, pow(2, ++$attempt))); |
| 149 | +} while (time() - $startTime < 600); // 10 minute timeout |
147 | 150 |
|
148 | 151 | // Print finding counts |
149 | 152 | printf('Job %s status: %s' . PHP_EOL, $job->getName(), JobState::name($job->getState())); |
|
183 | 186 | var_dump($error->getDetails()); |
184 | 187 | } |
185 | 188 | break; |
| 189 | + case JobState::PENDING: |
| 190 | + printf('Job has not completed. Consider a longer timeout or an asynchronous execution model' . PHP_EOL); |
| 191 | + break; |
186 | 192 | default: |
187 | 193 | print('Unexpected job state. Most likely, the job is either running or has not yet started.'); |
188 | 194 | } |
|
0 commit comments