Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
scriptqueue.php
1<?php
2
4
7
24class ScriptQueueTable extends Main\Entity\DataManager
25{
29 public static function getTableName()
30 {
31 return 'b_bp_script_queue';
32 }
33
37 public static function getMap()
38 {
39 return [
40 'ID' => [
41 'data_type' => 'integer',
42 'primary' => true,
43 ],
44 'SCRIPT_ID' => [
45 'data_type' => 'integer'
46 ],
47 'STARTED_DATE' => [
48 'data_type' => 'datetime'
49 ],
50 'STARTED_BY' => [
51 'data_type' => 'integer'
52 ],
53 'STARTED_USER' => [
54 'data_type' => Main\UserTable::class,
55 'reference' => [
56 '=this.STARTED_BY' => 'ref.ID'
57 ],
58 'join_type' => 'LEFT',
59 ],
60 'STATUS' => [
61 'data_type' => 'integer'
62 ],
63 'MODIFIED_DATE' => [
64 'data_type' => 'datetime'
65 ],
66 'MODIFIED_BY' => [
67 'data_type' => 'integer'
68 ],
69 new Main\ORM\Fields\ArrayField('WORKFLOW_PARAMETERS'),
70 ];
71 }
72
79 public static function getDocumentCounters(int $queueId): array
80 {
81 $all = (int) ScriptQueueDocumentTable::getCount(['=QUEUE_ID' => $queueId]);
82 $queued = (int) ScriptQueueDocumentTable::getCount(['=QUEUE_ID' => $queueId, '=STATUS' => Status::QUEUED]);
83
84 return [
85 'all' => $all,
86 'queued' => $queued,
87 'completed' => $all - $queued
88 ];
89 }
90
91 public static function getNextQueuedDocument(int $queueId): ?EO_ScriptQueueDocument
92 {
93 $document = ScriptQueueDocumentTable::getList([
94 'filter' => [
95 '=QUEUE_ID' => $queueId,
96 '=STATUS' => Status::QUEUED,
97 ],
98 'order' => ['ID' => 'ASC'],
99 'limit' => 1
100 ])->fetchObject();
101
102 return $document;
103 }
104
105 public static function markTerminated(int $queueId, int $userId)
106 {
107 static::update(
108 $queueId,
109 [
110 'STATUS' => Status::TERMINATED,
111 'MODIFIED_BY' => $userId
112 ]
113 );
114
115 $docResult = ScriptQueueDocumentTable::getList([
116 'filter' => [
117 '=QUEUE_ID' => $queueId,
118 '=STATUS' => Status::QUEUED,
119 ],
120 'select' => ['ID']
121 ]);
122
123 $docIds = array_column($docResult->fetchAll(), 'ID');
124
125 ScriptQueueDocumentTable::updateMulti($docIds, ['STATUS' => Status::TERMINATED], true);
126 }
127
128 public static function markExecuting(int $queueId)
129 {
130 static::update($queueId, ['STATUS' => Status::EXECUTING]);
131 }
132
133 public static function markCompleted(int $queueId)
134 {
135 static::update($queueId, ['STATUS' => Status::COMPLETED]);
136 }
137
138 public static function deleteByScript(int $scriptId)
139 {
140 $result = static::getList(['filter' => ['=SCRIPT_ID' => $scriptId], 'select' => ['ID']]);
141
142 foreach ($result as $row)
143 {
144 static::delete($row['ID']);
146 }
147 }
148
149 public static function onBeforeUpdate(Main\ORM\Event $event): Main\ORM\EventResult
150 {
151 $result = new Main\ORM\EventResult();
152
153 $fields = $event->getParameter('fields');
154 $modifyFields = ['MODIFIED_DATE' => new Main\Type\DateTime()];
155
156 if(!isset($fields['MODIFIED_BY']))
157 {
158 $modifyFields['MODIFIED_BY'] = 0;
159 }
160
161 $result->modifyFields($modifyFields);
162
163 return $result;
164 }
165}
static onBeforeUpdate(Main\ORM\Event $event)
static markTerminated(int $queueId, int $userId)