Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / applications / repository / engine / PhabricatorRepositoryEngine.php
blobedf54d1d2b0292a004c6f29fee0cb8e667eaf8e9
1 <?php
3 /**
4 * @task config Configuring Repository Engines
5 * @task internal Internals
6 */
7 abstract class PhabricatorRepositoryEngine extends Phobject {
9 private $repository;
10 private $verbose;
12 /**
13 * @task config
15 public function setRepository(PhabricatorRepository $repository) {
16 $this->repository = $repository;
17 return $this;
21 /**
22 * @task config
24 protected function getRepository() {
25 if ($this->repository === null) {
26 throw new PhutilInvalidStateException('setRepository');
29 return $this->repository;
33 /**
34 * @task config
36 public function setVerbose($verbose) {
37 $this->verbose = $verbose;
38 return $this;
42 /**
43 * @task config
45 public function getVerbose() {
46 return $this->verbose;
50 public function getViewer() {
51 return PhabricatorUser::getOmnipotentUser();
54 protected function newRepositoryLock(
55 PhabricatorRepository $repository,
56 $lock_key,
57 $lock_device_only) {
59 $lock_parts = array(
60 'repositoryPHID' => $repository->getPHID(),
63 if ($lock_device_only) {
64 $device = AlmanacKeys::getLiveDevice();
65 if ($device) {
66 $lock_parts['devicePHID'] = $device->getPHID();
70 return PhabricatorGlobalLock::newLock($lock_key, $lock_parts);
73 /**
74 * @task internal
76 protected function log($pattern /* ... */) {
77 if ($this->getVerbose()) {
78 $console = PhutilConsole::getConsole();
79 $argv = func_get_args();
80 array_unshift($argv, "%s\n");
81 call_user_func_array(array($console, 'writeOut'), $argv);
83 return $this;
86 final protected function queueCommitImportTask(
87 PhabricatorRepository $repository,
88 $commit_phid,
89 $task_priority,
90 $via) {
92 $vcs = $repository->getVersionControlSystem();
93 switch ($vcs) {
94 case PhabricatorRepositoryType::REPOSITORY_TYPE_GIT:
95 $class = 'PhabricatorRepositoryGitCommitMessageParserWorker';
96 break;
97 case PhabricatorRepositoryType::REPOSITORY_TYPE_SVN:
98 $class = 'PhabricatorRepositorySvnCommitMessageParserWorker';
99 break;
100 case PhabricatorRepositoryType::REPOSITORY_TYPE_MERCURIAL:
101 $class = 'PhabricatorRepositoryMercurialCommitMessageParserWorker';
102 break;
103 default:
104 throw new Exception(
105 pht(
106 'Unknown repository type "%s"!',
107 $vcs));
110 $data = array(
111 'commitPHID' => $commit_phid,
114 if ($via !== null) {
115 $data['via'] = $via;
118 $options = array(
119 'priority' => $task_priority,
120 'objectPHID' => $commit_phid,
121 'containerPHID' => $repository->getPHID(),
124 PhabricatorWorker::scheduleTask($class, $data, $options);
127 final protected function getImportTaskPriority(
128 PhabricatorRepository $repository,
129 array $refs) {
130 assert_instances_of($refs, 'PhabricatorRepositoryCommitRef');
132 // If the repository is importing for the first time, we schedule tasks
133 // at IMPORT priority, which is very low. Making progress on importing a
134 // new repository for the first time is less important than any other
135 // daemon task.
137 // If the repository has finished importing and we're just catching up
138 // on recent commits, we usually schedule discovery at COMMIT priority,
139 // which is slightly below the default priority.
141 // Note that followup tasks and triggered tasks (like those generated by
142 // Herald or Harbormaster) will queue at DEFAULT priority, so that each
143 // commit tends to fully import before we start the next one. This tends
144 // to give imports fairly predictable progress. See T11677 for some
145 // discussion.
147 if ($repository->isImporting()) {
148 $this->log(
149 pht(
150 'Importing %s commit(s) at low priority ("PRIORITY_IMPORT") '.
151 'because this repository is still importing.',
152 phutil_count($refs)));
154 return PhabricatorWorker::PRIORITY_IMPORT;
157 // See T13369. If we've discovered a lot of commits at once, import them
158 // at lower priority.
160 // This is mostly aimed at reducing the impact that synchronizing thousands
161 // of commits from a remote upstream has on other repositories. The queue
162 // is "mostly FIFO", so queueing a thousand commit imports can stall other
163 // repositories.
165 // In a perfect world we'd probably give repositories round-robin queue
166 // priority, but we don't currently have the primitives for this and there
167 // isn't a strong case for building them.
169 // Use "a whole lot of commits showed up at once" as a heuristic for
170 // detecting "someone synchronized an upstream", and import them at a lower
171 // priority to more closely approximate fair scheduling.
173 if (count($refs) >= PhabricatorRepository::LOWPRI_THRESHOLD) {
174 $this->log(
175 pht(
176 'Importing %s commit(s) at low priority ("PRIORITY_IMPORT") '.
177 'because many commits were discovered at once.',
178 phutil_count($refs)));
180 return PhabricatorWorker::PRIORITY_IMPORT;
183 // Otherwise, import at normal priority.
185 if ($refs) {
186 $this->log(
187 pht(
188 'Importing %s commit(s) at normal priority ("PRIORITY_COMMIT").',
189 phutil_count($refs)));
192 return PhabricatorWorker::PRIORITY_COMMIT;