Skip to content

Commit

Permalink
Merge pull request #371 from daedongbread/345-feature-opensearch-도입-및…
Browse files Browse the repository at this point in the history
…-데이터-처리-프로그램-구현

feat: Divide profiles of schedulers for test
  • Loading branch information
chris910512 authored Nov 24, 2023
2 parents b2230bc + 28de897 commit 6bc4999
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

import javax.annotation.PostConstruct;

import com.depromeet.breadmapbackend.global.security.domain.RoleType;
import com.depromeet.breadmapbackend.global.security.token.JwtToken;
import com.depromeet.breadmapbackend.global.security.token.JwtTokenProvider;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
Expand All @@ -19,7 +23,10 @@
@ConfigurationPropertiesScan
@SpringBootApplication
@EnableScheduling
//@RequiredArgsConstructor
public class BreadMapBackendApplication {

// private final JwtTokenProvider jwtTokenProvider;
@PostConstruct
public void started() {
TimeZone.setDefault(TimeZone.getTimeZone("Asia/Seoul"));
Expand All @@ -33,4 +40,13 @@ public static void main(String[] args) {
public PasswordEncoder passwordEncoder() {
return PasswordEncoderFactories.createDelegatingPasswordEncoder();
}

// @PostConstruct
// public PasswordEncoder passwordEncoder() {
// JwtToken adminUserForEventPost = jwtTokenProvider.createJwtToken("ADMIN_USER_FOR_EVENT_POST", RoleType.USER.getCode());
// System.out.println("passwordEncoder :: ================" + adminUserForEventPost.getAccessToken());
//
// return PasswordEncoderFactories.createDelegatingPasswordEncoder();
//
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.context.annotation.Profile;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

Expand All @@ -14,11 +15,11 @@

@Slf4j
@Component
@Profile("prod")
@RequiredArgsConstructor
public class OpenSearchLoadScheduler {
public class OpenSearchLoadProdScheduler {

private final OpenSearchService openSearchService;
// @Scheduled(cron = "0 0/5 * * * *") // for test
@Scheduled(cron = "0 0 0 1,15 * *")
public void loadEntireData() throws IOException {
RedissonClient client = Redisson.create();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.depromeet.breadmapbackend.domain.search;

import com.depromeet.breadmapbackend.domain.search.dto.OpenSearchIndex;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.context.annotation.Profile;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component
@Profile("!prod")
@RequiredArgsConstructor
public class OpenSearchLoadTestScheduler {

private final OpenSearchService openSearchService;
@Scheduled(cron = "0 0 0/6 * * *") // for local test
public void loadEntireData() throws IOException {
RedissonClient client = Redisson.create();

RLock lock = client.getLock("Load-Entire-Data");
try {

if (lock.tryLock(2, TimeUnit.HOURS)) {
log.info("========================= Loading entire data to search engine =========================");

openSearchService.deleteAndCreateIndex(OpenSearchIndex.BAKERY_SEARCH.getIndexNameWithVersion());
openSearchService.deleteAndCreateIndex(OpenSearchIndex.BREAD_SEARCH.getIndexNameWithVersion());

openSearchService.loadEntireData();
log.info("Job loadEntireData executed by this instance");
} else {
log.info("Job loadEntireData skipped by this instance");
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} finally {
lock.unlock();
}

}
}

0 comments on commit 6bc4999

Please sign in to comment.