@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.)
hq.recaptime.dev/wiki/Phorge
phorge
phabricator
1<?php
2
3final class HarbormasterLogWorker extends HarbormasterWorker {
4
5 protected function doWork() {
6 $viewer = $this->getViewer();
7
8 $data = $this->getTaskData();
9 $log_phid = idx($data, 'logPHID');
10
11 $log = id(new HarbormasterBuildLogQuery())
12 ->setViewer($viewer)
13 ->withPHIDs(array($log_phid))
14 ->executeOne();
15 if (!$log) {
16 throw new PhabricatorWorkerPermanentFailureException(
17 pht(
18 'Invalid build log PHID "%s".',
19 $log_phid));
20 }
21
22 $lock = $log->getLock();
23
24 try {
25 $lock->lock();
26 } catch (PhutilLockException $ex) {
27 throw new PhabricatorWorkerYieldException(15);
28 }
29
30 $caught = null;
31 try {
32 $log->reload();
33
34 if ($log->getLive()) {
35 throw new PhabricatorWorkerPermanentFailureException(
36 pht(
37 'Log "%s" is still live. Logs can not be finalized until '.
38 'they have closed.',
39 $log_phid));
40 }
41
42 $this->finalizeBuildLog($log);
43 } catch (Exception $ex) {
44 $caught = $ex;
45 }
46
47 $lock->unlock();
48
49 if ($caught) {
50 throw $caught;
51 }
52 }
53
54 private function finalizeBuildLog(HarbormasterBuildLog $log) {
55 $viewer = $this->getViewer();
56
57 $data = $this->getTaskData();
58 $is_force = idx($data, 'force');
59
60 if (!$log->getByteLength() || !$log->getLineMap() || $is_force) {
61 $iterator = $log->newDataIterator();
62
63 $log
64 ->setByteLength(0)
65 ->setLineMap(array());
66
67 foreach ($iterator as $block) {
68 $log->updateLineMap($block);
69 }
70
71 $log->save();
72 }
73
74 $format_text = HarbormasterBuildLogChunk::CHUNK_ENCODING_TEXT;
75 if (($log->getChunkFormat() === $format_text) || $is_force) {
76 if ($log->canCompressLog()) {
77 $log->compressLog();
78 }
79 }
80
81 if ($is_force) {
82 $log->destroyFile();
83 }
84
85 if (!$log->getFilePHID()) {
86 $iterator = $log->newDataIterator();
87
88 $source = id(new PhabricatorIteratorFileUploadSource())
89 ->setName('harbormaster-log-'.$log->getID().'.log')
90 ->setViewPolicy(PhabricatorPolicies::POLICY_NOONE)
91 ->setMIMEType('application/octet-stream')
92 ->setIterator($iterator);
93
94 $file = $source->uploadFile();
95
96 $file->attachToObject($log->getPHID());
97
98 $log
99 ->setFilePHID($file->getPHID())
100 ->save();
101 }
102
103 }
104
105}