// New EM (after setting thread name)
EntityManager em = Helpers.getNewEm();
// Get configuration data
Node node = em.find(Node.class, this.engine.getNode().getId());
this.step = Long.parseLong(Helpers.getParameter("internalPollingPeriodMs", String.valueOf(this.step), em));
this.alive = Long.parseLong(Helpers.getParameter("aliveSignalMs", String.valueOf(this.step), em));
em.close();
this.localThread = Thread.currentThread();
// Launch main loop
long sinceLatestPing = 0;
while (true)
{
try
{
Thread.sleep(this.step);
}
catch (InterruptedException e)
{
run = false;
}
if (!run)
{
break;
}
// Get session
em = Helpers.getNewEm();
// Check if stop order
node = em.find(Node.class, node.getId());
if (node.isStop())
{
jqmlogger.info("Node has received a stop order from the database");
jqmlogger.trace("At stop order time, there are " + this.engine.getCurrentlyRunningJobCount() + " jobs running in the node");
this.run = false;
this.engine.stop();
em.close();
break;
}
// I am alive signal
sinceLatestPing += this.step;
if (sinceLatestPing >= this.alive * 0.9)
{
em.getTransaction().begin();
em.createQuery("UPDATE Node n SET n.lastSeenAlive = current_timestamp() WHERE n.id = :id").setParameter("id", node.getId())
.executeUpdate();
em.getTransaction().commit();
sinceLatestPing = 0;
}