| 1 | <?php |
||
| 2 | |||
| 3 | namespace LeKoala\SimpleJobs; |
||
| 4 | |||
| 5 | use SilverStripe\ORM\DataObject; |
||
| 6 | use SilverStripe\Security\Permission; |
||
| 7 | |||
| 8 | /** |
||
| 9 | * Store the result of a cron task |
||
| 10 | * |
||
| 11 | * @property ?string $TaskClass |
||
| 12 | * @property ?string $Result |
||
| 13 | * @property bool|int $Failed |
||
| 14 | * @property bool|int $ForcedRun |
||
| 15 | * @property ?string $StartDate |
||
| 16 | * @property ?string $EndDate |
||
| 17 | * @property int $TimeToExecute |
||
| 18 | * @mixin \SilverStripe\Assets\Shortcodes\FileLinkTracking |
||
| 19 | * @mixin \SilverStripe\Assets\AssetControlExtension |
||
| 20 | * @mixin \SilverStripe\CMS\Model\SiteTreeLinkTracking |
||
| 21 | * @mixin \SilverStripe\Versioned\RecursivePublishable |
||
| 22 | * @mixin \SilverStripe\Versioned\VersionedStateExtension |
||
| 23 | */ |
||
| 24 | class CronTaskResult extends DataObject |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | private static $singular_name = 'Job Result'; |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 30 | |||
| 31 | /** |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | private static $plural_name = 'Job Results'; |
||
|
0 ignored issues
–
show
|
|||
| 35 | |||
| 36 | /** |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | private static $table_name = 'CronTaskResult'; |
||
|
0 ignored issues
–
show
|
|||
| 40 | |||
| 41 | /** |
||
| 42 | * @var array<string, string> |
||
| 43 | */ |
||
| 44 | private static $db = [ |
||
|
0 ignored issues
–
show
|
|||
| 45 | 'TaskClass' => 'Varchar(255)', |
||
| 46 | 'Result' => 'Text', |
||
| 47 | 'Failed' => 'Boolean', |
||
| 48 | 'ForcedRun' => 'Boolean', |
||
| 49 | 'StartDate' => 'Datetime', |
||
| 50 | 'EndDate' => 'Datetime', |
||
| 51 | 'TimeToExecute' => 'Int', |
||
| 52 | ]; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var string |
||
| 56 | */ |
||
| 57 | private static $default_sort = 'Created DESC'; |
||
|
0 ignored issues
–
show
|
|||
| 58 | |||
| 59 | /** |
||
| 60 | * @var array<string, string> |
||
| 61 | */ |
||
| 62 | private static $summary_fields = [ |
||
|
0 ignored issues
–
show
|
|||
| 63 | 'Created' => 'Created', |
||
| 64 | 'TaskClass' => 'Task Class', |
||
| 65 | 'Failed' => 'Failed', |
||
| 66 | 'TimeToExecute' => 'Time To Execute' |
||
| 67 | ]; |
||
| 68 | |||
| 69 | public function canView($member = null, $context = []) |
||
| 70 | { |
||
| 71 | return Permission::check('CMS_ACCESS_SimpleJobsAdmin', 'any', $member); |
||
| 72 | } |
||
| 73 | |||
| 74 | public function canEdit($member = null, $context = []) |
||
| 75 | { |
||
| 76 | return Permission::check('CMS_ACCESS_SimpleJobsAdmin', 'any', $member); |
||
| 77 | } |
||
| 78 | |||
| 79 | public function canCreate($member = null, $context = []) |
||
| 80 | { |
||
| 81 | return Permission::check('CMS_ACCESS_SimpleJobsAdmin', 'any', $member); |
||
| 82 | } |
||
| 83 | |||
| 84 | public function canDelete($member = null) |
||
| 85 | { |
||
| 86 | return Permission::check('CMS_ACCESS_SimpleJobsAdmin', 'any', $member); |
||
| 87 | } |
||
| 88 | |||
| 89 | public function Status(): string |
||
| 90 | { |
||
| 91 | $status = "Task {$this->TaskClass}"; |
||
| 92 | if ($this->Failed) { |
||
| 93 | $status .= " failed to run"; |
||
| 94 | } else { |
||
| 95 | $status .= " ran successfully"; |
||
| 96 | } |
||
| 97 | $status .= " at " . $this->Created; |
||
| 98 | if ($this->ForcedRun) { |
||
| 99 | $status .= ' (forced run)'; |
||
| 100 | } |
||
| 101 | return $status; |
||
| 102 | } |
||
| 103 | |||
| 104 | public function PrettyResult(): string |
||
| 105 | { |
||
| 106 | return self::PrettifyResult($this->Result); |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @param string|object|array<mixed>|bool|null $result |
||
| 111 | * @return string |
||
| 112 | */ |
||
| 113 | public static function PrettifyResult($result): string |
||
| 114 | { |
||
| 115 | if ($result === false) { |
||
| 116 | $result = 'Task failed'; |
||
| 117 | } |
||
| 118 | if (is_object($result)) { |
||
| 119 | $result = print_r($result, true); |
||
| 120 | } elseif (is_array($result)) { |
||
| 121 | $result = json_encode($result); |
||
| 122 | } elseif ($result === null) { |
||
|
0 ignored issues
–
show
|
|||
| 123 | $result = 'NULL'; |
||
| 124 | } |
||
| 125 | return '<pre> ' . $result . '</pre>'; |
||
| 126 | } |
||
| 127 | } |
||
| 128 |