
Security News
Crates.io Users Targeted by Phishing Emails
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.
@commandkit/tasks
Advanced tools
Task management plugin for CommandKit. Provides on-demand task creation and management with support for both static and dynamic tasks.
npm install @commandkit/tasks
import { tasks } from '@commandkit/tasks';
export default {
plugins: [
tasks({
tasksPath: 'app/tasks', // optional, defaults to 'app/tasks'
enableHMR: true, // optional, defaults to true in development
}),
],
};
Create a file in src/app/tasks/
:
import { task } from '@commandkit/tasks';
export const refreshExchangeRate = task({
name: 'refresh-exchange-rate',
schedule: '0 0 * * *', // cron expression - daily at midnight
async execute(ctx) {
// Fetch latest exchange rates
const rates = await fetchExchangeRates();
await updateDatabase(rates);
},
});
export const cleanupOldData = task({
name: 'cleanup-old-data',
schedule: () => new Date(Date.now() + 24 * 60 * 60 * 1000), // tomorrow
async prepare(ctx) {
// Only run if there's old data to clean
return await hasOldData();
},
async execute(ctx) {
await cleanupOldRecords();
},
});
import { createTask } from '@commandkit/tasks';
export default {
name: 'remind-me',
description: 'Set a reminder',
async run(ctx) {
const time = ctx.interaction.options.getString('time');
const reason = ctx.interaction.options.getString('reason');
await createTask({
name: 'reminder',
schedule: new Date(Date.now() + ms(time)),
data: {
userId: ctx.interaction.user.id,
reason,
},
});
await ctx.interaction.reply('Reminder set!');
},
};
interface TasksPluginOptions {
tasksPath?: string; // Path to tasks directory, defaults to 'app/tasks'
enableHMR?: boolean; // Enable HMR for tasks, defaults to true in development
}
interface TaskDefinition {
name: string;
schedule?: ScheduleType;
prepare?: (ctx: TaskContext) => Promise<boolean> | boolean;
execute: (ctx: TaskContext) => Promise<void> | void;
}
type ScheduleType =
| Date
| number // unix timestamp
| string // cron expression or date string
| (() => Date | number | string); // dynamic schedule
Cron Expressions: The plugin supports standard cron expressions (e.g., '0 0 * * *'
for daily at midnight). Cron parsing is handled by cron-parser
for in-memory and SQLite drivers, while BullMQ uses its built-in cron support.
interface TaskContext {
task: TaskData;
commandkit: CommandKit;
client: Client;
}
task(definition: TaskDefinition)
Creates a task definition.
import { task } from '@commandkit/tasks';
export const myTask = task({
name: 'my-task',
schedule: '0 0 * * *',
async execute(ctx) {
// Task logic here
},
});
createTask(options: CreateTaskOptions)
Creates a dynamic task.
import { createTask } from '@commandkit/tasks';
await createTask({
name: 'reminder',
schedule: new Date(Date.now() + 60000), // 1 minute from now
data: { userId: '123', message: 'Hello!' },
});
executeTask(taskOrName: TaskDefinition | string)
Executes a task immediately.
import { executeTask } from '@commandkit/tasks';
await executeTask('my-task');
// or
await executeTask(myTask);
cancelTask(taskOrName: TaskDefinition | string)
Cancels a scheduled task.
import { cancelTask } from '@commandkit/tasks';
await cancelTask('my-task');
pauseTask(taskOrName: TaskDefinition | string)
Pauses a task.
import { pauseTask } from '@commandkit/tasks';
await pauseTask('my-task');
resumeTask(taskOrName: TaskDefinition | string)
Resumes a paused task.
import { resumeTask } from '@commandkit/tasks';
await resumeTask('my-task');
The drivers handle all scheduling and timing internally. When a task is due for execution, the driver calls the plugin's execution handler.
import { driver } from '@commandkit/tasks';
import { InMemoryDriver } from '@commandkit/tasks/drivers';
driver.use(new InMemoryDriver());
import { driver } from '@commandkit/tasks';
import { SQLiteDriver } from '@commandkit/tasks/drivers';
driver.use(new SQLiteDriver('./tasks.db'));
Note: Requires sqlite3
, sqlite
, and cron-parser
packages to be installed.
import { driver } from '@commandkit/tasks';
import { BullMQDriver } from '@commandkit/tasks/drivers';
driver.use(new BullMQDriver({
host: 'localhost',
port: 6379,
}));
Note: Requires bullmq
package to be installed. BullMQ has built-in cron support, so no additional cron parsing is needed.
import { task } from '@commandkit/tasks';
export const databaseBackup = task({
name: 'database-backup',
schedule: '0 2 * * *', // Daily at 2 AM
async execute(ctx) {
const backup = await createBackup();
await uploadToCloud(backup);
await ctx.client.channels.cache.get('backup-log')?.send('Backup completed!');
},
});
import { task } from '@commandkit/tasks';
export const reminder = task({
name: 'reminder',
async execute(ctx) {
const { userId, message } = ctx.task.data;
const user = await ctx.client.users.fetch(userId);
await user.send(`Reminder: ${message}`);
},
});
import { task } from '@commandkit/tasks';
export const maintenanceCheck = task({
name: 'maintenance-check',
schedule: '0 */6 * * *', // Every 6 hours
async prepare(ctx) {
// Only run if maintenance mode is not enabled
return !ctx.commandkit.store.get('maintenance-mode');
},
async execute(ctx) {
await performMaintenanceChecks();
},
});
MIT
FAQs
Task management plugin for CommandKit
The npm package @commandkit/tasks receives a total of 1,023 weekly downloads. As such, @commandkit/tasks popularity was classified as popular.
We found that @commandkit/tasks demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.
Product
Socket now lets you customize pull request alert headers, helping security teams share clear guidance right in PRs to speed reviews and reduce back-and-forth.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.