Skip to content

Commit

Permalink
[feature] deleting job completed
Browse files Browse the repository at this point in the history
  • Loading branch information
HahaBill committed Oct 30, 2024
1 parent f139a04 commit 37f032b
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 2 deletions.
22 changes: 21 additions & 1 deletion src/jobs/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export default class Job {
* @returns {Promise<void>} - Resolves when the job is created.
* @throws {MindsDbError} - If job creation fails.
*/
async create(): Promise<void> {
async createJob(): Promise<void> {
if (this.queries.length === 0) {
throw new MindsDbError('No queries added to the job.');
}
Expand All @@ -116,4 +116,24 @@ export default class Job {
this.ifCondition
);
}

/**
* Deletes the job from MindsDB.
* @returns {Promise<void>} - Resolves when the job is deleted.
* @throws {MindsDbError} - If job deletion fails.
*/
async deleteJob(): Promise<void> {
await this.jobsApiClient.deleteJob(this.name, this.project);
}

/**
* Drops (deletes) a job from MindsDB by name.
* @param {string} name - Name of the job to drop.
* @param {string} project - Project the job belongs to.
* @returns {Promise<void>} - Resolves when the job is dropped.
* @throws {MindsDbError} - Something went wrong while dropping the job.
*/
async dropJob(name: string, project: string = "mindsdb"): Promise<void> {
await this.jobsApiClient.dropJob(name, project);
}
}
21 changes: 21 additions & 0 deletions src/jobs/jobsApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,25 @@ export default abstract class JobsApiClient {
every?: string,
ifCondition?: string
): Promise<void>;

/**
* Internal method to delete the job in MindsDB.
* @param {string} name - Name of the job to delete.
* @param {string} project - Project the job will be deleted.
* @returns {Promise<void>} - Resolves when the job is deleted.
* @throws {MindsDbError} - Something went wrong while deleting the job.
*/
abstract deleteJob(
name: string,
project: string
): Promise<void>

/**
* Drops (deletes) a job from MindsDB by name.
* @param {string} name - Name of the job to drop.
* @param {string} project - Project the job belongs to.
* @returns {Promise<void>} - Resolves when the job is dropped.
* @throws {MindsDbError} - Something went wrong while dropping the job.
*/
abstract dropJob(name: string, project: string): Promise<void>;
}
40 changes: 39 additions & 1 deletion src/jobs/jobsRestApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default class JobsRestApiClient extends JobsApiClient {
* @param {string} project - Project the job belongs to.
* @returns {Job} - A new Job instance.
*/
override create(name: string, project: string): Job {
override create(name: string, project: string = "mindsdb"): Job {
return new Job(this, name, project);
}

Expand Down Expand Up @@ -67,4 +67,42 @@ export default class JobsRestApiClient extends JobsApiClient {
throw new MindsDbError(sqlQueryResult.error_message);
}
}

/**
* Internal method to delete the job in MindsDB.
* @param {string} name - Name of the job to delete.
* @param {string} project - Project the job belongs to.
* @returns {Promise<void>} - Resolves when the job is deleted.
* @throws {MindsDbError} - Something went wrong while deleting the job.
*/
override async deleteJob(
name: string,
project: string
): Promise<void> {
const dropJobQuery = `DROP JOB ${mysql.escapeId(project)}.${mysql.escapeId(name)};`;

const sqlQueryResult = await this.sqlClient.runQuery(dropJobQuery);
if (sqlQueryResult.error_message) {
throw new MindsDbError(sqlQueryResult.error_message);
}
}

/**
* Internal method to deleting the job in MindsDB with users providing a name
* @param {string} name - Name of the job to delete.
* @param {string} project - Project the job belongs to.
* @returns {Promise<void>} - Resolves when the job is deleted.
* @throws {MindsDbError} - Something went wrong while deleting the job.
*/
override async dropJob(
name: string,
project: string = "mindsdb"
): Promise<void> {
const dropJobQuery = `DROP JOB ${mysql.escapeId(project)}.${mysql.escapeId(name)};`;

const sqlQueryResult = await this.sqlClient.runQuery(dropJobQuery);
if (sqlQueryResult.error_message) {
throw new MindsDbError(sqlQueryResult.error_message);
}
}
}

0 comments on commit 37f032b

Please sign in to comment.