Skip to content

Commit

Permalink
Merge pull request #640 from scireum/aha/StateUpdateLimit
Browse files Browse the repository at this point in the history
Limits the excessive updates of the task context state.
  • Loading branch information
sabieber authored Jul 23, 2019
2 parents 904ed11 + 0e7d03e commit 3ce247b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/main/java/sirius/web/data/CSVProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import sirius.kernel.async.TaskContext;
import sirius.kernel.commons.BOMReader;
import sirius.kernel.commons.CSVReader;
import sirius.kernel.commons.RateLimit;
import sirius.kernel.nls.NLS;

import java.io.InputStream;
Expand All @@ -36,11 +37,14 @@ public void run(RowProcessor rowProcessor, Predicate<Exception> errorHandler) th
CSVReader reader = new CSVReader(new BOMReader(new InputStreamReader(input, Charsets.UTF_8)));
AtomicInteger rowCounter = new AtomicInteger(0);
TaskContext tc = TaskContext.get();
RateLimit stateUpdateLimiter = tc.shouldUpdateState();

reader.execute(row -> {
try {
rowProcessor.handleRow(rowCounter.incrementAndGet(), row);
tc.setState(NLS.get("LineBasedProcessor.linesProcessed"), rowCounter.get());
if (stateUpdateLimiter.check()) {
tc.setState(NLS.get("LineBasedProcessor.linesProcessed"), rowCounter.get());
}
} catch (Exception e) {
if (!errorHandler.test(e)) {
throw e;
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/sirius/web/data/XLSProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import sirius.kernel.async.TaskContext;
import sirius.kernel.commons.Doubles;
import sirius.kernel.commons.RateLimit;
import sirius.kernel.commons.Strings;
import sirius.kernel.commons.Values;
import sirius.kernel.nls.NLS;
Expand Down Expand Up @@ -52,6 +53,8 @@ public void run(RowProcessor rowProcessor, Predicate<Exception> errorHandler) th
Iterator<Row> iter = sheet.rowIterator();
int current = 0;
TaskContext tc = TaskContext.get();
RateLimit stateUpdateLimiter = tc.shouldUpdateState();

while (iter.hasNext() && tc.isActive()) {
try {
current++;
Expand All @@ -65,7 +68,9 @@ public void run(RowProcessor rowProcessor, Predicate<Exception> errorHandler) th
values.add(value);
}
rowProcessor.handleRow(current, Values.of(values));
tc.setState(NLS.get("LineBasedProcessor.linesProcessed"), current);
if (stateUpdateLimiter.check()) {
tc.setState(NLS.get("LineBasedProcessor.linesProcessed"), current);
}
} catch (Exception e) {
if (!errorHandler.test(e)) {
throw e;
Expand Down

0 comments on commit 3ce247b

Please sign in to comment.