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

fix: duplicate cron task #835

Merged
merged 1 commit into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [unreleased]

## [6.0.14] - 2023-10-12

- Fixes issue with duplicate cron task

## [6.0.13] - 2023-09-15

- Fixes paid stats reporting for multitenancy
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ compileTestJava { options.encoding = "UTF-8" }
// }
//}

version = "6.0.13"
version = "6.0.14"


repositories {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/supertokens/cronjobs/Cronjobs.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ public static void addCronjob(Main main, CronTask task) {
}
Cronjobs instance = getInstance(main);
synchronized (instance.lock) {
instance.executor.scheduleWithFixedDelay(task, task.getInitialWaitTimeSeconds(),
task.getIntervalTimeSeconds(), TimeUnit.SECONDS);
if (!instance.tasks.contains(task)) {
instance.executor.scheduleWithFixedDelay(task, task.getInitialWaitTimeSeconds(),
task.getIntervalTimeSeconds(), TimeUnit.SECONDS);
instance.tasks.add(task);
}
}
Expand Down
63 changes: 63 additions & 0 deletions src/test/java/io/supertokens/test/CronjobTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.reflections.Reflections;

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

import static org.junit.Assert.*;

Expand Down Expand Up @@ -308,6 +309,49 @@ protected void doTaskPerStorage(Storage storage) throws Exception {
}
}

static class CounterCronJob extends CronTask {
private static final String RESOURCE_ID = "io.supertokens.test.CronjobTest.CounterCronJob";
private static AtomicInteger count = new AtomicInteger();

private CounterCronJob(Main main, List<List<TenantIdentifier>> tenantsInfo) {
super("CounterCronJob", main, tenantsInfo, false);
}

public static CounterCronJob getInstance(Main main) {
try {
return (CounterCronJob) main.getResourceDistributor()
.getResource(new TenantIdentifier(null, null, null), RESOURCE_ID);
} catch (TenantOrAppNotFoundException e) {
List<TenantIdentifier> tenants = new ArrayList<>();
tenants.add(new TenantIdentifier(null, null, null));
List<List<TenantIdentifier>> finalList = new ArrayList<>();
finalList.add(tenants);
return (CounterCronJob) main.getResourceDistributor()
.setResource(new TenantIdentifier(null, null, null), RESOURCE_ID,
new CounterCronJob(main, finalList));
}
}

@Override
public int getIntervalTimeSeconds() {
return 1;
}

@Override
public int getInitialWaitTimeSeconds() {
return 0;
}

@Override
protected void doTaskPerStorage(Storage storage) throws Exception {
count.incrementAndGet();
}

public int getCount() {
return count.get();
}
}

@Rule
public TestRule watchman = Utils.getOnFailure();

Expand Down Expand Up @@ -780,6 +824,25 @@ public void testThatCoreAutomaticallySyncsToConfigChangesInDb() throws Exception
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED));
}

@Test
public void testThatReAddingSameCronTaskDoesNotScheduleMoreExecutors() throws Exception {
String[] args = {"../"};

TestingProcessManager.TestingProcess process = TestingProcessManager.start(args);
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));

for (int i=0; i<10; i++) {
Cronjobs.addCronjob(process.getProcess(), CounterCronJob.getInstance(process.getProcess()));
Thread.sleep(50);
}

Thread.sleep(5000);
assertTrue(CounterCronJob.getInstance(process.getProcess()).getCount() > 3 && CounterCronJob.getInstance(process.getProcess()).getCount() < 8);

process.kill();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED));
}

@Test
public void testThatNoCronJobIntervalIsMoreThanADay() throws Exception {
String[] args = {"../"};
Expand Down
Loading