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

Database lock during queue #62

Open
SeungpilPark opened this issue Jul 27, 2017 · 0 comments
Open

Database lock during queue #62

SeungpilPark opened this issue Jul 27, 2017 · 0 comments
Labels

Comments

@SeungpilPark
Copy link
Member

SeungpilPark commented Jul 27, 2017

다음의 OneTimeBuy 의 인보이스 발급 및 서브스크립션 합산 큐 클래스에 DB Lock 로직을 적용해야 한다.

멀티 인스턴스상황에서 row 간의 중복처리를 막기 위함.

package org.uengine.garuda.backend.scheduler.jobs;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.uengine.garuda.common.exception.ServiceException;
import org.uengine.garuda.killbill.KBServiceFactory;
import org.uengine.garuda.killbill.api.model.Clock;
import org.uengine.garuda.model.OneTimeBuy;
import org.uengine.garuda.model.Organization;
import org.uengine.garuda.util.ApplicationContextRegistry;
import org.uengine.garuda.util.UUIDUtils;
import org.uengine.garuda.web.onetimebuy.OneTimeBuyService;
import org.uengine.garuda.web.onetimebuy.OneTimeBuyState;
import org.uengine.garuda.web.organization.OrganizationRepository;

import java.util.*;

/**
 * ClientJob 의 Stopping Job 을 확인해서 종료시키는 작업.
 *
 * @author Seungpil, Park
 * @since 2.0
 */
public class OneTimeBuyJob extends QuartzJobBean {
    /**
     * SLF4J Logging
     */
    private Logger logger = LoggerFactory.getLogger(OneTimeBuyJob.class);

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        try {
            //TODO DB 락을 구현해야 한다.

            logger.debug("Now check the oneTimeBuy jobs to invoice.");
            ApplicationContext applicationContext = ApplicationContextRegistry.getApplicationContext();
            OneTimeBuyService oneTimeBuyService = applicationContext.getBean(OneTimeBuyService.class);
            OrganizationRepository organizationRepository = applicationContext.getBean(OrganizationRepository.class);
            KBServiceFactory kbServiceFactory = applicationContext.getBean(KBServiceFactory.class);

            //여기서 조직을 하나 선으로 얻는게 필요.
            Organization organization = organizationRepository.selectOneForTest();
            if (organization == null) {
                return;
            }

            //조직을 사용하여 clock api 호출
            Clock clock = kbServiceFactory
                    .apiClient(organization.getTenant_api_key(), organization.getTenant_api_secret())
                    .testApi().getTime();

            //oneTimeBuyList 획득
            List<OneTimeBuy> oneTimeBuyList = oneTimeBuyService.selectBcdPendingBuys(clock.getCurrentUtcTime());

            //oneTimeBuyList 를 organization 별로 나눈다.
            Map<String, List<OneTimeBuy>> oneTimeBuyOrgMap = new HashMap<String, List<OneTimeBuy>>();
            for (OneTimeBuy oneTimeBuy : oneTimeBuyList) {
                String organization_id = oneTimeBuy.getOrganization_id();
                if (!oneTimeBuyOrgMap.containsKey(organization_id)) {
                    oneTimeBuyOrgMap.put(organization_id, new ArrayList<OneTimeBuy>());
                }
                oneTimeBuyOrgMap.get(organization_id).add(oneTimeBuy);
            }

            //조직 별로 charge invoice 를 실행한다.
            for (Map.Entry<String, List<OneTimeBuy>> entry : oneTimeBuyOrgMap.entrySet()) {
                String organization_id = entry.getKey();
                List<OneTimeBuy> oneTimeBuys = entry.getValue();
                Organization org = organizationRepository.selectById(organization_id);
                if (org != null) {
                    oneTimeBuyService.sendOneTimeBuyImmediately(org, clock, oneTimeBuys);
                }
            }
            logger.debug("{} one time buy items invoiced.", oneTimeBuyList.size());


        } catch (Exception ex) {
            throw new ServiceException("Unable to run scheduled jobs", ex);
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant