增加同步锁
This commit is contained in:
parent
35fb294879
commit
e71711b4a0
@ -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) {
|
||||||
|
// 尝试获取锁(只使用 externalSystemId 作为锁参数)
|
||||||
|
if (!syncLockManager.tryLock(externalSystemId)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
doSyncJobs(externalSystemId, null);
|
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) {
|
||||||
|
// 尝试获取锁(使用 externalSystemId 和 viewId 作为锁参数)
|
||||||
|
if (!syncLockManager.tryLock(externalSystemId, viewId)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
doSyncJobs(externalSystemId, viewId);
|
doSyncJobs(externalSystemId, viewId);
|
||||||
|
} finally {
|
||||||
|
syncLockManager.unlock(externalSystemId, viewId);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -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);
|
||||||
|
|||||||
@ -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) {
|
||||||
|
// 尝试获取锁
|
||||||
|
if (!syncLockManager.tryLock(externalSystemId)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
doSyncBranches(externalSystemId, null, null);
|
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) {
|
||||||
|
// 尝试获取锁
|
||||||
|
if (!syncLockManager.tryLock(externalSystemId, repoGroupId)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
doSyncBranches(externalSystemId, repoGroupId, null);
|
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) {
|
||||||
|
// 尝试获取锁
|
||||||
|
if (!syncLockManager.tryLock(externalSystemId, repoGroupId, repoProjectId)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
doSyncBranches(externalSystemId, repoGroupId, repoProjectId);
|
doSyncBranches(externalSystemId, repoGroupId, repoProjectId);
|
||||||
|
} finally {
|
||||||
|
syncLockManager.unlock(externalSystemId, repoGroupId, repoProjectId);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -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);
|
||||||
|
|
||||||
|
|||||||
@ -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) {
|
||||||
|
// 尝试获取锁
|
||||||
|
if (!syncLockManager.tryLock(externalSystemId)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
doSyncProjects(externalSystemId, null);
|
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) {
|
||||||
|
// 尝试获取锁
|
||||||
|
if (!syncLockManager.tryLock(externalSystemId, repoGroupId)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
doSyncProjects(externalSystemId, repoGroupId);
|
doSyncProjects(externalSystemId, repoGroupId);
|
||||||
|
} finally {
|
||||||
|
syncLockManager.unlock(externalSystemId, repoGroupId);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user