Skip to content

Commit

Permalink
Different implementation of process activation and deactivation
Browse files Browse the repository at this point in the history
Hibernating and dehibernating a task instead of adding and removing it
from teh scheduler's queue.
  • Loading branch information
iliasger committed Dec 22, 2015
1 parent a684d2a commit 02e26e0
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -647,9 +647,9 @@ void componentProcessActiveChanged(ComponentInstance instance, ComponentProcess
Log.d(String.format("Changing the activity of task %s corresponding to process %s to %s.", t, process, active));

if (active) {
scheduler.addTask(t);
scheduler.deHibernateTask(t);
} else {
scheduler.removeTask(t);
scheduler.hibernateTask(t);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,9 @@ public interface Scheduler extends ExecutionListener, TimerEventListener {
public void setExecutor(Executor executor);

public Timer getTimer();

public void hibernateTask( Task task );

public void deHibernateTask( Task task );

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public class SchedulerEvent implements Comparable<SchedulerEvent> {
static final int CANCELLED = 2;
static final int EXECUTED = 3;
static final int RUNNING = 4;
static final int FAILED = 5;
static final int FAILED = 5;
static final int HIBERNATED = 6;

/**
* Next execution time for this task in the format returned by
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,34 @@ public void removeTask(Task task) {
allTasks.remove(task);
}

@Override
public void hibernateTask(Task task) {
if (task == null) {
throw new IllegalArgumentException("The task to be hibernated cannot be null");
}
if (!allTasks.contains(task)) {
Log.w("Attempting to hibernate a non-existing task.");
return;
}
for (SchedulerEvent event: queue.getTaskEvents(task)) {
event.state = SchedulerEvent.HIBERNATED;
}
}

@Override
public void deHibernateTask(Task task) {
if (task == null) {
throw new IllegalArgumentException("The task to be dehibernated cannot be null");
}
if (!allTasks.contains(task)) {
Log.w("Attempting to dehibernate a non-existing task.");
return;
}
for (SchedulerEvent event: queue.getTaskEvents(task)) {
event.state = SchedulerEvent.RUNNING;
}
}

@Override
public void executionCompleted(Task task, Trigger trigger) {
knowledgeChangeTriggers.remove(trigger);
Expand All @@ -136,7 +164,9 @@ public void at(long time) {
while ((!queue.isEmpty()) && (queue.first().nextExecutionTime <= time)) {
SchedulerEvent event = queue.pollFirst();

executor.execute(event.executable, event.trigger);
if (event.state != SchedulerEvent.HIBERNATED) {
executor.execute(event.executable, event.trigger);
}

if (event.periodic) {
// schedule for the next period add a random offset within the period (up to 75% of the period)
Expand Down

0 comments on commit 02e26e0

Please sign in to comment.