PollScheduler: add generic _main_loop()
authorZac Medico <zmedico@gentoo.org>
Sat, 11 Feb 2012 02:10:07 +0000 (18:10 -0800)
committerZac Medico <zmedico@gentoo.org>
Sat, 11 Feb 2012 02:10:07 +0000 (18:10 -0800)
pym/_emerge/MetadataRegen.py
pym/_emerge/PollScheduler.py
pym/_emerge/QueueScheduler.py
pym/_emerge/Scheduler.py

index 1ebc53268399215777f7b8615a126d7408d6f42e..3404001194e84a2debd3f1ed1398ad4b6574a12c 100644 (file)
@@ -82,18 +82,16 @@ class MetadataRegen(PollScheduler):
                                        portdb=portdb, repo_path=repo_path,
                                        settings=portdb.doebuild_settings)
 
+       def _keep_scheduling(self):
+               return self._remaining_tasks and not self._terminated_tasks
+
        def run(self):
 
                portdb = self._portdb
                from portage.cache.cache_errors import CacheError
                dead_nodes = {}
 
-               self._schedule()
-               while self._remaining_tasks and not self._terminated_tasks:
-                       self.sched_iface.iteration()
-
-               while self._jobs:
-                       self.sched_iface.iteration()
+               self._main_loop()
 
                if self._terminated_tasks:
                        self.returncode = 1
index eafb8a23780e5ccba9a08244e8ee318dc6223613..3b3ef67df1038c9b334795445180306659b1f010 100644 (file)
@@ -109,6 +109,28 @@ class PollScheduler(object):
                finally:
                        self._scheduling = False
 
+       def _main_loop(self):
+               # Populate initial event sources. We only need to do
+               # this once here, since it can be called during the
+               # loop from within event handlers.
+               self._schedule()
+
+               # Loop while there are jobs to be scheduled.
+               while self._keep_scheduling():
+                       self.sched_iface.iteration()
+
+               # Clean shutdown of previously scheduled jobs. In the
+               # case of termination, this allows for basic cleanup
+               # such as flushing of buffered output to logs.
+               while self._is_work_scheduled():
+                       self.sched_iface.iteration()
+
+       def _keep_scheduling(self):
+               return False
+
+       def _is_work_scheduled(self):
+               return bool(self._running_job_count())
+
        def _running_job_count(self):
                return self._jobs
 
index e34758a85a4ff1cc943c583671e11fbdc2d1f788..ba740c11fedd49a3319c9fdce0e1cb45f0dc6bcc 100644 (file)
@@ -44,15 +44,7 @@ class QueueScheduler(PollScheduler):
                                timeout, timeout_callback)
 
                try:
-
-                       while not (timeout_callback is not None and
-                               timeout_callback.timed_out) and self._schedule():
-                               self.sched_iface.iteration()
-
-                       while not (timeout_callback is not None and
-                               timeout_callback.timed_out) and self._running_job_count():
-                               self.sched_iface.iteration()
-
+                       self._main_loop()
                except StopIteration:
                        pass
                finally:
index 100d89d1cf0bddcb7195152e41a705ca7d3f3c36..66d76b1e07f9887de042f4bff3f24842eff52aa6 100644 (file)
@@ -1347,6 +1347,9 @@ class Scheduler(PollScheduler):
 
        def _merge(self):
 
+               if self._opts_no_background.intersection(self.myopts):
+                       self._set_max_jobs(1)
+
                self._add_prefetchers()
                self._add_packages()
                failed_pkgs = self._failed_pkgs
@@ -1493,18 +1496,6 @@ class Scheduler(PollScheduler):
        def _deallocate_config(self, settings):
                self._config_pool[settings['EROOT']].append(settings)
 
-       def _main_loop(self):
-
-               if self._opts_no_background.intersection(self.myopts):
-                       self._set_max_jobs(1)
-
-               self._schedule()
-               while self._keep_scheduling():
-                       self.sched_iface.iteration()
-
-               while self._is_work_scheduled():
-                       self.sched_iface.iteration()
-
        def _keep_scheduling(self):
                return bool(not self._terminated_tasks and self._pkg_queue and \
                        not (self._failed_pkgs and not self._build_opts.fetchonly))