后台管理接口修复 后端BUG修复
This commit is contained in:
@@ -1,21 +1,38 @@
|
||||
package com.nanri.aiimage.config;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.net.InetAddress;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class InstanceMetadata {
|
||||
|
||||
private final String instanceId;
|
||||
private final String hostname;
|
||||
private final String source;
|
||||
private final boolean stable;
|
||||
|
||||
public InstanceMetadata(@Value("${aiimage.instance-id:}") String configuredInstanceId) {
|
||||
this.hostname = resolveHostname();
|
||||
this.instanceId = configuredInstanceId == null || configuredInstanceId.isBlank()
|
||||
? this.hostname
|
||||
: configuredInstanceId.trim();
|
||||
String normalizedConfiguredInstanceId = configuredInstanceId == null ? "" : configuredInstanceId.trim();
|
||||
if (!normalizedConfiguredInstanceId.isBlank()) {
|
||||
this.instanceId = normalizedConfiguredInstanceId;
|
||||
this.source = "configured";
|
||||
this.stable = true;
|
||||
log.info("[instance] resolved instanceId={} source={} hostname={} continuityRisk=LOW",
|
||||
this.instanceId, this.source, this.hostname);
|
||||
} else {
|
||||
this.instanceId = this.hostname;
|
||||
this.source = detectFallbackSource(this.hostname);
|
||||
this.stable = false;
|
||||
log.warn("[instance] aiimage.instance-id is not configured, falling back to {}={} as instanceId. "
|
||||
+ "This may change after restart/redeploy and break task owner continuity. "
|
||||
+ "Set AIIMAGE_INSTANCE_ID in 1Panel/Docker env, or set -Daiimage.instance-id=<stable-id> in IDEA/local startup.",
|
||||
this.source, this.hostname);
|
||||
}
|
||||
}
|
||||
|
||||
public String getInstanceId() {
|
||||
@@ -26,6 +43,14 @@ public class InstanceMetadata {
|
||||
return hostname;
|
||||
}
|
||||
|
||||
public String getSource() {
|
||||
return source;
|
||||
}
|
||||
|
||||
public boolean isStable() {
|
||||
return stable;
|
||||
}
|
||||
|
||||
private static String resolveHostname() {
|
||||
try {
|
||||
return InetAddress.getLocalHost().getHostName();
|
||||
@@ -34,4 +59,12 @@ public class InstanceMetadata {
|
||||
return (envHostname == null || envHostname.isBlank()) ? "unknown-host" : envHostname;
|
||||
}
|
||||
}
|
||||
|
||||
private static String detectFallbackSource(String hostname) {
|
||||
String envHostname = System.getenv("HOSTNAME");
|
||||
if (envHostname != null && !envHostname.isBlank() && envHostname.equalsIgnoreCase(hostname)) {
|
||||
return "env.HOSTNAME";
|
||||
}
|
||||
return "os.hostname";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,14 +43,18 @@ public class RequestTraceFilter extends OncePerRequestFilter {
|
||||
|
||||
response.setHeader("X-AIIMAGE-Instance", instanceMetadata.getInstanceId());
|
||||
response.setHeader("X-AIIMAGE-Host", instanceMetadata.getHostname());
|
||||
response.setHeader("X-AIIMAGE-Instance-Source", instanceMetadata.getSource());
|
||||
response.setHeader("X-AIIMAGE-Instance-Stable", String.valueOf(instanceMetadata.isStable()));
|
||||
|
||||
try {
|
||||
filterChain.doFilter(request, response);
|
||||
} finally {
|
||||
long costMs = System.currentTimeMillis() - start;
|
||||
log.info(
|
||||
"request-trace instance={} host={} method={} uri={} status={} remote={} forwardedHost={} forwardedProto={} forwardedPort={} requestId={} userAgent={} costMs={}",
|
||||
"request-trace instance={} source={} stable={} host={} method={} uri={} status={} remote={} forwardedHost={} forwardedProto={} forwardedPort={} requestId={} userAgent={} costMs={}",
|
||||
instanceMetadata.getInstanceId(),
|
||||
instanceMetadata.getSource(),
|
||||
instanceMetadata.isStable(),
|
||||
instanceMetadata.getHostname(),
|
||||
request.getMethod(),
|
||||
request.getRequestURI(),
|
||||
|
||||
@@ -3,6 +3,8 @@ package com.nanri.aiimage.config;
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
import java.nio.file.Path;
|
||||
|
||||
@Data
|
||||
@ConfigurationProperties(prefix = "aiimage.storage")
|
||||
public class StorageProperties {
|
||||
@@ -12,4 +14,25 @@ public class StorageProperties {
|
||||
private long sourceRetentionHours = 24;
|
||||
private long resultRetentionHours = 24;
|
||||
private long transientPayloadRetentionHours = 72;
|
||||
|
||||
public String getLocalTempDir() {
|
||||
String configured = localTempDir == null ? "" : localTempDir.trim();
|
||||
if (configured.isBlank()) {
|
||||
configured = "./data/tmp";
|
||||
}
|
||||
try {
|
||||
Path raw = Path.of(configured);
|
||||
if (raw.isAbsolute()) {
|
||||
return raw.normalize().toString();
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
// Fall through to resolve against a stable base directory.
|
||||
}
|
||||
|
||||
String userDir = System.getProperty("user.dir");
|
||||
if (userDir == null || userDir.isBlank()) {
|
||||
userDir = System.getProperty("java.io.tmpdir", ".");
|
||||
}
|
||||
return Path.of(userDir).resolve(configured).normalize().toString();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user