/*
 * Copyright 2017 flow.ci
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.flow.platform.api.consumer;

import com.flow.platform.api.domain.CmdCallbackQueueItem;
import com.flow.platform.api.service.job.JobService;
import com.flow.platform.core.exception.NotFoundException;
import com.flow.platform.core.queue.PlatformQueue;
import com.flow.platform.core.queue.QueueListener;
import com.flow.platform.util.Logger;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author yh@firim
 */
@Component
public class CmdCallbackQueueConsumer implements QueueListener<CmdCallbackQueueItem> {

    private final static Logger LOGGER = new Logger(CmdCallbackQueueConsumer.class);

    @Autowired
    private PlatformQueue<CmdCallbackQueueItem> cmdCallbackQueue;

    @Autowired
    private JobService jobService;

    @PostConstruct
    public void init() {
        cmdCallbackQueue.register(this);
    }

    @Override
    public void onQueueItem(CmdCallbackQueueItem item) {
        if (item == null) {
            return;
        }
        try {
            jobService.callback(item);
        } catch (NotFoundException notFoundException) {
            // wait 1s re queue
            try {
                Thread.sleep(1000);
            } catch (Throwable throwable) {
            }

            jobService.enterQueue(item);

        } catch (Throwable throwable) {
            LOGGER.traceMarker("onQueueItem", String.format("exception - %s", throwable));
        }
    }
}

@@ -1759,24 +1759,25 @@
 xception) %7B%0A
+%0A
             
@@ -1783,320 +1783,478 @@
  // 
-wait 1s re queue%0A            try %7B%0A                Thread.sleep(1000);%0A            %7D catch (Throwable throwable) %7B%0A            %7D%0A%0A            jobService.enterQueue(item);%0A%0A        %7D catch (Throwable throwable) %7B%0A            LOGGER.traceMarker(%22onQueueItem%22, String.format(%22exception - %25s%22, throwable));%0A        %7D
+re-enqueue cmd callback if job not found since transaction problem%0A            reEnqueueJobCallback(item, 1000);%0A%0A        %7D catch (Throwable throwable) %7B%0A            LOGGER.traceMarker(%22onQueueItem%22, String.format(%22exception - %25s%22, throwable));%0A        %7D%0A    %7D%0A%0A    private void reEnqueueJobCallback(CmdCallbackQueueItem item, long wait) %7B%0A        try %7B%0A            Thread.sleep(wait);%0A        %7D catch (Throwable ignore) %7B%0A        %7D%0A%0A        jobService.enterQueue(item);
 %0A   

optimize code structure