SchedulingController.php
129 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Wpae\App\UnsecuredController; |
| 4 | |
| 5 | |
| 6 | use Wpae\Controller\BaseController; |
| 7 | use Wpae\Http\Request; |
| 8 | use Wpae\Scheduling\Export; |
| 9 | use Wpae\Http\JsonResponse; |
| 10 | |
| 11 | class SchedulingController extends BaseController |
| 12 | { |
| 13 | /** Scheduling API Version */ |
| 14 | const VERSION = 1; |
| 15 | |
| 16 | /** @var Export */ |
| 17 | private $scheduledExportService; |
| 18 | |
| 19 | public function __construct($container) |
| 20 | { |
| 21 | parent::__construct($container); |
| 22 | $this->scheduledExportService = new Export(); |
| 23 | } |
| 24 | |
| 25 | public function triggerAction(Request $request) |
| 26 | { |
| 27 | if (!$this->isRequestValid()) { |
| 28 | return new JsonResponse(array('message' => 'Export hash is invalid'), 401); |
| 29 | } |
| 30 | |
| 31 | $exportId = intval($request->get('export_id')); |
| 32 | |
| 33 | $export = new \PMXE_Export_Record(); |
| 34 | $export->getById($exportId); |
| 35 | |
| 36 | if ($export->isEmpty()) { |
| 37 | return new JsonResponse(array('message' => 'Export not found'), 404); |
| 38 | } |
| 39 | |
| 40 | if ((int)$export->executing) { |
| 41 | return new JsonResponse(array("message" => "Export #" . $export->id . " is currently in manually process. Request skipped."), 409); |
| 42 | } |
| 43 | if ($export->processing and !$export->triggered) { |
| 44 | return new JsonResponse(array("message" => "Export #" . $export->id . " currently in process. Request skipped."), 409); |
| 45 | |
| 46 | } |
| 47 | if (!$export->processing and $export->triggered) { |
| 48 | return new JsonResponse(array("message" => "Export #" . $export->id . " already triggered. Request skipped."), 409); |
| 49 | } |
| 50 | |
| 51 | if (!$export->processing and !$export->triggered) { |
| 52 | $this->scheduledExportService->trigger($export); |
| 53 | |
| 54 | return new JsonResponse(array('message' => "#" . $export->id . " Cron job triggered.")); |
| 55 | } |
| 56 | |
| 57 | return new JsonResponse(array("message" => "Can't process"), 500); |
| 58 | } |
| 59 | |
| 60 | public function processAction(Request $request) |
| 61 | { |
| 62 | if (!$this->isRequestValid()) { |
| 63 | return new JsonResponse(array('message' => 'Export hash is invalid'), 401); |
| 64 | } |
| 65 | |
| 66 | $exportId = intval($request->get('export_id')); |
| 67 | |
| 68 | $export = new \PMXE_Export_Record(); |
| 69 | $export->getById($exportId); |
| 70 | |
| 71 | if ($export->isEmpty()) { |
| 72 | return new JsonResponse(array('message' => 'Export not found'), 404); |
| 73 | } |
| 74 | |
| 75 | $logger = function($m) { |
| 76 | echo "<p>$m</p>\\n"; |
| 77 | }; |
| 78 | |
| 79 | if ($export->processing == 1 and (time() - strtotime($export->registered_on)) > 120) { |
| 80 | // it means processor crashed, so it will reset processing to false, and terminate. Then next run it will work normally. |
| 81 | $export->set(array( |
| 82 | 'processing' => 0 |
| 83 | ))->update(); |
| 84 | } |
| 85 | |
| 86 | // start execution imports that is in the cron process |
| 87 | if (!(int)$export->triggered) { |
| 88 | if (!empty($export->parent_id) or empty($queue_exports)) { |
| 89 | return new JsonResponse(array("message" => 'Export #' . $exportId . ' is not triggered. Request skipped.'), 400); |
| 90 | } |
| 91 | } elseif ((int)$export->executing) { |
| 92 | return new JsonResponse(array('message' => 'Export #' . $exportId . ' is currently in manually process. Request skipped.'), 409); |
| 93 | } elseif ((int)$export->triggered and !(int)$export->processing) { |
| 94 | |
| 95 | $export->set(array('canceled' => 0))->execute($logger, true); |
| 96 | |
| 97 | if (!(int)$export->triggered and !(int)$export->processing) { |
| 98 | $this->scheduledExportService->process($export); |
| 99 | return new JsonResponse(array('Export #' . $exportId . ' complete'), 201); |
| 100 | } else { |
| 101 | return new JsonResponse(array('message' => 'Records Processed ' . (int)$export->exported . '.')); |
| 102 | } |
| 103 | |
| 104 | } else { |
| 105 | return new JsonResponse(array('message' => 'Export #' . $exportId . ' already processing. Request skipped.'), 409); |
| 106 | } |
| 107 | |
| 108 | return new JsonResponse(array("message" => "Can't process"), 500); |
| 109 | } |
| 110 | |
| 111 | public function versionAction() |
| 112 | { |
| 113 | return new JsonResponse(array('version' => self::VERSION)); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * @return bool |
| 118 | */ |
| 119 | private function isRequestValid() |
| 120 | { |
| 121 | $cron_job_key = \PMXE_Plugin::getInstance()->getOption('cron_job_key'); |
| 122 | return |
| 123 | !empty($cron_job_key) and |
| 124 | !empty($_GET['export_id']) and |
| 125 | !empty($_GET['export_key']) and |
| 126 | $_GET['export_key'] == $cron_job_key; |
| 127 | } |
| 128 | |
| 129 | } |