def killJob(job_id):
    """Kills every task associated with job_id. Killed tasks have status code 
    'K'. If a task was already started, an a kill request is sent to the host 
    running it.
    @return: False if no errors while killing started tasks, else True"""
    
    # mark all of the Ready tasks as Killed
    with transaction() as t:
        t.cur.execute("""update Hydra_rendertask set status = 'K' 
                        where job_id = '%d' and status = 'R'""" % job_id)
    
    # get hostnames for tasks that were already started
    tuples = None # @UnusedVariable
    with transaction() as t:
        t.cur.execute("""select host from Hydra_rendertask 
                        where job_id = '%d' and status = 'S'""" % job_id)
        tuples = t.cur.fetchall()
        
    # make flat list out of single-element tuples fetched from db
    hosts = [t for (t,) in tuples]
    
    # send a kill request to each host, note if any failures occurred
    error = False
    for host in hosts:
        try:
            error = error or not sendKillQuestion(host)
        except socketerror:
            logger.debug("There was a problem communicating with {:s}"
                         .format(host))
            error = True
    
    return error
 def offline(self):
     """Changes the local render node's status to offline"""
     with transaction():
         [thisNode] = Hydra_rendernode.fetch ("where host = '%s'" % Utils.myHostName( ))
         thisNode.status = OFFLINE
         thisNode.update()
     self.updateRenderNodeInfo()
def resurrectJob(job_id):
    """Resurrects job with the given id. Tasks marked 'K' or 'F' will have 
    their data cleared and their statuses set to 'R'"""
    
    with transaction() as t:
        t.cur.execute("""update Hydra_rendertask 
                        set status = 'R' 
                        where job_id = '%d' and 
                        status = 'K' or status = 'F'""" % job_id)
def prioritizeJob(job_id, priority):
    
    with transaction() as t:
        t.cur.execute("""update Hydra_job
                        set priority = '%d'
                        where id = '%d'""" % (priority, job_id))
        t.cur.execute("""update Hydra_rendertask
                        set priority = '%d'
                        where job_id = '%d'""" % (priority, job_id))
 def createTasks(self, job):
     task = Hydra_rendertask( status = READY,
                              command = repr( self.command ),
                              job_id = job.id, 
                              priority = self.priority,
                              project = self.project,
                              createTime = job.createTime)
     with transaction() as t:
         task.insert(transaction=t)
 def createJob( self ):
     job = Hydra_job( pickledTicket = pickle.dumps( self ), 
                      priority = self.priority, 
                      project = self.project,
                      requirements = self.capability_pattern,
                      createTime = datetime.now())
     with transaction() as t:
         job.insert(transaction=t)
         
     return job
 def online(self):
     """Changes the local render node's status to online if it wasn't on-line already"""
     with transaction():
         [thisNode] = Hydra_rendernode.fetch ("where host = '%s'" % Utils.myHostName( ))
         if thisNode.status == OFFLINE:
             thisNode.status = IDLE
             thisNode.update()
         else:
             logger.debug("Node is already online.")
         self.updateRenderNodeInfo()
 def updateRenderNodeInfo(self):
     with transaction():
         [thisNode] = Hydra_rendernode.fetch ("where host = '%s'" % Utils.myHostName( ))
     
     if thisNode.host:
         self.nameLabel.setText("Node name: " + thisNode.host)
         self.statusLabel.setText("Status: " + codes[thisNode.status])
         if thisNode.task_id:
             self.jobLabel.setText("Job id: {0:d}".format(thisNode.task_id))
         else:
             self.jobLabel.setText("Job id: None")
     else:
         QMessageBox.about(self, "Error", "This computer is not registered as a render node.")
    def populateRequirementsListWidget(self):
        """Populates the requirements list widget."""

        # get current list of capabilities from the database
        tuples = None
        with transaction() as t:
            t.cur.execute("select * from Hydra_capabilities order by name")
            tuples = t.cur.fetchall()
        
        # flatten list of tuples fetched from the database
        capabilities = [t for (t,) in tuples]
        
        # populate the dropdown
        for item in capabilities:
            self.requirementsListWidget.addItem(item)        
    def populateExecutableComboBox(self):
        """Populates the executables dropdown box."""

        # get current list of executables from the database
        tuples = None
        with transaction() as t:
            t.cur.execute("select * from Hydra_executable")
            tuples = t.cur.fetchall()
        
        # flatten list of tuples fetched from the database
        executables = [t for (t,) in tuples]
        
        # populate the dropdown
        for program in executables:
            self.executableComboBox.addItem(program)
def killTask(task_id):
    """Kills the task with the specified id. If the task has been started, a 
    kill request is sent to the node running it.
    @return: True if there were no errors killing the task, else False."""
    
    [task] = Hydra_rendertask.fetch("where id = '%d'" % task_id)
    if task.status == READY:
        task.status = KILLED
        with transaction() as t:
            task.update(t)
        # if we reach this point: transaction successful, no exception raised
        return True
    elif task.status == STARTED:
        killed = sendKillQuestion(renderhost = task.host, newStatus = KILLED)
        # if we reach this point: TCPconnection successful, no exception raised
        return killed
    return False
 def createTasks( self, job ):
     starts = range( self.startFrame, self.endFrame + 1, self.batchSize )
     ends = [min( start + self.batchSize - 1,
                  self.endFrame )
             for start in starts
             ]
     for start, end in zip( starts, ends ):
         command = self.renderCommand(start, end)
         logger.debug( command )
         task = Hydra_rendertask( status = READY, 
                                  command = repr( command ),
                                  job_id = job.id, 
                                  priority = self.priority, 
                                  project = self.project,
                                  createTime = job.createTime,
                                  requirements = job.requirements,
                                  )
         with transaction() as t:
             task.insert(transaction=t)
def unregister(host=None):
    """
    Removes the specified node from Hydra_rendernode table. If none provided,
    then it gets the host name of the current machine / configuration and
    removes that from the database. For example, if the current machine's
    computer name is FOO and the DNS extension listed in hydraSettings.cfg is
    .xyz, then the host to be deleted is FOO.xyz
    """
    
    if not host:
        host = Utils.myHostName()
    
    [node] = Hydra_rendernode.fetch("where host = '%s'" % host)
    if node:
        with transaction() as t:
            t.cur.execute("delete from Hydra_rendernode where host = '%s'" % host)
        return True
    
    return False
def unstick (taskID=None, newTaskStatus=READY,
             host=None, newHostStatus=IDLE):
    with transaction () as t:
        if taskID:
            [task] = Hydra_rendertask.fetch ("where id = %d" % taskID)

            # if the task is marked, say, CRASHED, leave the host name alone.
            # only READY would be confusing with a host named filled in. I think.
            if newTaskStatus == READY:
                task.host = None

            task.status = newTaskStatus
            task.startTime = None
            task.endTime = None
            task.update (t)
        if host:
            [host] = Hydra_rendernode.fetch ("where host = '%s'" % host)
            host.task_id = None
            host.status = newHostStatus
            host.update (t)
def resurrectTask(task_id, ignoreStarted = False):
    """Resurrects the task with the specified id. 
    @return: True if there was an error, such as when the user tries to 
             resurrect a task that is marked as Started, else False."""
    
    [task] = Hydra_rendertask.fetch("where id = '%d'" % task_id)
    if (
            task.status == 'K' or task.status == 'F' or 
            (task.status == 'S' and ignoreStarted == True)
        ):
        task.status = 'R'
        task.host = None
        task.startTime = None
        task.endTime = None
    else:
        return True

    with transaction() as t:
        task.update(t)
    
    return False
    def populateProjectComboBox(self):
        """Populates the projects dropdown box."""

        # get current list of projects from the database
        tuples = None
        with transaction() as t:
            t.cur.execute("select * from Hydra_projects")
            tuples = t.cur.fetchall()
        
        # flatten list of tuples fetched from the database
        projectsList = [t for (t,) in tuples]
        
        # populate the dropdown
        for project in projectsList:
            self.projectComboBox.addItem(project)
        
        # show default project selection
        [thisNode] = Hydra_rendernode.fetch(
            "where host = '%s'" % Utils.myHostName())
        idx = self.projectComboBox.findText(
            thisNode.project,
            flags=Qt.MatchExactly|Qt.MatchCaseSensitive)
        self.projectComboBox.setCurrentIndex(idx)
import os

import LoggingSetup
import Utils

from MySQLSetup import Hydra_rendernode, OFFLINE, IDLE, transaction

with transaction () as t:
    [thisNode] = Hydra_rendernode.fetch( "where host = '%s'" % Utils.myHostName( ) )
    if thisNode.status == OFFLINE:
        thisNode.status = IDLE
        thisNode.update(t)