| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- <?php
- class Core_AsyncJobsFiles {
- static function basePath($idJob, $jobDate) { // @return string base path
- $confAsyncPath = Config::get('APP_PATH_ASYNC_JOB');
- if (!$confAsyncPath) throw new Exception("Missing Config APP_PATH_ASYNC_JOB");
- return "{$confAsyncPath}/{$jobDate}/{$idJob}";
- }
- static function propertyFile($idJob, $jobDate) { // @return string property file path
- return self::basePath($idJob, $jobDate) . "/job.properties";
- }
- static function startScript($idJob, $jobDate) { // @return string start script path
- return self::basePath($idJob, $jobDate) . "/start.sh";
- }
- static function outLog($idJob, $jobDate) { // @return string out log file path
- return self::basePath($idJob, $jobDate) . "/out.log";
- }
- static function errorLog($idJob, $jobDate) { // @return string error log file path
- return self::basePath($idJob, $jobDate) . "/error.log";
- }
- static function createNewJobFiles($idJob, $jobProps = []) {
- if (!$idJob) throw new Exception("Missing job ID");
- $today = date("Y-m-d");
- $jobBasePath = self::basePath($idJob, $today); // "{$confAsyncPath}/{$today}/{$idJob}";
- mkdir($jobBasePath, $mode = 0777, $recursive = TRUE);
- if (!file_exists($jobBasePath)) throw new Exception("Cannot create folder for async job");
- touch(self::outLog($idJob, $today)); // "{$jobBasePath}/out.log");
- touch(self::errorLog($idJob, $today)); // "{$jobBasePath}/error.log");
- // touch("{$jobBasePath}/start.sh");
- $baseProps = [];
- $baseProps['p5_async_job_base_path'] = $jobBasePath;
- $baseProps['p5_async_job_output'] = "{$jobBasePath}/output";
- $baseProps['p5_async_job_progress'] = "{$jobBasePath}/progress";
- $baseProps['p5_se_base_path'] = APP_PATH_ROOT;
- $allProps = array_merge($baseProps, $jobProps);
- $outProperties = implode("\n", array_map(function ($value, $key) {
- return "{$key}={$value}";
- }, $allProps, array_keys($allProps)));
- file_put_contents(self::propertyFile($idJob, $today), $outProperties);
- touch("{$jobBasePath}/progress");
- // touch("{$jobBasePath}/output");
- // - make base files:
- // - log files: `out.log`, `error.log`
- // - `output`: if process creates file. @param --out_file=out
- // - `output_type`: output mime type or namespace
- // - `progress`: if process implement progress. @param --progress_file=progress
- }
- static function createTaskStartScript($idJob, $jobDate, $jobStartScript) {
- file_put_contents(self::startScript($idJob, $jobDate), "#!/usr/bin/env bash" . "\n" . $jobStartScript);
- }
- static function isInstalled() {
- $confAsyncPath = Config::get('APP_PATH_ASYNC_JOB');
- if (!$confAsyncPath) throw new Exception("Missing Config APP_PATH_ASYNC_JOB");
- if (!file_exists($confAsyncPath)) {
- mkdir($confAsyncPath, $mode = 0777, $recursive = TRUE);
- }
- if (!file_exists($confAsyncPath)) throw new Exception("Folder not exists APP_PATH_ASYNC_JOB");
- return true;
- }
- }
|