Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove a job #86

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ModelsModule from './models/modelsModule';
import DatabasesModule from './databases/databasesModule';
import ProjectsModule from './projects/projectsModule';
import JobsModule from './jobs/jobsModule';
import SQLModule from './sql/sqlModule';
import ViewsModule from './views/viewsModule';
import Constants from './constants';
Expand All @@ -26,6 +27,7 @@ import { MindsDbError } from './errors';
import { BatchQueryOptions, QueryOptions } from './models/queryOptions';
import { FinetuneOptions, TrainingOptions } from './models/trainingOptions';
import Project from './projects/project';
import Job from './jobs/job';
import SqlQueryResult from './sql/sqlQueryResult';
import Table from './tables/table';
import { JsonPrimitive, JsonValue } from './util/json';
Expand All @@ -49,6 +51,11 @@ const Projects = new ProjectsModule.ProjectsRestApiClient(
defaultAxiosInstance,
httpAuthenticator
);
const Jobs = new JobsModule.JobsRestApiClient(
SQL,
defaultAxiosInstance,
httpAuthenticator
);
const Tables = new TablesModule.TablesRestApiClient(SQL);
const Views = new ViewsModule.ViewsRestApiClient(SQL);
const MLEngines = new MLEnginesModule.MLEnginesRestApiClient(
Expand Down Expand Up @@ -83,6 +90,7 @@ const connect = async function (options: ConnectionOptions): Promise<void> {
const httpClient = getAxiosInstance(options);
SQL.client = httpClient;
Projects.client = httpClient;
Jobs.client = httpClient;
MLEngines.client = httpClient;
Callbacks.client = httpClient;

Expand Down Expand Up @@ -114,6 +122,7 @@ export default {
Databases,
Models,
Projects,
Jobs,
Tables,
Views,
MLEngines,
Expand All @@ -131,6 +140,7 @@ export {
FinetuneOptions,
TrainingOptions,
Project,
Job,
SqlQueryResult,
Table,
JsonPrimitive,
Expand Down
25 changes: 25 additions & 0 deletions src/jobs/job.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export default class Job {
/** Name of the Job. */
name: string;
query: string;
if_query: string;
start_at: string;
end_at: string;
schedule_str: string;

constructor(
name: string,
query: string,
if_query: string,
start_at: string,
end_at: string,
schedule_str: string
) {
this.name = name;
this.query = query;
this.if_query = if_query;
this.start_at = start_at;
this.end_at = end_at;
this.schedule_str = schedule_str;
}
}
10 changes: 10 additions & 0 deletions src/jobs/jobsApiClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Job from './job';

/** Abstract class outlining Jobs operations supported by the SDK. */
export default abstract class JobsApiClient {
/**
* Removes a job for the current project
* @returns {Promise<void>}
*/
abstract deleteJob(name: string, project: string): Promise<void>;
}
3 changes: 3 additions & 0 deletions src/jobs/jobsModule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import JobsRestApiClient from './jobsRestApiClient';

export default { JobsRestApiClient };
44 changes: 44 additions & 0 deletions src/jobs/jobsRestApiClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Axios } from 'axios';
import JobsApiClient from './jobsApiClient';
import mysql from 'mysql';
import SqlApiClient from '../sql/sqlApiClient';
import HttpAuthenticator from '../httpAuthenticator';
import { MindsDbError } from '../errors';

/** Implementation of JobsApiClient that goes through the REST API. */
export default class JobsRestApiClient extends JobsApiClient {
/** Axios client to send all HTTP requests. */
client: Axios;

/** Authenticator to use for reauthenticating if needed. */
authenticator: HttpAuthenticator;

/** SQL API client to send all SQL query requests. */
sqlClient: SqlApiClient;

/**
* Constructor for Jobs API client.
* @param {Axios} client - Axios instance to send all HTTP requests.
*/
constructor(client: Axios, authenticator: HttpAuthenticator, sqlClient: SqlApiClient) {
super();
this.client = client;
this.authenticator = authenticator;
this.sqlClient = sqlClient;
}

/**
* Delete a job
* @returns {Promise<Array<Project>>} - Drop a job
* @throws {MindsDbError} - Something went wrong fetching projects.
*/
override async deleteJob(name: string, project: string): Promise<void> {
const deleteQuery = `DROP JOB ${mysql.escapeId(project)}.${mysql.escapeId(
name
)}`;
const sqlQueryResult = await this.sqlClient.runQuery(deleteQuery);
if (sqlQueryResult.error_message) {
throw new MindsDbError(sqlQueryResult.error_message);
}
}
}