Skip to content

Commit 9ef6e97

Browse files
feat: cron indexer
1 parent ee8c7a2 commit 9ef6e97

File tree

4 files changed

+101
-1
lines changed

4 files changed

+101
-1
lines changed

src/indexer/IndexManager.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,17 @@ import AclIndexer from './acl/AclIndexer';
1818
import { AclIndexData } from './acl/AclIndexData';
1919
import TemplateIndexer from './template/TemplateIndexer';
2020
import { TemplateIndexData } from './template/TemplateIndexData';
21+
import CronIndexer from './cron/CronIndexer';
22+
import { CronIndexData } from './cron/CronIndexData';
2123

2224
type IndexerInstance =
2325
| DiIndexer
2426
| ModuleIndexer
2527
| AutoloadNamespaceIndexer
2628
| EventsIndexer
2729
| AclIndexer
28-
| TemplateIndexer;
30+
| TemplateIndexer
31+
| CronIndexer;
2932

3033
type IndexerDataMap = {
3134
[DiIndexer.KEY]: DiIndexData;
@@ -34,6 +37,7 @@ type IndexerDataMap = {
3437
[EventsIndexer.KEY]: EventsIndexData;
3538
[AclIndexer.KEY]: AclIndexData;
3639
[TemplateIndexer.KEY]: TemplateIndexData;
40+
[CronIndexer.KEY]: CronIndexData;
3741
};
3842

3943
class IndexManager {
@@ -50,6 +54,7 @@ class IndexManager {
5054
new EventsIndexer(),
5155
new AclIndexer(),
5256
new TemplateIndexer(),
57+
new CronIndexer(),
5358
];
5459
this.indexStorage = new IndexStorage();
5560
}
@@ -187,6 +192,9 @@ class IndexManager {
187192
case TemplateIndexer.KEY:
188193
return new TemplateIndexData(data) as IndexerDataMap[T];
189194

195+
case CronIndexer.KEY:
196+
return new CronIndexData(data) as IndexerDataMap[T];
197+
190198
default:
191199
return undefined;
192200
}

src/indexer/cron/CronIndexData.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Memoize } from 'typescript-memoize';
2+
import { Job } from './types';
3+
import { AbstractIndexData } from 'indexer/AbstractIndexData';
4+
import CronIndexer from './CronIndexer';
5+
6+
export class CronIndexData extends AbstractIndexData<Job[]> {
7+
@Memoize({
8+
tags: [CronIndexer.KEY],
9+
})
10+
public getJobs(): Job[] {
11+
return this.getValues().flatMap(data => data);
12+
}
13+
}

src/indexer/cron/CronIndexer.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { RelativePattern, Uri } from 'vscode';
2+
import { XMLParser } from 'fast-xml-parser';
3+
import { get } from 'lodash-es';
4+
import FileSystem from 'util/FileSystem';
5+
import { Job } from './types';
6+
import { Indexer } from 'indexer/Indexer';
7+
import { IndexerKey } from 'types/indexer';
8+
9+
export default class CronIndexer extends Indexer<Job[]> {
10+
public static readonly KEY = 'cron';
11+
12+
protected xmlParser: XMLParser;
13+
14+
public constructor() {
15+
super();
16+
17+
this.xmlParser = new XMLParser({
18+
ignoreAttributes: false,
19+
attributeNamePrefix: '@_',
20+
isArray: (_name, jpath) => {
21+
return ['config.group', 'config.group.job'].includes(jpath);
22+
},
23+
});
24+
}
25+
26+
public getVersion(): number {
27+
return 2;
28+
}
29+
30+
public getId(): IndexerKey {
31+
return CronIndexer.KEY;
32+
}
33+
34+
public getName(): string {
35+
return 'crontab.xml';
36+
}
37+
38+
public getPattern(uri: Uri): RelativePattern {
39+
return new RelativePattern(uri, '**/etc/crontab.xml');
40+
}
41+
42+
public async indexFile(uri: Uri): Promise<Job[]> {
43+
const xml = await FileSystem.readFile(uri);
44+
const parsed = this.xmlParser.parse(xml);
45+
const config = get(parsed, 'config', {});
46+
47+
const data: Job[] = [];
48+
49+
// Index groups
50+
const groups = get(config, 'group', []);
51+
52+
for (const group of groups) {
53+
const jobs = get(group, 'job', []);
54+
55+
for (const job of jobs) {
56+
data.push({
57+
name: job['@_name'],
58+
instance: job['@_instance'],
59+
method: job['@_method'],
60+
schedule: job['schedule'],
61+
config_path: job['config_path'],
62+
path: uri.fsPath,
63+
group: group['@_id'],
64+
});
65+
}
66+
}
67+
68+
return data;
69+
}
70+
}

src/indexer/cron/types.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export interface Job {
2+
name: string;
3+
instance: string;
4+
method: string;
5+
schedule?: string;
6+
config_path?: string;
7+
path: string;
8+
group: string;
9+
}

0 commit comments

Comments
 (0)