Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 18 Aug 2007 02:25:46 GMT
From:      Ivan Voras <ivoras@FreeBSD.org>
To:        Perforce Change Reviews <perforce@FreeBSD.org>
Subject:   PERFORCE change 125283 for review
Message-ID:  <200708180225.l7I2PkU5044876@repoman.freebsd.org>

next in thread | raw e-mail | index | archive | help
http://perforce.freebsd.org/chv.cgi?CH=125283

Change 125283 by ivoras@ivoras_finstall on 2007/08/18 02:25:15

	Framework for asynchronous backend jobs

Affected files ...

.. //depot/projects/soc2007/ivoras_finstall/pybackend/systoolengine.py#8 edit

Differences ...

==== //depot/projects/soc2007/ivoras_finstall/pybackend/systoolengine.py#8 (text+ko) ====

@@ -24,6 +24,7 @@
 import os, sys
 import re
 import logging
+from threading import Thread, Lock
 
 import globals
 import freebsd
@@ -51,12 +52,39 @@
 		return [e]
 
 
+class SysToolJob(Thread):
+	"""A generic asynchronous SysTool job"""
+
+	def __init__(self):
+		Thread.__init__(self)
+		self.finished = False
+		self.percent_complete = 0
+		self.error = None
+		self.result = None
+
+	def _calc_percent(self, i, total):
+		return int((float(i+1) / total) * 100)
+
+
+class PartitionJob(SysToolJob):
+	"""A partitioning SysTool job. This one accept a list of partitions
+	to create and creates them one by one."""
+	def __init__(self, part_spec):
+		SysToolJob.__init__(self)
+		self.part_spec = part_spec
+
+	def run(self):
+		for i, part in enumerate(self.part_spec):
+			self.percent_complete = self._calc_percent(i, len(self.part_spec))
+
+
 class SysToolEngine:
 
-
 	def __init__(self):
 		self.root_dest = ""	# Config file / "new" root, sans final slash
 		self.root_live = ""	# Live file system root (for binaries!)
+		self.job_list = []
+		self.job_list_lock = Lock()
 
 
 	def GetId(self):
@@ -250,3 +278,41 @@
 		physmem = int(freebsd.get_sysctl("hw.realmem"))
 		return int(physmem / (1024*1024))
 
+
+	@logexception
+	def StartPartitionJob(self, part_spec):
+		"""Starts a job that executes a partition spec list. Returns
+		an integer job_id"""
+		self.job_list_lock.acquire()
+		job = PartitionJob(part_spec)
+		self.job_list.append(job)
+		job_id = len(self.job_list) # 1-based job IDs
+		job.start() # fires up a new thread
+		self.job_list_lock.release()
+		return job_id
+
+
+	@logexception
+	def QueryJobProgress(self, job_id):
+		"""Queries the progress of a job, returns percent complete or None
+		if the job is in error"""
+		self.job_list_lock.acquire()
+		job = self.job_list[job_id-1]
+		self.job_list_lock.release()
+		if job.error != None:
+			return None
+		return job.percent_complete
+
+
+	@logexception
+	def QueryJobResult(self, job_id):
+		"""Queries the result of a job, if the job is finished. Returns
+		a string with the job's status report or None if the job is not
+		yet finished."""
+		self.job_list_lock.acquire()
+		job = self.job_list[job_id-1]
+		self.job_list_lock.release()
+		if not job.finished:
+			return None
+		return job.result
+



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200708180225.l7I2PkU5044876>