Skip to content

Commit

Permalink
fix: auto remove blocked package on sync process (#700)
Browse files Browse the repository at this point in the history
> remove es index when the target pkg has been blocked or deleted.
1. ⚓ update `SyncESPackage` logic for event register
2. ♻️ do the block check when `syncPackage` 
---------
> 包删除或被 block 时,es 索引同步删除
1. ⚓ 修改 `SyncESPackage` 逻辑,更新事件注册
2. ♻️ `syncPackage` 时,重新判断包是否被 block,用于清理

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **New Features**
- Introduced event types `PACKAGE_BLOCKED` and `PACKAGE_UNBLOCKED` for
better package management.

- **Bug Fixes**
- Improved logic to handle package blocks before syncing, ensuring
smoother operations.

- **Tests**
- Added test scenarios to cover new package blocking and unblocking
features, enhancing reliability.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
elrrrrrrr authored Jun 25, 2024
1 parent 571d265 commit ca6ce4e
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 44 deletions.
50 changes: 6 additions & 44 deletions app/core/event/SyncESPackage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {
PACKAGE_MAINTAINER_CHANGED,
PACKAGE_MAINTAINER_REMOVED,
PACKAGE_META_CHANGED,
PACKAGE_BLOCKED,
PACKAGE_UNBLOCKED,
} from './index';

import { PackageSearchService } from '../service/PackageSearchService';
Expand All @@ -30,6 +32,7 @@ class SyncESPackage {
}

@Event(PACKAGE_UNPUBLISHED)
@Event(PACKAGE_BLOCKED)
export class PackageUnpublished extends SyncESPackage {
async handle(fullname: string) {
if (!this.config.cnpmcore.enableElasticsearch) return;
Expand All @@ -38,56 +41,15 @@ export class PackageUnpublished extends SyncESPackage {
}

@Event(PACKAGE_VERSION_ADDED)
export class PackageVersionAdded extends SyncESPackage {
async handle(fullname: string) {
await this.syncPackage(fullname);
}
}

@Event(PACKAGE_META_CHANGED)
@Event(PACKAGE_VERSION_REMOVED)
export class PackageVersionRemoved extends SyncESPackage {
async handle(fullname: string) {
await this.syncPackage(fullname);
}
}

@Event(PACKAGE_TAG_ADDED)
export class PackageTagAdded extends SyncESPackage {
async handle(fullname: string) {
await this.syncPackage(fullname);
}
}

@Event(PACKAGE_TAG_CHANGED)
export class PackageTagChanged extends SyncESPackage {
async handle(fullname: string) {
await this.syncPackage(fullname);
}
}

@Event(PACKAGE_TAG_REMOVED)
export class PackageTagRemoved extends SyncESPackage {
async handle(fullname: string) {
await this.syncPackage(fullname);
}
}

@Event(PACKAGE_MAINTAINER_CHANGED)
export class PackageMaintainerChanged extends SyncESPackage {
async handle(fullname: string) {
await this.syncPackage(fullname);
}
}

@Event(PACKAGE_MAINTAINER_REMOVED)
export class PackageMaintainerRemoved extends SyncESPackage {
async handle(fullname: string) {
await this.syncPackage(fullname);
}
}

@Event(PACKAGE_META_CHANGED)
export class PackageMetaChanged extends SyncESPackage {
@Event(PACKAGE_UNBLOCKED)
export class PackageVersionAdded extends SyncESPackage {
async handle(fullname: string) {
await this.syncPackage(fullname);
}
Expand Down
10 changes: 10 additions & 0 deletions app/core/service/PackageSearchService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { PackageManagerService } from './PackageManagerService';
import { SearchManifestType, SearchMappingType, SearchRepository } from '../../repository/SearchRepository';
import { PackageVersionDownloadRepository } from '../../repository/PackageVersionDownloadRepository';
import { PackageRepository } from '../../repository/PackageRepository';
import { PackageVersionBlockRepository } from '../../repository/PackageVersionBlockRepository';


@SingletonProto({
Expand All @@ -22,6 +23,8 @@ export class PackageSearchService extends AbstractService {
private packageVersionDownloadRepository: PackageVersionDownloadRepository;
@Inject()
protected packageRepository: PackageRepository;
@Inject()
protected packageVersionBlockRepository: PackageVersionBlockRepository;

async syncPackage(fullname: string, isSync = true) {
const [ scope, name ] = getScopeAndName(fullname);
Expand All @@ -38,6 +41,13 @@ export class PackageSearchService extends AbstractService {
return;
}

const block = await this.packageVersionBlockRepository.findPackageBlock(pkg.packageId);
if (block) {
this.logger.warn('[PackageSearchService.syncPackage] package:%s is blocked, try to remove es', fullname);
await this.removePackage(fullname);
return;
}

// get last year download data
const startDate = dayjs().subtract(1, 'year');
const endDate = dayjs();
Expand Down
71 changes: 71 additions & 0 deletions test/repository/SearchRepository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ import { strict as assert } from 'node:assert';
import { app, mock } from 'egg-mock/bootstrap';
import { SearchManifestType, SearchRepository } from '../../app/repository/SearchRepository';
import { mockES } from '../../config/config.unittest';
import { PackageManagerService } from '../../app/core/service/PackageManagerService';
import { TestUtil } from '../TestUtil';
import { PackageSearchService } from '../../app/core/service/PackageSearchService';

describe('test/repository/SearchRepository.test.ts', () => {
let searchRepository: SearchRepository;
let packageManagerService: PackageManagerService;

beforeEach(async () => {
mock(app.config.cnpmcore, 'enableElasticsearch', true);
mock(app.config.cnpmcore, 'elasticsearchIndex', 'cnpmcore_packages');
searchRepository = await app.getEggObject(SearchRepository);
packageManagerService = await app.getEggObject(PackageManagerService);
});

afterEach(async () => {
Expand Down Expand Up @@ -107,5 +112,71 @@ describe('test/repository/SearchRepository.test.ts', () => {
const id = await searchRepository.removePackage(mockedPackageName);
assert.equal(id, mockedPackageName);
});

it('should clear blocked pkg', async () => {

await TestUtil.createPackage({
name: '@cnpm/example',
});

const _source = {
downloads: {
all: 0,
},
package: {
name: '@cnpm/example',
description: 'example package',
},
};

mockES.add({
method: 'POST',
path: `/${app.config.cnpmcore.elasticsearchIndex}/_search`,
}, () => {
return {
hits: {
total: { value: 1, relation: 'eq' },
hits: [{
_source,
}],
},
};
});


let res = await searchRepository.searchPackage({
body: {
query: {
match: {
'package.name': '@cnpm/example',
},
},
},
});

assert.deepEqual(res.hits.length, 1);

res = await searchRepository.searchPackage({
body: {
query: {
match: {
'package.name': '@cnpm/example',
},
},
},
});

let called = false;

mock(PackageSearchService.prototype, 'removePackage', async (fullname: string) => {
if (fullname === '@cnpm/example') {
called = true;
}
});

await packageManagerService.blockPackageByFullname('@cnpm/example', 'test');
assert(called);

});
});
});

0 comments on commit ca6ce4e

Please sign in to comment.