Skip to content

Commit

Permalink
feat: rewrite to new standards
Browse files Browse the repository at this point in the history
  • Loading branch information
Sealos committed Feb 1, 2024
1 parent 284f339 commit f51ff62
Show file tree
Hide file tree
Showing 66 changed files with 4,801 additions and 20,025 deletions.
2 changes: 1 addition & 1 deletion .idea/runConfigurations/tests.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 14 additions & 14 deletions lib/agenda/cancel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@ import { Document, Filter } from "mongodb";

const debug = createDebugger("agenda:cancel");


/**
* Cancels any jobs matching the passed MongoDB query, and removes them from the database.
* @name Agenda#cancel
* @function
* @param query MongoDB query to use when cancelling
* @caller client code, Agenda.purge(), Job.remove()
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const cancel = async function (
this: Agenda,
query: Filter<Document>
export async function cancel(
this: Agenda,
query: Filter<Document>
): Promise<number | undefined> {
debug("attempting to cancel all Agenda jobs", query);
try {
const { deletedCount } = await this._collection.deleteMany(query);
debug("%s jobs cancelled", deletedCount);
return deletedCount;
} catch (error) {
debug("error trying to delete jobs from MongoDB");
throw error;
}
};
debug('attempting to cancel all Agenda jobs', query);
try {
const { deletedCount } = await this._collection.deleteMany(query);
debug('%s jobs cancelled', deletedCount);
return deletedCount;
} catch (error) {
debug('error trying to delete jobs from MongoDB');
throw error;
}
}
37 changes: 19 additions & 18 deletions lib/agenda/close.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import createDebugger from "debug";

const debug = createDebugger("agenda:close");


/** Close the db and it's underlying connections
* Only works if agenda was instantiated without preconfigured mongoDb instance.
* If the mongoDb instance was supplied during instantiation or via agenda.mongo, this function will do nothing and return agenda anyway.
Expand All @@ -15,23 +16,23 @@ const debug = createDebugger("agenda:close");
*
* @link https://mongodb.github.io/node-mongodb-native/2.0/api/Db.html#close
*/
export const close = async function (
this: Agenda,
option?: { force: boolean }
export async function close(
this: Agenda,
option?: { force: boolean }
): Promise<Agenda> {
debug("close db connection for this agenda instance");
const closeOptions = {
force: false,
...option,
};
try {
if (this._db) {
await this._db.close(closeOptions.force);
}
debug('close db connection for this agenda instance');
const closeOptions = {
force: false,
...option
};
try {
if (this._db) {
await this._db.close(closeOptions.force);
}

return this;
} catch (error) {
debug("error trying to close db connection to");
throw error;
}
};
return this;
} catch (error) {
debug('error trying to close db connection to');
throw error;
}
}
19 changes: 10 additions & 9 deletions lib/agenda/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@ import { Agenda } from ".";

const debug = createDebugger("agenda:create");


/**
* Given a name and some data, create a new job
* @name Agenda#create
* @function
* @param name name of job
* @param data data to set for job
*/
export const create = function<T extends JobAttributesData> (this: Agenda, name: string, data: T): Job {
debug("Agenda.create(%s, [Object])", name);
const priority = this._definitions[name]
? this._definitions[name].priority
: 0;
const shouldSaveResult = this._definitions[name] ? this._definitions[name].shouldSaveResult || false : false
const job = new Job({ name, data, type: "normal", priority, shouldSaveResult, agenda: this });
return job;
};
export function create<T extends JobAttributesData>(this: Agenda, name: string, data: T): Job {
debug('Agenda.create(%s, [Object])', name);
const priority = this._definitions[name]
? this._definitions[name].priority
: 0;
const shouldSaveResult = this._definitions[name] ? this._definitions[name].shouldSaveResult || false : false;
const job = new Job({ name, data, type: 'normal', priority, shouldSaveResult, agenda: this });
return job;
}
43 changes: 20 additions & 23 deletions lib/agenda/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,30 +34,27 @@ export const database = function (

collection = collection || "agendaJobs";

MongoClient.connect(url, options, (error, client) => {
if (error) {
debug("error connecting to MongoDB using collection: [%s]", collection);
if (cb) {
cb(error, null);
} else {
throw error;
}
MongoClient.connect(url, options).then((client) => {
debug(
"successful connection to MongoDB using collection: [%s]",
collection
);

return;
}
if (client) {
this._db = client;
this._mdb = client.db();
this.db_init(collection, cb);
} else {
throw new Error("Mongo Client is undefined");
}
}).catch((error) => {

debug(
"successful connection to MongoDB using collection: [%s]",
collection
);

if (client) {
this._db = client;
this._mdb = client.db();
this.db_init(collection, cb);
} else {
throw new Error("Mongo Client is undefined");
}
});
debug("error connecting to MongoDB using collection: [%s]", collection);
if (cb) {
cb(error, null);
} else {
throw error;
}
});
return this;
};
67 changes: 34 additions & 33 deletions lib/agenda/db-init.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,46 @@
import createDebugger from "debug";
import { AnyError, Collection } from "mongodb";
import { Agenda } from ".";
import createDebugger from 'debug';
import { AnyError, Collection } from 'mongodb';
import { Agenda } from '.';

const debug = createDebugger("agenda:db_init");


/**
* Setup and initialize the collection used to manage Jobs.
* @name Agenda#dbInit
* @function
* @param collection name or undefined for default 'agendaJobs'
* @param [cb] called when the db is initialized
*/
export const dbInit = function (
this: Agenda,
collection = "agendaJobs",
cb?: (error: AnyError | undefined, collection: Collection<any> | null) => void
export function dbInit(
this: Agenda,
collection = 'agendaJobs',
cb?: (error: AnyError | undefined, collection: Collection<any> | null) => void
): void {
debug("init database collection using name [%s]", collection);
this._collection = this._mdb.collection(collection);
if (this._disableAutoIndex) {
debug("skipping auto index creation");
this.emit("ready");
return;
}

debug("attempting index creation");
this._collection.createIndex(
this._indices,
{ name: "findAndLockNextJobIndex" },
(error) => {
if (error) {
debug("index creation failed");
this.emit("error", error);
} else {
debug("index creation success");
this.emit("ready");
}

if (cb) {
cb(error, this._collection);
}
debug('init database collection using name [%s]', collection);
this._collection = this._mdb.collection(collection);
if (this._disableAutoIndex) {
debug('skipping auto index creation');
this.emit('ready');
return;
}
);
};

debug('attempting index creation');
this._collection.createIndex(
this._indices,
{ name: 'findAndLockNextJobIndex' })
.then(() => {
debug('index creation success');
this.emit('ready');
if (cb) {
cb(undefined, this._collection);
}
})
.catch(error => {
debug('index creation failed');
this.emit('error', error);
if (cb) {
cb(error, this._collection);
}
});
}
15 changes: 8 additions & 7 deletions lib/agenda/default-concurrency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ import createDebugger from "debug";

const debug = createDebugger("agenda:defaultConcurrency");


/**
* Set the default concurrency for each job
* @name Agenda#defaultConcurrency
* @function
* @param concurrency default concurrency
*/
export const defaultConcurrency = function (
this: Agenda,
concurrency: number
export function defaultConcurrency(
this: Agenda,
concurrency: number
): Agenda {
debug("Agenda.defaultConcurrency(%d)", concurrency);
this._defaultConcurrency = concurrency;
return this;
};
debug('Agenda.defaultConcurrency(%d)', concurrency);
this._defaultConcurrency = concurrency;
return this;
}
11 changes: 6 additions & 5 deletions lib/agenda/default-lock-lifetime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ import createDebugger from "debug";

const debug = createDebugger("agenda:defaultLockLifetime");


/**
* Set the default lock time (in ms)
* Default is 10 * 60 * 1000 ms (10 minutes)
* @name Agenda#defaultLockLifetime
* @function
* @param {Number} ms time in ms to set default lock
*/
export const defaultLockLifetime = function (this: Agenda, ms: number): Agenda {
debug("Agenda.defaultLockLifetime(%d)", ms);
this._defaultLockLifetime = ms;
return this;
};
export function defaultLockLifetime(this: Agenda, ms: number): Agenda {
debug('Agenda.defaultLockLifetime(%d)', ms);
this._defaultLockLifetime = ms;
return this;
}
13 changes: 7 additions & 6 deletions lib/agenda/default-lock-limit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ import createDebugger from "debug";

const debug = createDebugger("agenda:defaultLockLimit");


/**
* Set default lock limit per job type
* @name Agenda#defaultLockLimit
* @function
* @param {Number} num Lock limit per job
* @param {Number} times Lock limit per job
* @returns {Agenda} agenda instance
*/
export const defaultLockLimit = function (this: Agenda, times: number): Agenda {
debug("Agenda.defaultLockLimit(%d)", times);
this._defaultLockLimit = times;
return this;
};
export function defaultLockLimit(this: Agenda, times: number): Agenda {
debug('Agenda.defaultLockLimit(%d)', times);
this._defaultLockLimit = times;
return this;
}
Loading

0 comments on commit f51ff62

Please sign in to comment.