Changeset 5406


Ignore:
Timestamp:
Sep 13, 2010, 12:36:49 PM (12 years ago)
Author:
Nicklas Nordborg
Message:

References #1509: Job queue order after restarting a job

Added a 'scheduled' property to jobs that is used to order the job queue.

Location:
trunk
Files:
12 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/core/common-queries.xml

    r5403 r5406  
    15561556      AND job.type = :type
    15571557      AND job.removed = false
    1558       ORDER BY job.priority ASC, job.created ASC
     1558      ORDER BY job.priority ASC, job.scheduled ASC
    15591559    </sql>
    15601560    <description>
    15611561      A Hibernate query that loads plugin jobs in the job queue waiting to be
    1562       executed sorted by priority and waiting type. The query should return all
     1562      executed sorted by priority and waiting time. The query should return all
    15631563      jobs, ignoring all settings specifying that the internal job queue should
    15641564      not be used.
     
    15751575      AND job.pluginDefinition.useInternalJobQueue = true
    15761576      AND job.jobAgentId IS NULL
    1577       ORDER BY job.priority ASC, job.created ASC
     1577      ORDER BY job.priority ASC, job.scheduled ASC
    15781578    </sql>
    15791579    <description>
     
    38733873  </query>
    38743874
     3875  <query id="SET_SCHEDULED_FOR_JOBS" type="HQL">
     3876    <sql>
     3877      UPDATE JobData job
     3878      SET job.scheduled = job.created
     3879      WHERE job.status > 1 AND job.scheduled IS NULL
     3880    </sql>
     3881    <description>
     3882      An HQL update-query that sets the scheduled date
     3883      to the creation date for existing jobs that have been
     3884      scheduled and doesn't have a scheduled date.
     3885    </description>
     3886  </query>
     3887
     3888
    38753889</predefined-queries>
  • trunk/src/core/net/sf/basedb/core/Install.java

    r5405 r5406  
    115115    method.
    116116  */
    117   public static final int NEW_SCHEMA_VERSION = Integer.valueOf(83).intValue();
     117  public static final int NEW_SCHEMA_VERSION = Integer.valueOf(84).intValue();
    118118 
    119119  public static synchronized void createTables(boolean update, final ProgressReporter progress)
  • trunk/src/core/net/sf/basedb/core/InternalJobQueue.java

    r5399 r5406  
    317317        [AND job.pluginDefinition.useInternalJobQueue = true]
    318318        [AND job.jobAgentId IS NULL]
    319         ORDER BY job.priority ASC, job.created ASC
     319        ORDER BY job.priority ASC, job.scheduled ASC
    320320      */
    321321      query.setInteger("status", Job.Status.WAITING.getValue());
  • trunk/src/core/net/sf/basedb/core/Job.java

    r5405 r5406  
    353353      getData().setActiveProjectId(projectId);
    354354    }
     355    if (action == Action.CREATE && getStatus() == Status.WAITING)
     356    {
     357      getData().setScheduled(new Date());
     358    }
    355359  }
    356360 
     
    673677  }
    674678
     679  /**
     680    Get the date and time the job was scheduled to the job queue.
     681    @return A date, or null if the job hasn't been scheduled
     682    @since 2.16
     683  */
     684  public Date getScheduled()
     685  {
     686    return DateUtil.copy(getData().getScheduled());
     687  }
     688 
    675689  /**
    676690    Get the date and time the job was started.
     
    10931107    data.setStatus(Job.Status.WAITING.getValue());
    10941108    data.setPercentComplete(0);
     1109    data.setScheduled(new Date());
    10951110    data.setStarted(null);
    10961111    data.setEnded(null);
     
    11241139      job is started, if not given {@link Request#COMMAND_EXECUTE}
    11251140      is used
     1141    @param isShuttingDown TRUE if the system is currently shutting down
     1142      (which means that the 'scheduled' data is not reset to allow the
     1143      job to be started first once the system is up and running)
    11261144    @since 2.16
    11271145  */
    1128   void continueLater(String executeCommand)
     1146  void continueLater(String executeCommand, boolean isShuttingDown)
    11291147  {
    11301148    JobData data = getData();
    11311149    data.setStatus(Job.Status.WAITING.getValue());
    11321150    data.setPercentComplete(0);
     1151    if (!isShuttingDown)  data.setScheduled(new Date());
    11331152    data.setStarted(null);
    11341153    data.setEnded(null);
     
    13661385    }
    13671386    getData().setStatus(Status.WAITING.getValue());
     1387    getData().setScheduled(new Date());
    13681388    setParameterValuesInternal(name, label, description, parameterType, Arrays.asList(value), true);
    13691389  }
     
    14171437    }
    14181438    getData().setStatus(Status.WAITING.getValue());
     1439    getData().setScheduled(new Date());
    14191440    setParameterValuesInternal(name, label, description, parameterType, values, true);
    14201441  }
     
    16841705      JobData data = job.getData();
    16851706      data.setStatus(Job.Status.WAITING.getValue());
     1707      data.setScheduled(new Date());
    16861708      data.setPercentComplete(0);
    16871709      data.setStarted(null);
     
    17191741    }
    17201742   
    1721     void setEnded(PluginResponse response)
     1743    void setEnded(PluginResponse response, boolean isShuttingDown)
    17221744    {
    17231745      DbControl dc = null;
     
    17351757        {
    17361758          // The job will be continue again when the system restarts
    1737           job.continueLater(response.getNextCommand());
     1759          job.continueLater(response.getNextCommand(), isShuttingDown);
    17381760        }
    17391761        else
  • trunk/src/core/net/sf/basedb/core/JobAgent.java

    r5384 r5406  
    390390      AND job.status = Job.Status.WAITING
    391391      AND job.jobAgentId IS NULL OR job.jobAgentId = this.id
    392       ORDER BY job.priority - jas.priorityBoost ASC, job.created ASC
     392      ORDER BY job.priority - jas.priorityBoost ASC, job.scheduled ASC
    393393    */
    394394   
     
    512512   
    513513    // Order by the waiting time
    514     query.order(Orders.asc(Hql.property("created")));
     514    query.order(Orders.asc(Hql.property("scheduled")));
    515515   
    516516    return query;
  • trunk/src/core/net/sf/basedb/core/PluginExecutionRequest.java

    r4889 r5406  
    2727import net.sf.basedb.core.plugin.Request;
    2828import net.sf.basedb.core.plugin.Response;
     29import net.sf.basedb.core.signal.DelegatingSignalHandler;
     30import net.sf.basedb.core.signal.Signal;
    2931import net.sf.basedb.core.signal.SignalHandler;
    3032import net.sf.basedb.core.signal.SignalReceiver;
     
    8890    Request request = new RequestImpl(false);
    8991    Response response = pluginResponse.getResponseImpl();
    90     SignalHandler signalHandler = null;
     92    DelegatingSignalHandler signalHandler = null;
     93    boolean isShutdown = false;
    9194    Pinger pinger = null;
    9295    try
     
    98101      {
    99102        // Register a signal handler, receiver and transporter
    100         signalHandler = ((SignalTarget)plugin).getSignalHandler();
    101         if (signalReceiver != null && signalHandler != null)
     103        // We wrap the signal handler in a DelegatingSignalHandler
     104        // since we need to know if the SHUTDOWN signal has been sent
     105        SignalHandler sh = ((SignalTarget)plugin).getSignalHandler();
     106        if (signalReceiver != null && sh != null)
    102107        {
     108          signalHandler = new DelegatingSignalHandler(sh);
    103109          signalId = signalReceiver.registerSignalHandler(signalHandler);
    104110          signalTransporter = signalReceiver.getSignalTransporterClass();
     
    123129    }
    124130    done();
    125     if (progress != null) progress.setEnded(pluginResponse);
     131    if (progress != null) progress.setEnded(pluginResponse, signalHandler.hasReceived(Signal.SHUTDOWN));
    126132    return pluginResponse;
    127133  }
  • trunk/src/core/net/sf/basedb/core/Update.java

    r5405 r5406  
    908908    </td>
    909909  </tr>
     910  <tr>
     911    <td>84</td>
     912    <td>
     913      Added {@link JobData#getScheduled()}. The update will set the scheduled
     914      date to the creation date for all existing jobs that is in WAITING status
     915      or higher.
     916    </td>
     917  </tr>
    910918  </table>
    911919
     
    12381246        schemaVersion = setSchemaVersionInTransaction(session, 83);
    12391247      }
    1240    
     1248
     1249      if (schemaVersion < 84)
     1250      {
     1251        if (progress != null) progress.display((int)(83*progress_factor),
     1252            "--Updating schema version: " + schemaVersion + " -> 84...");
     1253        schemaVersion = updateToSchemaVersion84(session);
     1254      }
     1255
    12411256      sc.logout();
    12421257      if (progress != null) progress.display(100, "Database updated successfully.");
     
    28652880      if (tx != null) HibernateUtil.rollback(tx);
    28662881      log.error("updateToSchemaVersion80: FAILED", ex);
     2882      throw ex;
     2883    }
     2884    return schemaVersion;
     2885  }
     2886
     2887  /**
     2888    Set the scheduled data for existing jobs.
     2889  */
     2890  private static int updateToSchemaVersion84(org.hibernate.Session session)
     2891    throws BaseException
     2892  {
     2893    final int schemaVersion = 84;
     2894    org.hibernate.Transaction tx = null;
     2895    try
     2896    {
     2897      tx = HibernateUtil.newTransaction(session);
     2898     
     2899      // Load and execute the query
     2900      org.hibernate.Query query = HibernateUtil.getPredefinedQuery(session, "SET_SCHEDULED_FOR_JOBS");
     2901      /*
     2902        UPDATE JobData job
     2903        SET job.scheduled = job.created
     2904        WHERE job.status > WAITING (=1) AND job.scheduled IS NULL
     2905      */
     2906      query.executeUpdate();
     2907     
     2908      // Update the schema version number
     2909      setSchemaVersion(session, schemaVersion);
     2910 
     2911      // Commit the changes
     2912      HibernateUtil.commit(tx);
     2913      log.info("updateToSchemaVersion84: OK");
     2914    }
     2915    catch (BaseException ex)
     2916    {
     2917      if (tx != null) HibernateUtil.rollback(tx);
     2918      log.error("updateToSchemaVersion84: FAILED", ex);
    28672919      throw ex;
    28682920    }
  • trunk/src/core/net/sf/basedb/core/data/JobData.java

    r5405 r5406  
    371371  }
    372372
     373  private Date scheduled;
     374  /**
     375    Get the date and time the job was scheduled to the job queue, or null
     376    if it hasn't been scheduled yet. This timestamp should be updated whenever
     377    the status is changed to WAITING.
     378    @hibernate.property column="`scheduled`" type="timestamp" not-null="false"
     379    @since 2.16
     380  */
     381  public Date getScheduled()
     382  {
     383    return scheduled;
     384  }
     385  public void setScheduled(Date scheduled)
     386  {
     387    this.scheduled = scheduled;
     388  }
     389 
    373390  private Date started;
    374391  /**
  • trunk/src/core/net/sf/basedb/core/signal/DelegatingSignalHandler.java

    r5384 r5406  
    2222package net.sf.basedb.core.signal;
    2323
     24import java.util.ArrayList;
    2425import java.util.Collection;
    2526import java.util.Collections;
     
    2728import java.util.HashSet;
    2829import java.util.Iterator;
     30import java.util.List;
    2931import java.util.Map;
    3032import java.util.Set;
     
    5355  */
    5456  private Map<Signal, Set<SignalHandler>> handlers;
     57
     58  /**
     59    List holding received signals.
     60    @since 2.16
     61  */
     62  private final List<Signal> received;
    5563 
    5664  /**
     
    6371    super();
    6472    handlers = new HashMap<Signal, Set<SignalHandler>>();
     73    received = Collections.synchronizedList(new ArrayList<Signal>());
     74  }
     75 
     76  /**
     77    Create a new delegating signal handler and register the
     78    given handlers as delegates.
     79    @param handlers An array with signal handlers that signal should
     80      be delegated to
     81    @since 2.16
     82  */
     83  public DelegatingSignalHandler(SignalHandler... handlers)
     84  {
     85    this();
     86    for (SignalHandler sh : handlers)
     87    {
     88      registerSignalHandler(sh);
     89    }
    6590  }
    6691 
     
    104129      throw new UnsupportedSignalException(signal);
    105130    }
     131    received.add(signal);
    106132    for (SignalHandler handler : all)
    107133    {
     
    153179  }
    154180
     181  /**
     182    Check if any signals has been received by this handler.
     183    @return TRUE if at least one signal has been received, FALSE otherwise
     184    @since 2.16
     185  */
     186  public boolean hasReceivedSignals()
     187  {
     188    return received.size() > 0;
     189  }
     190 
     191  /**
     192    Get the list of received signals. Calling this method clears the internal
     193    list and if no signals are received in the meantime, the next call to
     194    this method will return null.
     195    @return A list with signals in the order they were
     196      received, or null if no signals has been received
     197    @since 2.16
     198  */
     199  public List<Signal> getReceivedSignals()
     200  {
     201    List<Signal> toReturn = null;
     202    if (received.size() > 0)
     203    {
     204      toReturn = new ArrayList<Signal>(received);
     205      received.clear();
     206    }
     207    return toReturn;
     208  }
     209 
     210  /**
     211    Check if the given signal has been received by this signal handler.
     212    @param signal The signal to check for
     213    @return TRUE if the signal has been received, FALSE otherwise
     214    @since 2.16
     215  */
     216  public boolean hasReceived(Signal signal)
     217  {
     218    return received.contains(signal);
     219  }
     220
     221 
    155222}
  • trunk/www/views/jobs/index.jsp

    r4889 r5406  
    5656<%@ taglib prefix="base" uri="/WEB-INF/base.tld" %>
    5757<%!
    58   private static final ItemContext defaultContext = Base.createDefaultContext("name", "name,pluginType,percentComplete,status,statusMessage,description");
     58  private static final ItemContext defaultContext = Base.createDefaultContext("scheduled", "name,pluginType,scheduled,percentComplete,status,statusMessage,description");
     59  {
     60    defaultContext.setSortDirection(ItemContext.SortDirection.DESC);
     61  }
    5962  private static final Item itemType = Item.JOB;
    6063%>
  • trunk/www/views/jobs/list_jobs.jsp

    r5351 r5406  
    345345        datatype="date"
    346346        title="Created"
     347        sortable="true"
     348        filterable="true"
     349        exportable="true"
     350        formatter="<%=dateTimeFormatter%>"
     351      />
     352      <tbl:columndef
     353        id="scheduled"
     354        property="scheduled"
     355        datatype="date"
     356        title="Scheduled"
    347357        sortable="true"
    348358        filterable="true"
     
    644654                </tbl:cell>
    645655                <tbl:cell column="created" value="<%=item.getCreated()%>" />
     656                <tbl:cell column="scheduled" value="<%=item.getScheduled()%>" />
    646657                <tbl:cell column="started" value="<%=item.getStarted()%>" />
    647658                <tbl:cell column="ended" value="<%=item.getEnded()%>" />
  • trunk/www/views/jobs/view_job.jsp

    r5404 r5406  
    378378        <td>
    379379          <%=dateTimeFormatter.format(job.getCreated())%>
     380        </td>
     381      </tr>
     382      <tr valign="top">
     383        <td class="prompt">Scheduled</td>
     384        <td>
     385          <%=dateTimeFormatter.format(job.getScheduled())%>
    380386        </td>
    381387      </tr>
Note: See TracChangeset for help on using the changeset viewer.