增加同步锁

This commit is contained in:
dengqichen 2025-12-01 18:07:38 +08:00
parent 35fb294879
commit e71711b4a0
6 changed files with 112 additions and 8 deletions

View File

@ -66,6 +66,9 @@ public class JenkinsJobServiceImpl extends BaseServiceImpl<JenkinsJob, JenkinsJo
@Resource @Resource
private com.qqchen.deploy.backend.deploy.repository.IJenkinsBuildRepository jenkinsBuildRepository; private com.qqchen.deploy.backend.deploy.repository.IJenkinsBuildRepository jenkinsBuildRepository;
@Resource
private com.qqchen.deploy.backend.deploy.lock.SyncLockManager syncLockManager;
/** /**
* 同步外部系统下所有视图的Jenkins任务异步执行 * 同步外部系统下所有视图的Jenkins任务异步执行
* *
@ -75,7 +78,16 @@ public class JenkinsJobServiceImpl extends BaseServiceImpl<JenkinsJob, JenkinsJo
@Async @Async
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void syncJobs(Long externalSystemId) { public void syncJobs(Long externalSystemId) {
doSyncJobs(externalSystemId, null); // 尝试获取锁只使用 externalSystemId 作为锁参数
if (!syncLockManager.tryLock(externalSystemId)) {
return;
}
try {
doSyncJobs(externalSystemId, null);
} finally {
syncLockManager.unlock(externalSystemId);
}
} }
/** /**
@ -88,7 +100,16 @@ public class JenkinsJobServiceImpl extends BaseServiceImpl<JenkinsJob, JenkinsJo
@Async @Async
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void syncJobs(Long externalSystemId, Long viewId) { public void syncJobs(Long externalSystemId, Long viewId) {
doSyncJobs(externalSystemId, viewId); // 尝试获取锁使用 externalSystemId viewId 作为锁参数
if (!syncLockManager.tryLock(externalSystemId, viewId)) {
return;
}
try {
doSyncJobs(externalSystemId, viewId);
} finally {
syncLockManager.unlock(externalSystemId, viewId);
}
} }
/** /**

View File

@ -60,6 +60,9 @@ public class JenkinsViewServiceImpl extends BaseServiceImpl<JenkinsView, Jenkins
@Resource @Resource
private IJenkinsSyncHistoryService jenkinsSyncHistoryService; private IJenkinsSyncHistoryService jenkinsSyncHistoryService;
@Resource
private com.qqchen.deploy.backend.deploy.lock.SyncLockManager syncLockManager;
/** /**
* 同步Jenkins视图异步执行 * 同步Jenkins视图异步执行
* *
@ -69,6 +72,19 @@ public class JenkinsViewServiceImpl extends BaseServiceImpl<JenkinsView, Jenkins
@Async @Async
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void syncViews(Long externalSystemId) { public void syncViews(Long externalSystemId) {
// 尝试获取锁
if (!syncLockManager.tryLock(externalSystemId)) {
return;
}
try {
doSyncViews(externalSystemId);
} finally {
syncLockManager.unlock(externalSystemId);
}
}
private void doSyncViews(Long externalSystemId) {
// 1. 创建同步历史记录 // 1. 创建同步历史记录
JenkinsSyncHistoryDTO syncHistory = new JenkinsSyncHistoryDTO(); JenkinsSyncHistoryDTO syncHistory = new JenkinsSyncHistoryDTO();
syncHistory.setExternalSystemId(externalSystemId); syncHistory.setExternalSystemId(externalSystemId);

View File

@ -66,6 +66,9 @@ public class RepositoryBranchServiceImpl extends BaseServiceImpl<RepositoryBranc
@Resource(name = "repositoryBranchExecutor") @Resource(name = "repositoryBranchExecutor")
private ThreadPoolTaskExecutor executor; private ThreadPoolTaskExecutor executor;
@Resource
private com.qqchen.deploy.backend.deploy.lock.SyncLockManager syncLockManager;
private static final int BATCH_SIZE = 50; // 减小批次大小提高容错性 private static final int BATCH_SIZE = 50; // 减小批次大小提高容错性
private static final int MAX_RETRIES = 3; private static final int MAX_RETRIES = 3;
@ -193,21 +196,48 @@ public class RepositoryBranchServiceImpl extends BaseServiceImpl<RepositoryBranc
@Async @Async
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void syncBranches(Long externalSystemId) { public void syncBranches(Long externalSystemId) {
doSyncBranches(externalSystemId, null, null); // 尝试获取锁
if (!syncLockManager.tryLock(externalSystemId)) {
return;
}
try {
doSyncBranches(externalSystemId, null, null);
} finally {
syncLockManager.unlock(externalSystemId);
}
} }
@Override @Override
@Async @Async
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void syncBranches(Long externalSystemId, Long repoGroupId) { public void syncBranches(Long externalSystemId, Long repoGroupId) {
doSyncBranches(externalSystemId, repoGroupId, null); // 尝试获取锁
if (!syncLockManager.tryLock(externalSystemId, repoGroupId)) {
return;
}
try {
doSyncBranches(externalSystemId, repoGroupId, null);
} finally {
syncLockManager.unlock(externalSystemId, repoGroupId);
}
} }
@Override @Override
@Async @Async
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void syncBranches(Long externalSystemId, Long repoGroupId, Long repoProjectId) { public void syncBranches(Long externalSystemId, Long repoGroupId, Long repoProjectId) {
doSyncBranches(externalSystemId, repoGroupId, repoProjectId); // 尝试获取锁
if (!syncLockManager.tryLock(externalSystemId, repoGroupId, repoProjectId)) {
return;
}
try {
doSyncBranches(externalSystemId, repoGroupId, repoProjectId);
} finally {
syncLockManager.unlock(externalSystemId, repoGroupId, repoProjectId);
}
} }
/** /**

View File

@ -60,10 +60,26 @@ public class RepositoryGroupServiceImpl extends BaseServiceImpl<RepositoryGroup,
@Resource @Resource
private IRepositorySyncHistoryService repositorySyncHistoryService; private IRepositorySyncHistoryService repositorySyncHistoryService;
@Resource
private com.qqchen.deploy.backend.deploy.lock.SyncLockManager syncLockManager;
@Override @Override
@Async @Async
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void syncGroups(Long externalSystemId) { public void syncGroups(Long externalSystemId) {
// 尝试获取锁
if (!syncLockManager.tryLock(externalSystemId)) {
return;
}
try {
doSyncGroups(externalSystemId);
} finally {
syncLockManager.unlock(externalSystemId);
}
}
private void doSyncGroups(Long externalSystemId) {
// 1. 创建同步历史记录 // 1. 创建同步历史记录
RepositorySyncHistoryDTO groupHistory = repositorySyncHistoryService.createSyncHistory(externalSystemId, RepositorySyncType.GROUP); RepositorySyncHistoryDTO groupHistory = repositorySyncHistoryService.createSyncHistory(externalSystemId, RepositorySyncType.GROUP);

View File

@ -76,6 +76,9 @@ public class RepositoryProjectServiceImpl extends BaseServiceImpl<RepositoryProj
@Resource @Resource
private IRepositorySyncHistoryService repositorySyncHistoryService; private IRepositorySyncHistoryService repositorySyncHistoryService;
@Resource
private com.qqchen.deploy.backend.deploy.lock.SyncLockManager syncLockManager;
private static final int BATCH_SIZE = 100; private static final int BATCH_SIZE = 100;
private static final int MAX_RETRIES = 3; private static final int MAX_RETRIES = 3;
private static final long RETRY_DELAY = 100L; private static final long RETRY_DELAY = 100L;
@ -111,14 +114,32 @@ public class RepositoryProjectServiceImpl extends BaseServiceImpl<RepositoryProj
@Async @Async
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void syncProjects(Long externalSystemId) { public void syncProjects(Long externalSystemId) {
doSyncProjects(externalSystemId, null); // 尝试获取锁
if (!syncLockManager.tryLock(externalSystemId)) {
return;
}
try {
doSyncProjects(externalSystemId, null);
} finally {
syncLockManager.unlock(externalSystemId);
}
} }
@Override @Override
@Async @Async
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void syncProjects(Long externalSystemId, Long repoGroupId) { public void syncProjects(Long externalSystemId, Long repoGroupId) {
doSyncProjects(externalSystemId, repoGroupId); // 尝试获取锁
if (!syncLockManager.tryLock(externalSystemId, repoGroupId)) {
return;
}
try {
doSyncProjects(externalSystemId, repoGroupId);
} finally {
syncLockManager.unlock(externalSystemId, repoGroupId);
}
} }
/** /**