Skip to content

Commit

Permalink
fix: sync self pkg (#532)
Browse files Browse the repository at this point in the history
> During the syncUpstream process, it will attempt to create sync
repeatedly until it times out, when the pkg has been published in the
self registry.
1. 🐞 When executing the syncTask, filter out scenarios where the target
registry is the self registry.
-------

> 包迁移至当前 registry 时,收到同步请求会产生无效的同步任务,当 `syncUpstream` 时,会尝试重复创建 sync
直到超时。
1. 🐞 syncTask 执行时,先过滤目标 registry 是当前 registry 的场景。
  • Loading branch information
elrrrrrrr committed Jun 25, 2023
1 parent fece882 commit ada3e22
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
10 changes: 9 additions & 1 deletion app/core/service/PackageSyncerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import { Registry } from '../entity/Registry';
import { BadRequestError } from 'egg-errors';
import { ScopeManagerService } from './ScopeManagerService';
import { EventCorkAdvice } from './EventCorkerAdvice';
import { SyncDeleteMode } from '../../common/constants';
import { PresetRegistryName, SyncDeleteMode } from '../../common/constants';

type syncDeletePkgOptions = {
task: Task,
Expand Down Expand Up @@ -372,6 +372,14 @@ export class PackageSyncerService extends AbstractService {
}
logs.push(`[${isoNow()}] 🚧 log: ${logUrl}`);

if (registry?.name === PresetRegistryName.self) {
logs.push(`[${isoNow()}] ❌❌❌❌❌ ${fullname} has been published to the self registry, skip sync ❌❌❌❌❌`);
await this.taskService.finishTask(task, TaskState.Fail, logs.join('\n'));
this.logger.info('[PackageSyncerService.executeTask:fail] taskId: %s, targetName: %s, invalid registryId',
task.taskId, task.targetName);
return;
}

if (pkg && pkg?.registryId !== registry?.registryId) {
if (pkg.registryId) {
logs.push(`[${isoNow()}] ❌❌❌❌❌ ${fullname} registry is ${pkg.registryId} not belong to ${registry?.registryId}, skip sync ❌❌❌❌❌`);
Expand Down
42 changes: 42 additions & 0 deletions test/core/service/PackageSyncerService/executeTask.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,48 @@ describe('test/core/service/PackageSyncerService/executeTask.test.ts', () => {

});

it('should skip self registry', async () => {
const name = '@cnpmcore/test-self-sync';
const { user } = await userService.create({
name: 'test-user',
password: 'this-is-password',
email: '[email protected]',
ip: '127.0.0.1',
});

const registry = await registryManagerService.ensureSelfRegistry();

const publishCmd = {
scope: '@cnpmcore',
name: 'test-self-sync',
version: '1.0.0',
description: '1.0.0',
readme: '',
registryId: registry.registryId,
packageJson: { name, test: 'test', version: '1.0.0' },
dist: {
content: Buffer.alloc(0),
},
isPrivate: false,
publishTime: new Date(),
skipRefreshPackageManifests: false,
};
const pkgVersion = await packageManagerService.publish(publishCmd, user);
assert(pkgVersion.version === '1.0.0');

await packageSyncerService.createTask(name);
const task = await packageSyncerService.findExecuteTask();
assert(task);
await packageSyncerService.executeTask(task);

const stream = await packageSyncerService.findTaskLog(task);
assert(stream);
const log = await TestUtil.readStreamToLog(stream);
// console.log(log);
assert(log.includes(`${name} has been published to the self registry, skip sync ❌❌❌❌❌`));

});

it('should updated package manifests when version insert duplicated', async () => {
// https://www.npmjs.com/package/@cnpmcore/test-sync-package-has-two-versions
const name = '@cnpmcore/test-sync-package-has-two-versions';
Expand Down

0 comments on commit ada3e22

Please sign in to comment.