AsyncJobsFiles.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. class Core_AsyncJobsFiles {
  3. static function basePath($idJob, $jobDate) { // @return string base path
  4. $confAsyncPath = Config::get('APP_PATH_ASYNC_JOB');
  5. if (!$confAsyncPath) throw new Exception("Missing Config APP_PATH_ASYNC_JOB");
  6. return "{$confAsyncPath}/{$jobDate}/{$idJob}";
  7. }
  8. static function propertyFile($idJob, $jobDate) { // @return string property file path
  9. return self::basePath($idJob, $jobDate) . "/job.properties";
  10. }
  11. static function startScript($idJob, $jobDate) { // @return string start script path
  12. return self::basePath($idJob, $jobDate) . "/start.sh";
  13. }
  14. static function outLog($idJob, $jobDate) { // @return string out log file path
  15. return self::basePath($idJob, $jobDate) . "/out.log";
  16. }
  17. static function errorLog($idJob, $jobDate) { // @return string error log file path
  18. return self::basePath($idJob, $jobDate) . "/error.log";
  19. }
  20. static function createNewJobFiles($idJob, $jobProps = []) {
  21. if (!$idJob) throw new Exception("Missing job ID");
  22. $today = date("Y-m-d");
  23. $jobBasePath = self::basePath($idJob, $today); // "{$confAsyncPath}/{$today}/{$idJob}";
  24. mkdir($jobBasePath, $mode = 0777, $recursive = TRUE);
  25. if (!file_exists($jobBasePath)) throw new Exception("Cannot create folder for async job");
  26. touch(self::outLog($idJob, $today)); // "{$jobBasePath}/out.log");
  27. touch(self::errorLog($idJob, $today)); // "{$jobBasePath}/error.log");
  28. // touch("{$jobBasePath}/start.sh");
  29. $baseProps = [];
  30. $baseProps['p5_async_job_base_path'] = $jobBasePath;
  31. $baseProps['p5_async_job_output'] = "{$jobBasePath}/output";
  32. $baseProps['p5_async_job_progress'] = "{$jobBasePath}/progress";
  33. $baseProps['p5_se_base_path'] = APP_PATH_ROOT;
  34. $allProps = array_merge($baseProps, $jobProps);
  35. $outProperties = implode("\n", array_map(function ($value, $key) {
  36. return "{$key}={$value}";
  37. }, $allProps, array_keys($allProps)));
  38. file_put_contents(self::propertyFile($idJob, $today), $outProperties);
  39. touch("{$jobBasePath}/progress");
  40. // touch("{$jobBasePath}/output");
  41. // - make base files:
  42. // - log files: `out.log`, `error.log`
  43. // - `output`: if process creates file. @param --out_file=out
  44. // - `output_type`: output mime type or namespace
  45. // - `progress`: if process implement progress. @param --progress_file=progress
  46. }
  47. static function createTaskStartScript($idJob, $jobDate, $jobStartScript) {
  48. file_put_contents(self::startScript($idJob, $jobDate), "#!/usr/bin/env bash" . "\n" . $jobStartScript);
  49. }
  50. static function isInstalled() {
  51. $confAsyncPath = Config::get('APP_PATH_ASYNC_JOB');
  52. if (!$confAsyncPath) throw new Exception("Missing Config APP_PATH_ASYNC_JOB");
  53. if (!file_exists($confAsyncPath)) {
  54. mkdir($confAsyncPath, $mode = 0777, $recursive = TRUE);
  55. }
  56. if (!file_exists($confAsyncPath)) throw new Exception("Folder not exists APP_PATH_ASYNC_JOB");
  57. return true;
  58. }
  59. }