Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DNM] Delegate agency scheduled task run to submit #2825

Open
wants to merge 3 commits into
base: development
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 27 additions & 19 deletions src/foam/core/AbstractAgency.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,44 @@ foam.CLASS({
],

javaCode: `
private static final Timer TIMER = new Timer();
private static final ConcurrentHashMap<String, TimerTask> TASK_QUEUE = new ConcurrentHashMap<>();
protected static final Timer TIMER = new Timer("Agency Scheduler");
protected static final ConcurrentHashMap<String, TimerTask> TASK_QUEUE = new ConcurrentHashMap<>();

public void schedule(X x, ContextAgent agent, String key, long delay) {
if ( delay <= 0 ) {
submit(x, agent, key);
return;
}

var logger = Loggers.logger(x, this, "schedule");

// Do not re-schedule existing task with the same key. Subsequent attempts
// to schedule the same task/key are ignored until the task is executed
// and removed from the queue.
if ( ! TASK_QUEUE.containsKey(key) ) {
var task = new TimerTask() {
public void run() {
X oldX = ((ProxyX) XLocator.get()).getX();
XLocator.set(x);
try {
agent.execute(x);
} catch ( java.lang.Exception e ) {
Loggers.logger(x, this).error("schedule", "failed", key, e);
} finally {
XLocator.set(oldX);
}
TASK_QUEUE.remove(key);
}
};
TASK_QUEUE.put(key, task);
TIMER.schedule(task, delay);
if ( TASK_QUEUE.containsKey(key) ) {
logger.info("ignored re-scheduling existing task", key);
return;
}

// Schedule new task to execute the agent
var task = new TimerTask() {
public void run() {
logger.debug("running", key, delay);
submit(x,
(x) -> {
try {
agent.execute(x);
} catch ( Throwable t ) {
logger.error("failed", key, t);
} finally {
TASK_QUEUE.remove(key);
}
}
, key);
}
};
TASK_QUEUE.put(key, task);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this should be before the run(), no guarantee that calling thread continues before the timertask.

TIMER.schedule(task, delay);
}
`
});