package cn.reghao.autodop.dmaster.app.model.po;

import cn.reghao.autodop.common.msg.rpc.dto.app.AppStatus;
import cn.reghao.autodop.dmaster.app.model.po.config.AppConfig;
import cn.reghao.autodop.dmaster.app.model.po.config.DeployConfig;
import cn.reghao.autodop.dmaster.app.model.po.log.BuildLog;
import lombok.*;

import javax.persistence.Entity;
import java.time.LocalDateTime;

/**
 * 正在运行的应用
 *
 * @author reghao
 * @date 2021-05-31 13:36:46
 */
@NoArgsConstructor
@Getter
@Setter
@Entity
public class AppRunning extends AppProperty {
    private Integer httpPort;
    private String healthCheck;
    private String buildLogId;
    private String packagePath;
    private Boolean isRunning;
    private LocalDateTime startTime;
    private Integer pid;
    private LocalDateTime lastCheck;

    public AppRunning(AppConfig app, DeployConfig deployConfig) {
        super(app, deployConfig);
        this.httpPort = app.getRunningConfig().getHttpPort();
        this.healthCheck = app.getRunningConfig().getHealthCheck();
    }

    public AppRunning update(AppConfig app, String machineIpv4) {
        this.update0(app.getAppName(), machineIpv4);
        this.httpPort = app.getRunningConfig().getHttpPort();
        this.healthCheck = app.getRunningConfig().getHealthCheck();
        return this;
    }

    public AppRunning update(BuildLog buildLog, AppStatus appStatus) {
        this.setBuildLogId(buildLog.getId());
        this.setPackagePath(buildLog.getPackagePath());
        this.setIsRunning(appStatus.getIsRunning());
        this.setStartTime(appStatus.getStartTime());
        this.setPid(appStatus.getPid());
        this.setLastCheck(LocalDateTime.now());
        return this;
    }

    public AppRunning update(AppStatus appStatus) {
        this.setPackagePath(appStatus.getPackagePath());
        this.setIsRunning(appStatus.getIsRunning());
        this.setStartTime(appStatus.getStartTime());
        this.setPid(appStatus.getPid());
        this.setLastCheck(LocalDateTime.now());
        return this;
    }
}
