Example #1
0
    def test_active_job(self):
        self.add_jobs()
        active = self.jobs.active_job()
        self.assertEqual(active.id, 2)

        jobs = Jobs()
        active = jobs.active_job()
        self.assertEqual(active, None, 'no jobs available')
Example #2
0
class TestJobs(unittest.TestCase):
    def setUp(self):
        self.jobobjs = [
            MockJob(id=1, status='complete'),
            MockJob(id=2, status='pending'),
            MockJob(id=3, status='pending')
        ]

        self.jobs = Jobs()

    def add_jobs(self):
        for job in self.jobobjs:
            self.jobs.add_job(job)

    def test_add_job(self):
        self.add_jobs()

        self.assertEqual(
            [ job for job in self.jobs.all() ], 
            [1, 2, 3]
        )
        self.assertEqual(
            [ job.id for job in self.jobs.pending()  ], 
            [2, 3]
        )

    def test_delete_job(self):
        self.add_jobs()

        self.jobs.delete_job(1)
        self.assertEqual(
            [ job for job in self.jobs.all()  ], 
            [2, 3]
        )

    def test_active_job(self):
        self.add_jobs()
        active = self.jobs.active_job()
        self.assertEqual(active.id, 2)

        jobs = Jobs()
        active = jobs.active_job()
        self.assertEqual(active, None, 'no jobs available')

    def test_task_to_hash(self):
        t = Task(
            startframe=1,
            endframe=250,
            status='pending',
            client=None
        )

        self.assertEqual(
            t.to_hash(),
            { 'startframe': 1,
              'endframe': 250,
              'status': 'pending',
              'client': None }
        )
Example #3
0
    def setUp(self):
        self.jobobjs = [
            MockJob(id=1, status='complete'),
            MockJob(id=2, status='pending'),
            MockJob(id=3, status='pending')
        ]

        self.jobs = Jobs()
Example #4
0
class Master(object):
    def __init__(self):
        self.clients = {}
        self.jobs = Jobs()
    

    def add_client(self, client):
        self.clients[client.id] = client
        client.send_line("# Welcome client %d" % (client.id))
        log.msg("Adding client %d" % (client.id))
        log.msg("%d clients currently in pool" % len(self.clients))

    def remove_client(self, id):
        try:
            del self.clients[id]
        except KeyError:
            raise LegionError('No client with id %d' % (id,))

    def clients(self):
        return self.clients

    def get_client(self, id):
        try:
            return self.clients[id]
        except KeyError:
            raise LegionError('No client with id %d' % (id,))

    def idle_clients(self):
        log.msg("Looking for idle clients in pool")
        return (
            self.clients[id]
            for id in self.clients
            if self.clients[id].is_idle()
        )

    def dispatch_idle_clients(self):
        log.msg("Check for clients that need work")
        idle = self.idle_clients()
        active_job = self.jobs.active_job()
        if not active_job: return
        log.msg("Active job %s" % (active_job.to_hash()))
        log.msg("Active tasks %s" % ( [ t.to_hash() for t in active_job.tasks ], ))

        for client in idle:
            task = active_job.assign_next_task(client)
            client.render_task(active_job, task)

    def handle_line(self, client_id, line):
        client = self.get_client(client_id)
        line = line.strip()
        tokens = line.split(' ')
        log.msg(tokens)

        try:
            if len(tokens) == 0:
                raise RuntimeError("Empty line")

            cmd = tokens[0]
            cmd = re.sub(r'\W', '_', cmd)
            
            try:
                func = getattr(self, 'do_%s' % (cmd,))
                func(client, tokens[1:])
            except AttributeError:
                raise
        except Exception, e:
            client.send_line("Error: %s" % (e,))
Example #5
0
 def __init__(self):
     self.clients = {}
     self.jobs = Jobs()