package com.muyu.cloud.vehicle.gateway.aliyun.ecs;


import com.muyu.cloud.vehicle.gateway.aliyun.service.AliYunEcsService;
import com.muyu.cloud.vehicle.gateway.config.AliProperties;
import com.muyu.cloud.vehicle.gateway.domain.AliInstance;
import com.muyu.common.redis.service.RedisService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 创建实例
 */

@Log4j2
@Component
public class Sample implements ApplicationRunner{

    @Autowired
    private AliYunEcsService aliYunEcsService;

    @Autowired
    private AliProperties aliProperties;


    @Override
    public void run(ApplicationArguments args) throws Exception {
        List<String> list;
        try{
            log.info("开始创建实例");
            list =aliYunEcsService.generateInstance(aliProperties.getAmount());
        } catch (Exception e) {
            log.info("创建实例失败");
            throw new RuntimeException(e);
        }
        log.info("创建实例成功");
//        redisService.setCacheList("instanceIds",list);
        try{
            Thread.sleep(9000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        List<AliInstance> aliInstances = aliYunEcsService.selectInstance(list);
        log.info("查询实例信息成功：{}",aliInstances);
        //将查询到的实例信息列表存储到redis中
//        redisService.setCacheList("instanceList",aliInstances);
//        log.info("redis存储成功：{}",aliInstances);
    }
}
