4 * @task config Configuring Repository Engines
5 * @task internal Internals
7 abstract class PhabricatorRepositoryEngine
extends Phobject
{
15 public function setRepository(PhabricatorRepository
$repository) {
16 $this->repository
= $repository;
24 protected function getRepository() {
25 if ($this->repository
=== null) {
26 throw new PhutilInvalidStateException('setRepository');
29 return $this->repository
;
36 public function setVerbose($verbose) {
37 $this->verbose
= $verbose;
45 public function getVerbose() {
46 return $this->verbose
;
50 public function getViewer() {
51 return PhabricatorUser
::getOmnipotentUser();
54 protected function newRepositoryLock(
55 PhabricatorRepository
$repository,
60 'repositoryPHID' => $repository->getPHID(),
63 if ($lock_device_only) {
64 $device = AlmanacKeys
::getLiveDevice();
66 $lock_parts['devicePHID'] = $device->getPHID();
70 return PhabricatorGlobalLock
::newLock($lock_key, $lock_parts);
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);
86 final protected function queueCommitImportTask(
87 PhabricatorRepository
$repository,
92 $vcs = $repository->getVersionControlSystem();
94 case PhabricatorRepositoryType
::REPOSITORY_TYPE_GIT
:
95 $class = 'PhabricatorRepositoryGitCommitMessageParserWorker';
97 case PhabricatorRepositoryType
::REPOSITORY_TYPE_SVN
:
98 $class = 'PhabricatorRepositorySvnCommitMessageParserWorker';
100 case PhabricatorRepositoryType
::REPOSITORY_TYPE_MERCURIAL
:
101 $class = 'PhabricatorRepositoryMercurialCommitMessageParserWorker';
106 'Unknown repository type "%s"!',
111 'commitPHID' => $commit_phid,
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,
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
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
147 if ($repository->isImporting()) {
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
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
) {
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.
188 'Importing %s commit(s) at normal priority ("PRIORITY_COMMIT").',
189 phutil_count($refs)));
192 return PhabricatorWorker
::PRIORITY_COMMIT
;