mirror of
https://github.com/morten-olsen/mini-loader.git
synced 2026-02-08 01:36:26 +01:00
feat: add scheduler (#30)
This commit is contained in:
32
packages/cli/src/commands/schedules/schedules.add.ts
Normal file
32
packages/cli/src/commands/schedules/schedules.add.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import { Command } from 'commander';
|
||||
import { createClient } from '../../client/client.js';
|
||||
import { step } from '../../utils/step.js';
|
||||
import { Context } from '../../context/context.js';
|
||||
import { Config } from '../../config/config.js';
|
||||
|
||||
const add = new Command('add');
|
||||
|
||||
add
|
||||
.description('Add schedule')
|
||||
.argument('<load-id>', 'Load ID')
|
||||
.argument('<cron>', 'Cron')
|
||||
.option('-n, --name <name>', 'Name')
|
||||
.action(async (loadId, cron) => {
|
||||
const config = new Config();
|
||||
const context = new Context(config.context);
|
||||
const { name } = add.opts();
|
||||
const client = await step('Connecting to server', async () => {
|
||||
return createClient(context);
|
||||
});
|
||||
const id = await step('Adding schedule', async () => {
|
||||
return await client.schedules.add.mutate({
|
||||
name,
|
||||
load: loadId,
|
||||
cron,
|
||||
});
|
||||
});
|
||||
|
||||
console.log(`Schedule added with ID ${id}`);
|
||||
});
|
||||
|
||||
export { add };
|
||||
39
packages/cli/src/commands/schedules/schedules.list.ts
Normal file
39
packages/cli/src/commands/schedules/schedules.list.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { Command } from 'commander';
|
||||
import { createClient } from '../../client/client.js';
|
||||
import { step } from '../../utils/step.js';
|
||||
import { Context } from '../../context/context.js';
|
||||
import { Config } from '../../config/config.js';
|
||||
|
||||
const list = new Command('list');
|
||||
|
||||
const toInt = (value?: string) => {
|
||||
if (!value) {
|
||||
return undefined;
|
||||
}
|
||||
return parseInt(value, 10);
|
||||
};
|
||||
|
||||
list
|
||||
.alias('ls')
|
||||
.description('List schedules')
|
||||
.option('-l, --load-ids <loadIds...>', 'Load ID')
|
||||
.option('-o, --offset <offset>', 'Offset')
|
||||
.option('-a, --limit <limit>', 'Limit', '1000')
|
||||
.action(async () => {
|
||||
const { loadIds, offset, limit } = list.opts();
|
||||
const config = new Config();
|
||||
const context = new Context(config.context);
|
||||
const client = await step('Connecting to server', async () => {
|
||||
return createClient(context);
|
||||
});
|
||||
const schedules = await step('Getting schedules', async () => {
|
||||
return await client.schedules.find.query({
|
||||
loadIds,
|
||||
offset: toInt(offset),
|
||||
limit: toInt(limit),
|
||||
});
|
||||
});
|
||||
console.table(schedules);
|
||||
});
|
||||
|
||||
export { list };
|
||||
61
packages/cli/src/commands/schedules/schedules.remove.ts
Normal file
61
packages/cli/src/commands/schedules/schedules.remove.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import { Command } from 'commander';
|
||||
import { createClient } from '../../client/client.js';
|
||||
import { step } from '../../utils/step.js';
|
||||
import { Context } from '../../context/context.js';
|
||||
import inquirer from 'inquirer';
|
||||
import { Config } from '../../config/config.js';
|
||||
|
||||
const remove = new Command('remove');
|
||||
|
||||
const toInt = (value?: string) => {
|
||||
if (!value) {
|
||||
return undefined;
|
||||
}
|
||||
return parseInt(value, 10);
|
||||
};
|
||||
|
||||
remove
|
||||
.alias('ls')
|
||||
.description('LRemove schedules')
|
||||
.option('-i, --ids <ids...>', 'Load IDs')
|
||||
.option('-l, --load-ids <loadIds...>', 'Load IDs')
|
||||
.option('-o, --offset <offset>', 'Offset')
|
||||
.option('-a, --limit <limit>', 'Limit', '1000')
|
||||
.action(async () => {
|
||||
const { ids, loadIds, offset, limit } = remove.opts();
|
||||
const config = new Config();
|
||||
const context = new Context(config.context);
|
||||
const client = await step('Connecting to server', async () => {
|
||||
return createClient(context);
|
||||
});
|
||||
const response = await step('Preparing to delete', async () => {
|
||||
return await client.schedules.prepareRemove.query({
|
||||
ids,
|
||||
loadIds,
|
||||
offset: toInt(offset),
|
||||
limit: toInt(limit),
|
||||
});
|
||||
});
|
||||
|
||||
if (!response.ids.length) {
|
||||
console.log('No logs to delete');
|
||||
return;
|
||||
}
|
||||
const { confirm } = await inquirer.prompt([
|
||||
{
|
||||
type: 'confirm',
|
||||
name: 'confirm',
|
||||
message: `Are you sure you want to delete ${response.ids.length} schedules?`,
|
||||
},
|
||||
]);
|
||||
|
||||
if (!confirm) {
|
||||
return;
|
||||
}
|
||||
|
||||
await step('Deleting artifacts', async () => {
|
||||
await client.artifacts.remove.mutate(response);
|
||||
});
|
||||
});
|
||||
|
||||
export { remove };
|
||||
11
packages/cli/src/commands/schedules/schedules.ts
Normal file
11
packages/cli/src/commands/schedules/schedules.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { Command } from 'commander';
|
||||
import { list } from './schedules.list.js';
|
||||
import { remove } from './schedules.remove.js';
|
||||
import { add } from './schedules.add.js';
|
||||
|
||||
const schedules = new Command('schedules');
|
||||
schedules.addCommand(list);
|
||||
schedules.addCommand(remove);
|
||||
schedules.addCommand(add);
|
||||
|
||||
export { schedules };
|
||||
@@ -8,6 +8,7 @@ import { secrets } from './commands/secrets/secrets.js';
|
||||
import { local } from './commands/local/local.js';
|
||||
import { auth } from './commands/auth/auth.js';
|
||||
import { contexts } from './commands/contexts/contexts.js';
|
||||
import { schedules } from './commands/schedules/schedules.js';
|
||||
|
||||
program.addCommand(loads);
|
||||
program.addCommand(runs);
|
||||
@@ -17,6 +18,7 @@ program.addCommand(secrets);
|
||||
program.addCommand(local);
|
||||
program.addCommand(auth);
|
||||
program.addCommand(contexts);
|
||||
program.addCommand(schedules);
|
||||
|
||||
program.version(pkg.version);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user