baseDir = getenv('GOOGLE_CLOUD_BATCH_DAEMON_FAILURE_DIR'); if ($this->baseDir === false) { $this->baseDir = sprintf('%s/batch-daemon-failure', sys_get_temp_dir()); } if (!is_dir($this->baseDir)) { if (@mkdir($this->baseDir, 0700, true) === false) { throw new \RuntimeException(sprintf('Couuld not create a directory: %s', $this->baseDir)); } } // Use getmypid for simplicity. $this->failureFile = sprintf('%s/failed-items-%d', $this->baseDir, getmypid()); } /** * Save the items to the failureFile. We silently abandon the items upon * failures in this method because there's nothing we can do. * * @param int $idNum A numeric id for the job. * @param array $items Items to save. */ public function handleFailure($idNum, array $items) { if (!$this->failureFile) { $this->initFailureFile(); } $fp = @fopen($this->failureFile, 'a'); @fwrite($fp, serialize([$idNum => $items]) . PHP_EOL); @fclose($fp); } /** * Get all the filenames for the failure files. * * @return array Filenames for all the failure files. */ private function getFailedFiles() { $pattern = sprintf('%s/failed-items-*', $this->baseDir); return glob($pattern) ?: []; } }