已将业务时间生成入口统一为 Asia/Shanghai,不依赖服务器默认时区
This commit is contained in:
+4
-3
@@ -13,6 +13,7 @@ import com.linkerbot.vibemake.service.gitea.GiteaClient.RemoteRepository;
|
||||
import com.linkerbot.vibemake.service.mapper.ProjectMapper;
|
||||
import com.linkerbot.vibemake.service.oss.OssResourcePaths;
|
||||
import com.linkerbot.vibemake.service.support.ProjectInputs;
|
||||
import com.linkerbot.vibemake.service.support.ProjectSupport;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -116,7 +117,7 @@ public final class ImportCoordinator {
|
||||
e -> {
|
||||
if (e.reservedAt == null)
|
||||
e.reservedAt =
|
||||
LocalDateTime.now(Clock.systemUTC())
|
||||
ProjectSupport.now()
|
||||
.truncatedTo(ChronoUnit.MILLIS)
|
||||
.toString();
|
||||
e.phase = Phase.RESERVING;
|
||||
@@ -258,7 +259,7 @@ public final class ImportCoordinator {
|
||||
|
||||
project.setGiteaRepoId(remote.id());
|
||||
project.setRepoStatus("READY");
|
||||
project.setUpdatedAt(LocalDateTime.now(Clock.systemUTC()));
|
||||
project.setUpdatedAt(ProjectSupport.now());
|
||||
if (projects.updateById(project) != 1)
|
||||
throw new ImportFailure("DATABASE_UPDATE_FAILED", "项目仓库关联写入失败");
|
||||
store.phase(entry, Phase.LINKED);
|
||||
@@ -317,7 +318,7 @@ public final class ImportCoordinator {
|
||||
}
|
||||
if (owned && project != null && !"READY".equals(project.getRepoStatus())) {
|
||||
project.setRepoStatus(review ? "CREATING" : "FAILED");
|
||||
project.setUpdatedAt(LocalDateTime.now(Clock.systemUTC()));
|
||||
project.setUpdatedAt(ProjectSupport.now());
|
||||
try {
|
||||
projects.updateById(project);
|
||||
} catch (RuntimeException databaseFailure) {
|
||||
|
||||
@@ -69,6 +69,8 @@ public final class ImportModels {
|
||||
|
||||
public static final class Manifest {
|
||||
public int version = 1;
|
||||
// 旧清单未声明时区,其 reservedAt 按 UTC 迁移。
|
||||
public String timeZone;
|
||||
public String fingerprint;
|
||||
public String ownerUserCode;
|
||||
public String giteaBaseUrl;
|
||||
|
||||
@@ -3,16 +3,22 @@ package com.linkerbot.vibemake.importer;
|
||||
import static com.linkerbot.vibemake.importer.ImportModels.*;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.linkerbot.vibemake.service.support.ProjectSupport;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.nio.channels.FileLock;
|
||||
import java.nio.file.*;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneOffset;
|
||||
import java.util.*;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public final class StateStore implements AutoCloseable {
|
||||
private static final Logger log = LoggerFactory.getLogger(StateStore.class);
|
||||
private final Path directory;
|
||||
private final ObjectMapper json = new ObjectMapper().findAndRegisterModules();
|
||||
private final FileChannel channel;
|
||||
@@ -49,8 +55,10 @@ public final class StateStore implements AutoCloseable {
|
||||
.equals(manifest.entries.stream().map(e -> e.candidate).toList())
|
||||
|| !data.excluded().equals(manifest.excluded))
|
||||
throw new ImportFailure("BATCH_CHANGED", "Excel 或清单内容已变化,请使用新的状态目录");
|
||||
migrateTimeZone();
|
||||
} else {
|
||||
manifest = new Manifest();
|
||||
manifest.timeZone = ProjectSupport.TIME_ZONE.getId();
|
||||
manifest.fingerprint = data.fingerprint();
|
||||
manifest.excluded.addAll(data.excluded());
|
||||
data.candidates().forEach(c -> manifest.entries.add(new Entry(c)));
|
||||
@@ -63,6 +71,24 @@ public final class StateStore implements AutoCloseable {
|
||||
}
|
||||
}
|
||||
|
||||
private void migrateTimeZone() throws IOException {
|
||||
String targetZone = ProjectSupport.TIME_ZONE.getId();
|
||||
if (targetZone.equals(manifest.timeZone)) return;
|
||||
if (manifest.timeZone != null)
|
||||
throw new ImportFailure("BATCH_TIME_ZONE_MISMATCH", "导入清单时区不受支持,请人工核对");
|
||||
int converted = 0;
|
||||
for (Entry entry : manifest.entries) {
|
||||
if (entry.reservedAt == null) continue;
|
||||
entry.reservedAt = ProjectSupport.fromInstant(
|
||||
LocalDateTime.parse(entry.reservedAt).toInstant(ZoneOffset.UTC)).toString();
|
||||
converted++;
|
||||
}
|
||||
manifest.timeZone = targetZone;
|
||||
save();
|
||||
log.info("导入清单时间迁移完成 sourceTimeZone=UTC targetTimeZone={} convertedEntries={}",
|
||||
targetZone, converted);
|
||||
}
|
||||
|
||||
public synchronized void update(Entry entry, Consumer<Entry> change) throws IOException {
|
||||
change.accept(entry);
|
||||
save();
|
||||
@@ -90,6 +116,7 @@ public final class StateStore implements AutoCloseable {
|
||||
phase.name(), manifest.entries.stream().filter(e -> e.phase == phase).count());
|
||||
Map<String, Object> report = new LinkedHashMap<>();
|
||||
report.put("fingerprint", manifest.fingerprint);
|
||||
report.put("timeZone", manifest.timeZone);
|
||||
report.put("candidateCount", manifest.entries.size());
|
||||
report.put("excludedCount", manifest.excluded.size());
|
||||
report.put("counts", counts);
|
||||
|
||||
+2
-2
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.linkerbot.vibemake.service.config.OssProperties;
|
||||
import com.linkerbot.vibemake.service.oss.OssResourcePaths;
|
||||
import com.linkerbot.vibemake.service.support.ProjectSupport;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
@@ -11,7 +12,6 @@ import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.nio.file.attribute.PosixFilePermissions;
|
||||
import java.time.Instant;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
@@ -100,7 +100,7 @@ public final class CoverMigrationApplication {
|
||||
long unique = candidates.stream().map(candidate -> candidate.originalUrl().trim()).distinct().count();
|
||||
System.out.printf("封面迁移预览 environmentRoot=%s candidateProjects=%d uniqueUrls=%d execute=%s%n",
|
||||
oss.getRootDirectory(), candidates.size(), unique, options.execute());
|
||||
Path run = options.reportDirectory().resolve(Instant.now().toString().replace(':', '-') + "-" + UUID.randomUUID().toString().substring(0, 8));
|
||||
Path run = options.reportDirectory().resolve(ProjectSupport.now().toString().replace(':', '-') + "-" + UUID.randomUUID().toString().substring(0, 8));
|
||||
Files.createDirectories(run);
|
||||
restrict(run, "rwx------");
|
||||
ObjectMapper json = new ObjectMapper();
|
||||
|
||||
@@ -6,7 +6,7 @@ spring:
|
||||
init:
|
||||
mode: never
|
||||
datasource:
|
||||
url: ${DB_URL:jdbc:mysql://localhost:3306/vibemake?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC}
|
||||
url: ${DB_URL:jdbc:mysql://localhost:3306/vibemake?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai}
|
||||
username: ${DB_USERNAME:root}
|
||||
password: ${DB_PASSWORD:}
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
|
||||
+7
@@ -180,9 +180,16 @@ class ImportCoordinatorTest {
|
||||
|
||||
@Test
|
||||
void migrationBindsShortCodeAdaptsMasterAndPublishesWithoutEmptyRepository() throws Exception {
|
||||
Instant before = Instant.now().truncatedTo(java.time.temporal.ChronoUnit.MILLIS);
|
||||
worker(projects, service).run(entry);
|
||||
Instant after = Instant.now();
|
||||
assertThat(entry.phase).isEqualTo(Phase.PUBLISHED);
|
||||
Project project = service.published(entry.projectCode);
|
||||
ZoneId shanghai = ZoneId.of("Asia/Shanghai");
|
||||
assertThat(project.getCreatedAt().atZone(shanghai).toInstant()).isBetween(before, after);
|
||||
assertThat(project.getUpdatedAt().atZone(shanghai).toInstant()).isBetween(before, after);
|
||||
assertThat(project.getPublishedAt().atZone(shanghai).toInstant()).isBetween(before, after);
|
||||
assertThat(LocalDateTime.parse(entry.reservedAt)).isEqualTo(project.getCreatedAt());
|
||||
assertThat(project.getProjectCode()).isEqualTo(entry.projectCode).hasSize(12);
|
||||
assertThat(project.getGiteaRepoId()).isEqualTo(77L);
|
||||
assertThat(project.getPublishedCommitSha()).isEqualTo(SHA);
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
package com.linkerbot.vibemake.importer;
|
||||
|
||||
import static com.linkerbot.vibemake.importer.ImportModels.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
class StateStoreTest {
|
||||
@TempDir Path directory;
|
||||
private final ObjectMapper json = new ObjectMapper();
|
||||
private final Candidate candidate = new Candidate("sheet", 1, "项目", "https://github.com/example/robot",
|
||||
"简介", "GENERAL", null);
|
||||
private final WorkbookData data = new WorkbookData("fingerprint", List.of(candidate), List.of());
|
||||
|
||||
@Test
|
||||
void newStateAndReportDeclareShanghaiTimeZone() throws Exception {
|
||||
try (StateStore store = new StateStore(directory, data)) {
|
||||
assertThat(store.manifest.timeZone).isEqualTo("Asia/Shanghai");
|
||||
store.report();
|
||||
}
|
||||
assertThat(json.readTree(Files.readString(directory.resolve("report.json")))
|
||||
.path("timeZone").asText()).isEqualTo("Asia/Shanghai");
|
||||
}
|
||||
|
||||
@Test
|
||||
void legacyUtcReservationsAreMigratedAtomicallyAndOnlyOnce() throws Exception {
|
||||
Manifest legacy = new Manifest();
|
||||
legacy.fingerprint = data.fingerprint();
|
||||
Entry reserved = new Entry(candidate);
|
||||
reserved.reservedAt = "2026-12-31T16:00:00.123";
|
||||
legacy.entries.add(reserved);
|
||||
json.writeValue(directory.resolve("manifest.json").toFile(), legacy);
|
||||
|
||||
try (StateStore store = new StateStore(directory, data)) {
|
||||
assertThat(store.manifest.entries.getFirst().reservedAt).isEqualTo("2027-01-01T00:00:00.123");
|
||||
assertThat(store.manifest.timeZone).isEqualTo("Asia/Shanghai");
|
||||
}
|
||||
try (StateStore reopened = new StateStore(directory, data)) {
|
||||
assertThat(reopened.manifest.entries.getFirst().reservedAt).isEqualTo("2027-01-01T00:00:00.123");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void unsupportedStateTimeZonesFailBeforeChangingProgress() throws Exception {
|
||||
try (StateStore store = new StateStore(directory, data)) {
|
||||
store.manifest.timeZone = "America/New_York";
|
||||
store.save();
|
||||
}
|
||||
String before = Files.readString(directory.resolve("manifest.json"));
|
||||
assertThatThrownBy(() -> new StateStore(directory, data))
|
||||
.isInstanceOfSatisfying(ImportFailure.class,
|
||||
error -> assertThat(error.code()).isEqualTo("BATCH_TIME_ZONE_MISMATCH"));
|
||||
assertThat(Files.readString(directory.resolve("manifest.json"))).isEqualTo(before);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user