1818use Queue \Model \Entity \QueuedJob ;
1919use Queue \Queue \Config ;
2020use Queue \Queue \TaskFinder ;
21+ use Queue \Utility \Memory ;
2122use RuntimeException ;
2223use Search \Manager ;
2324
3031 * @method \Queue\Model\Entity\QueuedJob|false save(\Cake\Datasource\EntityInterface $entity, array $options = [])
3132 * @method \Queue\Model\Entity\QueuedJob patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = [])
3233 * @method array<\Queue\Model\Entity\QueuedJob> patchEntities(iterable $entities, array $data, array $options = [])
33- * @method \Queue\Model\Entity\QueuedJob findOrCreate($search, ?callable $callback = null, array $options = [])
34+ * @method \Queue\Model\Entity\QueuedJob findOrCreate(\Cake\ORM\Query\SelectQuery|callable|array $search, ?callable $callback = null, array $options = [])
3435 * @mixin \Cake\ORM\Behavior\TimestampBehavior
3536 * @method \Queue\Model\Entity\QueuedJob saveOrFail(\Cake\Datasource\EntityInterface $entity, array $options = [])
3637 * @mixin \Search\Model\Behavior\SearchBehavior
4041 * @method \Cake\Datasource\ResultSetInterface<\Queue\Model\Entity\QueuedJob> saveManyOrFail(iterable $entities, array $options = [])
4142 * @method \Cake\Datasource\ResultSetInterface<\Queue\Model\Entity\QueuedJob>|false deleteMany(iterable $entities, array $options = [])
4243 * @method \Cake\Datasource\ResultSetInterface<\Queue\Model\Entity\QueuedJob> deleteManyOrFail(iterable $entities, array $options = [])
44+ * @extends \Cake\ORM\Table<array{Search: \Search\Model\Behavior\SearchBehavior, Timestamp: \Cake\ORM\Behavior\TimestampBehavior}>
4345 */
4446class QueuedJobsTable extends Table {
4547
@@ -720,6 +722,7 @@ public function updateProgress(int $id, float $progress, ?string $status = null)
720722
721723 $ values = [
722724 'progress ' => round ($ progress , 2 ),
725+ 'memory ' => Memory::usage (),
723726 ];
724727 if ($ status !== null ) {
725728 $ values ['status ' ] = $ status ;
@@ -739,6 +742,7 @@ public function markJobDone(QueuedJob $job): bool {
739742 $ fields = [
740743 'progress ' => 1 ,
741744 'completed ' => $ this ->getDateTime (),
745+ 'memory ' => Memory::usage (),
742746 ];
743747 $ job = $ this ->patchEntity ($ job , $ fields );
744748
@@ -756,6 +760,7 @@ public function markJobDone(QueuedJob $job): bool {
756760 public function markJobFailed (QueuedJob $ job , ?string $ failureMessage = null ): bool {
757761 $ fields = [
758762 'failure_message ' => $ failureMessage ,
763+ 'memory ' => Memory::usage (),
759764 ];
760765 $ job = $ this ->patchEntity ($ job , $ fields );
761766
@@ -796,6 +801,7 @@ public function reset(?int $id = null, bool $full = false): int {
796801 'attempts ' => 0 ,
797802 'workerkey ' => null ,
798803 'failure_message ' => null ,
804+ 'memory ' => null ,
799805 ];
800806 $ conditions = [
801807 'completed IS ' => null ,
@@ -828,6 +834,7 @@ public function rerunByTask(string $task, ?string $reference = null): int {
828834 'attempts ' => 0 ,
829835 'workerkey ' => null ,
830836 'failure_message ' => null ,
837+ 'memory ' => null ,
831838 ];
832839 $ conditions = [
833840 'completed IS NOT ' => null ,
@@ -853,6 +860,7 @@ public function rerun(int $id): int {
853860 'attempts ' => 0 ,
854861 'workerkey ' => null ,
855862 'failure_message ' => null ,
863+ 'memory ' => null ,
856864 ];
857865 $ conditions = [
858866 'completed IS NOT ' => null ,
@@ -862,6 +870,28 @@ public function rerun(int $id): int {
862870 return $ this ->updateAll ($ fields , $ conditions );
863871 }
864872
873+ /**
874+ * @param \Queue\Model\Entity\QueuedJob $queuedJob
875+ * @return \Queue\Model\Entity\QueuedJob|null
876+ */
877+ public function clone (QueuedJob $ queuedJob ): ?QueuedJob
878+ {
879+ $ defaults = [
880+ 'completed ' => null ,
881+ 'fetched ' => null ,
882+ 'progress ' => null ,
883+ 'attempts ' => 0 ,
884+ 'workerkey ' => null ,
885+ 'failure_message ' => null ,
886+ 'memory ' => null ,
887+ ];
888+ $ data = $ defaults + $ queuedJob ->toArray ();
889+ $ data ['created ' ] = new DateTime ();
890+ $ queuedJob = $ this ->newEntity ($ data );
891+
892+ return $ this ->save ($ queuedJob ) ?: null ;
893+ }
894+
865895 /**
866896 * Return some statistics about unfinished jobs still in the Database.
867897 *
@@ -881,6 +911,7 @@ public function getPendingStats(): SelectQuery {
881911 'notbefore ' ,
882912 'attempts ' ,
883913 'failure_message ' ,
914+ 'memory ' ,
884915 ],
885916 'conditions ' => [
886917 'completed IS ' => null ,
@@ -911,6 +942,7 @@ public function getScheduledStats(): SelectQuery {
911942 'notbefore ' ,
912943 'attempts ' ,
913944 'failure_message ' ,
945+ 'memory ' ,
914946 ],
915947 'conditions ' => [
916948 'completed IS ' => null ,
0 commit comments