Skip to content

Commit f703603

Browse files
authored
Merge pull request #605 from rivaldi8/scheduled-export-not-running-after-fix
Bugfix 604: Scheduled exports aren't executed if they're checked before the next run Fixes #604
2 parents 5642de8 + c13a94c commit f703603

2 files changed

Lines changed: 25 additions & 18 deletions

File tree

app/src/main/java/org/gnucash/android/service/ScheduledActionService.java

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public static void processScheduledActions(List<ScheduledAction> scheduledAction
127127
*/
128128
private static void executeScheduledEvent(ScheduledAction scheduledAction, SQLiteDatabase db){
129129
Log.i(LOG_TAG, "Executing scheduled action: " + scheduledAction.toString());
130-
int executionCount = scheduledAction.getExecutionCount();
130+
int executionCount = 0;
131131

132132
switch (scheduledAction.getActionType()){
133133
case TRANSACTION:
@@ -139,20 +139,21 @@ private static void executeScheduledEvent(ScheduledAction scheduledAction, SQLit
139139
break;
140140
}
141141

142-
//the last run time is computed instead of just using "now" so that if the more than
143-
// one period has been skipped, all intermediate transactions can be created
144-
145-
scheduledAction.setLastRun(System.currentTimeMillis());
146-
//set the execution count in the object because it will be checked for the next iteration in the calling loop
147-
scheduledAction.setExecutionCount(executionCount); //this call is important, do not remove!!
148-
//update the last run time and execution count
149-
ContentValues contentValues = new ContentValues();
150-
contentValues.put(DatabaseSchema.ScheduledActionEntry.COLUMN_LAST_RUN,
151-
scheduledAction.getLastRunTime());
152-
contentValues.put(DatabaseSchema.ScheduledActionEntry.COLUMN_EXECUTION_COUNT,
153-
scheduledAction.getExecutionCount());
154-
db.update(DatabaseSchema.ScheduledActionEntry.TABLE_NAME, contentValues,
155-
DatabaseSchema.ScheduledActionEntry.COLUMN_UID + "=?", new String[]{scheduledAction.getUID()});
142+
if (executionCount > 0) {
143+
scheduledAction.setLastRun(System.currentTimeMillis());
144+
// Set the execution count in the object because it will be checked
145+
// for the next iteration in the calling loop.
146+
// This call is important, do not remove!!
147+
scheduledAction.setExecutionCount(scheduledAction.getExecutionCount() + executionCount);
148+
// Update the last run time and execution count
149+
ContentValues contentValues = new ContentValues();
150+
contentValues.put(DatabaseSchema.ScheduledActionEntry.COLUMN_LAST_RUN,
151+
scheduledAction.getLastRunTime());
152+
contentValues.put(DatabaseSchema.ScheduledActionEntry.COLUMN_EXECUTION_COUNT,
153+
scheduledAction.getExecutionCount());
154+
db.update(DatabaseSchema.ScheduledActionEntry.TABLE_NAME, contentValues,
155+
DatabaseSchema.ScheduledActionEntry.COLUMN_UID + "=?", new String[]{scheduledAction.getUID()});
156+
}
156157
}
157158

158159
/**
@@ -219,6 +220,7 @@ private static int executeTransactions(ScheduledAction scheduledAction, SQLiteDa
219220
int totalPlannedExecutions = scheduledAction.getTotalPlannedExecutionCount();
220221
List<Transaction> transactions = new ArrayList<>();
221222

223+
int previousExecutionCount = scheduledAction.getExecutionCount(); // We'll modify it
222224
//we may be executing scheduled action significantly after scheduled time (depending on when Android fires the alarm)
223225
//so compute the actual transaction time from pre-known values
224226
long transactionTime = scheduledAction.computeNextCountBasedScheduledExecutionTime();
@@ -235,6 +237,8 @@ private static int executeTransactions(ScheduledAction scheduledAction, SQLiteDa
235237
}
236238

237239
transactionsDbAdapter.bulkAddRecords(transactions, DatabaseAdapter.UpdateMethod.insert);
240+
// Be nice and restore the parameter's original state to avoid confusing the callers
241+
scheduledAction.setExecutionCount(previousExecutionCount);
238242
return executionCount;
239243
}
240244
}

app/src/test/java/org/gnucash/android/test/unit/service/ScheduledActionServiceTest.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,6 @@ public void recurringTransactions_shouldHaveScheduledActionUID(){
290290
* was done on Monday and it's Thursday, two backups have been
291291
* missed. Doing the two missed backups plus today's wouldn't be
292292
* useful, so just one should be done.</p>
293-
*
294-
* <p><i>Note</i>: the execution count will include the missed runs
295-
* as computeNextCountBasedScheduledExecutionTime depends on it.</p>
296293
*/
297294
@Test
298295
public void scheduledBackups_shouldRunOnlyOnce(){
@@ -302,6 +299,7 @@ public void scheduledBackups_shouldRunOnlyOnce(){
302299
scheduledBackup.setRecurrence(PeriodType.MONTH, 1);
303300
scheduledBackup.setExecutionCount(2);
304301
scheduledBackup.setLastRun(LocalDateTime.now().minusMonths(2).toDate().getTime());
302+
long previousLastRun = scheduledBackup.getLastRunTime();
305303

306304
ExportParams backupParams = new ExportParams(ExportFormat.XML);
307305
backupParams.setExportTarget(ExportParams.ExportTarget.SD_CARD);
@@ -317,13 +315,16 @@ public void scheduledBackups_shouldRunOnlyOnce(){
317315
// Check there's not a backup for each missed run
318316
ScheduledActionService.processScheduledActions(actions, mDb);
319317
assertThat(scheduledBackup.getExecutionCount()).isEqualTo(3);
318+
assertThat(scheduledBackup.getLastRunTime()).isGreaterThan(previousLastRun);
320319
File[] backupFiles = backupFolder.listFiles();
321320
assertThat(backupFiles).hasSize(1);
322321
assertThat(backupFiles[0]).exists().hasExtension("gnca");
323322

324323
// Check also across service runs
324+
previousLastRun = scheduledBackup.getLastRunTime();
325325
ScheduledActionService.processScheduledActions(actions, mDb);
326326
assertThat(scheduledBackup.getExecutionCount()).isEqualTo(3);
327+
assertThat(scheduledBackup.getLastRunTime()).isEqualTo(previousLastRun);
327328
backupFiles = backupFolder.listFiles();
328329
assertThat(backupFiles).hasSize(1);
329330
assertThat(backupFiles[0]).exists().hasExtension("gnca");
@@ -340,6 +341,7 @@ public void scheduledBackups_shouldNotRunBeforeNextScheduledExecution(){
340341
ScheduledAction scheduledBackup = new ScheduledAction(ScheduledAction.ActionType.BACKUP);
341342
scheduledBackup.setStartTime(LocalDateTime.now().minusDays(2).toDate().getTime());
342343
scheduledBackup.setLastRun(scheduledBackup.getStartTime());
344+
long previousLastRun = scheduledBackup.getLastRunTime();
343345
scheduledBackup.setExecutionCount(1);
344346
scheduledBackup.setRecurrence(PeriodType.WEEK, 1);
345347

@@ -357,6 +359,7 @@ public void scheduledBackups_shouldNotRunBeforeNextScheduledExecution(){
357359
ScheduledActionService.processScheduledActions(actions, mDb);
358360

359361
assertThat(scheduledBackup.getExecutionCount()).isEqualTo(1);
362+
assertThat(scheduledBackup.getLastRunTime()).isEqualTo(previousLastRun);
360363
assertThat(backupFolder.listFiles()).hasSize(0);
361364
}
362365

0 commit comments

Comments
 (0)