Skip to content

Commit

Permalink
Fix multi-instance mode (hibernate)
Browse files Browse the repository at this point in the history
Some jobs were never ending or were being run twice because the
job status was put back to RUNNING when the client was getting the
status.
  • Loading branch information
Patrick Valsecchi committed Apr 5, 2017
1 parent 3bacef1 commit 5a7d7ba
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 11 deletions.
1 change: 1 addition & 0 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -335,4 +335,5 @@ task createDocker(type: Docker, dependsOn: 'unzipWar') {
setEnvironment('SPRING_LOG_LEVEL', 'WARN')
setEnvironment('JASPER_LOG_LEVEL', 'WARN')
setEnvironment('APACHE_LOG_LEVEL', 'WARN')
setEnvironment('SQL_LOG_LEVEL', 'WARN')
}
1 change: 1 addition & 0 deletions core/logback-custom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
<logger name="com.codahale.metrics" level="INFO" />
<logger name="net.sf.jasperreports" level="${JASPER_LOG_LEVEL}" />
<logger name="org.apache" level="${APACHE_LOG_LEVEL}" />
<logger name="org.hibernate" level="${SQL_LOG_LEVEL}" />
</included>
Original file line number Diff line number Diff line change
Expand Up @@ -363,10 +363,8 @@ public final void cancel(final String referenceId) throws NoSuchReferenceExcepti

@Override
public final PrintJobStatus getStatus(final String referenceId) throws NoSuchReferenceException {
PrintJobStatus jobStatus = null;

// check if the reference id is valid
jobStatus = this.jobQueue.get(referenceId, true);
final PrintJobStatus jobStatus = this.jobQueue.get(referenceId, true);
jobStatus.getEntry().assertAccess();

if (jobStatus.getStatus() == PrintJobStatus.Status.WAITING) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
*/
@Transactional
public class HibernateJobQueue implements JobQueue {

private static final int DEFAULT_TIME_TO_KEEP_AFTER_ACCESS = 30; /* minutes */

private static final long DEFAULT_CLEAN_UP_INTERVAL = 300; /* seconds */
Expand Down Expand Up @@ -96,8 +95,7 @@ public final PrintJobStatus get(final String referenceId, final boolean external
}
record.setStatusTime(now);
if (!record.isDone() && external) {
record.setLastCheckTime(System.currentTimeMillis());
this.dao.save(record);
this.dao.updateLastCheckTime(referenceId, System.currentTimeMillis());
}
return record;
}
Expand Down Expand Up @@ -222,5 +220,4 @@ protected void doInTransactionWithoutResult(final TransactionStatus status) {
}
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public final Session getSession() {
*/
public final void save(final PrintJobStatusExtImpl entry) {
getSession().merge(entry);
getSession().flush();
getSession().evict(entry);
}

/**
Expand All @@ -57,7 +59,9 @@ public final void save(final PrintJobStatusExtImpl entry) {
* @return
*/
public final PrintJobStatusExtImpl get(final String id) {
return get(id, false);
final PrintJobStatusExtImpl result = get(id, false);
getSession().evict(result);
return result;
}

/**
Expand All @@ -72,8 +76,10 @@ public final PrintJobStatusExtImpl get(final String id, final boolean lock) {
Criteria c = getSession().createCriteria(PrintJobStatusExtImpl.class);
c.add(Restrictions.idEq(id));
if (lock) { //LOCK means SELECT FOR UPDATE which prevents these records to be pulled by different instances
c.setLockMode("pj", LockMode.PESSIMISTIC_READ);
c.setFetchMode("result", FetchMode.SELECT);
c.setLockMode("pj", LockMode.PESSIMISTIC_READ);
c.setFetchMode("result", FetchMode.SELECT);
} else {
c.setReadOnly(true); // make sure the object is not updated if there is no lock
}
return (PrintJobStatusExtImpl) c.uniqueResult();
}
Expand Down Expand Up @@ -152,6 +158,19 @@ public final void cancelOld(final long starttimeThreshold, final long checkTimeT
query.executeUpdate();
}

/**
* Update the lastCheckTime of the given record.
* @param id the id
* @param lastCheckTime the new value
*/
public final void updateLastCheckTime(final String id, final long lastCheckTime) {
Query query = getSession().createQuery("update PrintJobStatusExtImpl pj " + "set lastCheckTime=:lastCheckTime "
+ "where pj.referenceId = :id");
query.setParameter("id", id);
query.setParameter("lastCheckTime", lastCheckTime);
query.executeUpdate();
}

/**
* Delete old jobs.
*
Expand Down Expand Up @@ -195,5 +214,4 @@ public final PrintJobResultExtImpl getResult(final URI reportURI) {
c.add(Restrictions.idEq(reportURI.toString()));
return (PrintJobResultExtImpl) c.uniqueResult();
}

}
1 change: 1 addition & 0 deletions core/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<logger name="com.codahale.metrics.JmxReporter" level="INFO" />
<logger name="org.springframework" level="WARN" />
<logger name="org.apache" level="WARN" />
<logger name="org.hibernate" level="WARN" />

<root level="INFO">
<appender-ref ref="standardOut" />
Expand Down

0 comments on commit 5a7d7ba

Please sign in to comment.