Skip to content

Commit

Permalink
Run scheduled exports according to their recurrence.
Browse files Browse the repository at this point in the history
They were always executed twice a day.

Fixes codinguser#583
  • Loading branch information
rivaldi8 committed Sep 16, 2016
1 parent dae1caf commit 54cfecd
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ private static int executeBackup(ScheduledAction scheduledAction, SQLiteDatabase
if (endTime > 0 && endTime < now)
return executionCount;

if (scheduledAction.computeNextScheduledExecutionTime() > now)
return 0;

ExportParams params = ExportParams.parseCsv(scheduledAction.getTag());
try {
//wait for async task to finish before we proceed (we are holding a wake lock)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.gnucash.android.test.unit.testutil.ShadowCrashlytics;
import org.gnucash.android.test.unit.testutil.ShadowUserVoice;
import org.joda.time.DateTime;
import org.joda.time.LocalDateTime;
import org.joda.time.Weeks;
import org.junit.After;
import org.junit.Before;
Expand Down Expand Up @@ -304,6 +305,36 @@ public void scheduledBackups_shouldRunOnlyOnce(){
assertThat(backupFiles[0]).exists().hasExtension("gnca");
}

/**
* Tests that a scheduled backup isn't executed before the next scheduled
* execution according to its recurrence.
*
* <p>Tests for bug https://github.com/codinguser/gnucash-android/issues/583</p>
*/
@Test
public void scheduledBackups_shouldNotRunBeforeNextScheduledExecution(){
ScheduledAction scheduledBackup = new ScheduledAction(ScheduledAction.ActionType.BACKUP);
scheduledBackup.setStartTime(LocalDateTime.now().minusDays(2).toDate().getTime());
scheduledBackup.setExecutionCount(1);
scheduledBackup.setRecurrence(PeriodType.WEEK, 1);

ExportParams backupParams = new ExportParams(ExportFormat.XML);
backupParams.setExportTarget(ExportParams.ExportTarget.SD_CARD);
scheduledBackup.setTag(backupParams.toCsv());

File backupFolder = new File(
Exporter.getExportFolderPath(BooksDbAdapter.getInstance().getActiveBookUID()));
assertThat(backupFolder).exists();
assertThat(backupFolder.listFiles()).isEmpty();

List<ScheduledAction> actions = new ArrayList<>();
actions.add(scheduledBackup);
ScheduledActionService.processScheduledActions(actions, mDb);

assertThat(scheduledBackup.getExecutionCount()).isEqualTo(1);
assertThat(backupFolder.listFiles()).hasSize(0);
}

@After
public void tearDown(){
TransactionsDbAdapter.getInstance().deleteAllRecords();
Expand Down

0 comments on commit 54cfecd

Please sign in to comment.