Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cleanup demo account every 24h (#1014)
- Loading branch information
Showing
4 changed files
with
78 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/main/java/com/commafeed/backend/task/DemoAccountCleanupTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.commafeed.backend.task; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Singleton; | ||
|
||
import org.hibernate.SessionFactory; | ||
|
||
import com.commafeed.CommaFeedApplication; | ||
import com.commafeed.CommaFeedConfiguration; | ||
import com.commafeed.backend.dao.UnitOfWork; | ||
import com.commafeed.backend.dao.UserDAO; | ||
import com.commafeed.backend.model.User; | ||
import com.commafeed.backend.service.UserService; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@RequiredArgsConstructor(onConstructor = @__({ @Inject })) | ||
@Singleton | ||
@Slf4j | ||
public class DemoAccountCleanupTask extends ScheduledTask { | ||
|
||
private final CommaFeedConfiguration config; | ||
private final SessionFactory sessionFactory; | ||
private final UserDAO userDAO; | ||
private final UserService userService; | ||
|
||
@Override | ||
protected void run() { | ||
if (!config.getApplicationSettings().getCreateDemoAccount()) { | ||
return; | ||
} | ||
|
||
log.info("recreating demo user account"); | ||
UnitOfWork.run(sessionFactory, () -> { | ||
User demoUser = userDAO.findByName(CommaFeedApplication.USERNAME_DEMO); | ||
if (demoUser == null) { | ||
return; | ||
} | ||
|
||
userService.unregister(demoUser); | ||
userService.createDemoUser(); | ||
}); | ||
|
||
} | ||
|
||
@Override | ||
protected long getInitialDelay() { | ||
return 1; | ||
} | ||
|
||
@Override | ||
protected long getPeriod() { | ||
return getTimeUnit().convert(24, TimeUnit.HOURS); | ||
} | ||
|
||
@Override | ||
protected TimeUnit getTimeUnit() { | ||
return TimeUnit.MINUTES; | ||
} | ||
|
||
} |