Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Format using Prettier. (#1211)
* setup prettier

Signed-off-by: Haris Sulaiman <harisvsulaiman@gmail.com>

* format lib

Signed-off-by: Haris Sulaiman <harisvsulaiman@gmail.com>

format tests

Signed-off-by: Haris Sulaiman <harisvsulaiman@gmail.com>

format using xo

Signed-off-by: Haris Sulaiman <harisvsulaiman@gmail.com>

* use elint typescript and prettier with default values

Signed-off-by: Haris Sulaiman <harisvsulaiman@gmail.com>

* use eslint typescript and prettier with default values

Signed-off-by: Haris Sulaiman <harisvsulaiman@gmail.com>

* use eslint typescript and prettier with default values

Signed-off-by: Haris Sulaiman <harisvsulaiman@gmail.com>

* use eslint typescript and prettier.
format using defaults
add linstaged.

* remove eslintcache
remove unwanted eslint packages
warn for some defaults
update gitignore

Signed-off-by: Haris Sulaiman <harisvsulaiman@gmail.com>

* format using prettier

Signed-off-by: Haris Sulaiman <harisvsulaiman@gmail.com>

* format tests

Signed-off-by: Haris Sulaiman <harisvsulaiman@gmail.com>

* Move eslint config to package.json

Signed-off-by: Haris Sulaiman <harisvsulaiman@gmail.com>
  • Loading branch information
harisvsulaiman committed Feb 1, 2021
1 parent 4ae4ee9 commit 234fff0
Show file tree
Hide file tree
Showing 58 changed files with 2,450 additions and 4,470 deletions.
117 changes: 117 additions & 0 deletions .gitignore
Expand Up @@ -11,3 +11,120 @@ docs/agenda/*
dist
.ultra.cache.json
.vscode

# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# Next.js build output
.next
out

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
40 changes: 22 additions & 18 deletions examples/concurrency.js
Expand Up @@ -8,7 +8,7 @@ function time() {
}

function sleep(ms) {
return new Promise(resolve => {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
Expand All @@ -22,31 +22,35 @@ const agenda = new Agenda({
});

let jobRunCount = 1;
agenda.define('long-running job', {
lockLifetime: 5 * 1000, // Max amount of time the job should take
concurrency: 3 // Max number of job instances to run at the same time
}, async(job, done) => {
const thisJob = jobRunCount++;
console.log(`#${thisJob} started`);
agenda.define(
'long-running job',
{
lockLifetime: 5 * 1000, // Max amount of time the job should take
concurrency: 3 // Max number of job instances to run at the same time
},
async(job, done) => {
const thisJob = jobRunCount++;
console.log(`#${thisJob} started`);

// 3 job instances will be running at the same time, as specified by `concurrency` above
await sleep(30 * 1000);
// Comment the job processing statement above, and uncomment one of the blocks below
// 3 job instances will be running at the same time, as specified by `concurrency` above
await sleep(30 * 1000);
// Comment the job processing statement above, and uncomment one of the blocks below

/*
/*
// Imagine a job that takes 8 seconds. That is longer than the lockLifetime, so
// we'll break it into smaller chunks (or set its lockLifetime to a higher value).
await sleep(4 * 1000); // 4000 < lockLifetime of 5000, so the job still has time to finish
await job.touch(); // tell Agenda the job is still running, which resets the lock timeout
await sleep(4 * 1000); // do another chunk of work that takes less than the lockLifetime
*/

// Only one job will run at a time because 3000 < lockLifetime
// await sleep(3 * 1000);
// Only one job will run at a time because 3000 < lockLifetime
// await sleep(3 * 1000);

console.log(`#${thisJob} finished`);
done();
});
console.log(`#${thisJob} finished`);
done();
}
);

(async function() {
console.log(time(), 'Agenda started');
Expand All @@ -55,10 +59,10 @@ agenda.define('long-running job', {
await agenda.every('1 second', 'long-running job');

// Log job start and completion/failure
agenda.on('start', job => {
agenda.on('start', (job) => {
console.log(time(), `Job <${job.attrs.name}> starting`);
});
agenda.on('success', job => {
agenda.on('success', (job) => {
console.log(time(), `Job <${job.attrs.name}> succeeded`);
});
agenda.on('fail', (error, job) => {
Expand Down
16 changes: 8 additions & 8 deletions lib/agenda/cancel.ts
@@ -1,8 +1,8 @@
import { Agenda } from '.';
import createDebugger from 'debug';
import { FilterQuery } from 'mongodb';
import { Agenda } from ".";
import createDebugger from "debug";
import { FilterQuery } from "mongodb";

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

/**
* Cancels any jobs matching the passed MongoDB query, and removes them from the database.
Expand All @@ -11,14 +11,14 @@ const debug = createDebugger('agenda:cancel');
* @param FilterQuery<any> query MongoDB query to use when cancelling
* @caller client code, Agenda.purge(), Job.remove()
*/
export const cancel = async function(this: Agenda, query: FilterQuery<any>) {
debug('attempting to cancel all Agenda jobs', query);
export const cancel = async function (this: Agenda, query: FilterQuery<any>) {
debug("attempting to cancel all Agenda jobs", query);
try {
const { result } = await this._collection.deleteMany(query);
debug('%s jobs cancelled', result.n);
debug("%s jobs cancelled", result.n);
return result.n;
} catch (error: unknown) {
debug('error trying to delete jobs from MongoDB');
debug("error trying to delete jobs from MongoDB");
throw error;
}
};
17 changes: 10 additions & 7 deletions lib/agenda/close.ts
@@ -1,7 +1,7 @@
import { Agenda } from '.';
import createDebugger from 'debug';
import { Agenda } from ".";
import createDebugger from "debug";

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

/** Close the db and it's underlying connections
* Only works if agenda was instantiated without preconfigured mongoDb instance.
Expand All @@ -14,11 +14,14 @@ 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 }): Promise<Agenda> {
debug('close db connection for this agenda instance');
export const close = async function (
this: Agenda,
option?: { force: boolean }
): Promise<Agenda> {
debug("close db connection for this agenda instance");
const closeOptions = {
force: false,
...option
...option,
};
try {
if (this._db) {
Expand All @@ -27,7 +30,7 @@ export const close = async function(this: Agenda, option?: { force: boolean }):

return this;
} catch (error: unknown) {
debug('error trying to close db connection to');
debug("error trying to close db connection to");
throw error;
}
};
18 changes: 10 additions & 8 deletions lib/agenda/create.ts
@@ -1,8 +1,8 @@
import createDebugger from 'debug';
import { Job } from '../job';
import { Agenda } from '.';
import createDebugger from "debug";
import { Job } from "../job";
import { Agenda } from ".";

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

/**
* Given a name and some data, create a new job
Expand All @@ -11,9 +11,11 @@ const debug = createDebugger('agenda:create');
* @param name name of job
* @param data data to set for job
*/
export const create = function(this: Agenda, name: string, data: any): Job {
debug('Agenda.create(%s, [Object])', name);
const priority = this._definitions[name] ? this._definitions[name].priority : 0;
const job = new Job({ name, data, type: 'normal', priority, agenda: this });
export const create = function (this: Agenda, name: string, data: any): Job {
debug("Agenda.create(%s, [Object])", name);
const priority = this._definitions[name]
? this._definitions[name].priority
: 0;
const job = new Job({ name, data, type: "normal", priority, agenda: this });
return job;
};
41 changes: 26 additions & 15 deletions lib/agenda/database.ts
@@ -1,9 +1,9 @@
import { Collection, MongoClient, MongoClientOptions } from 'mongodb';
import createDebugger from 'debug';
import { hasMongoProtocol } from './has-mongo-protocol';
import { Agenda } from '.';
import { Collection, MongoClient, MongoClientOptions } from "mongodb";
import createDebugger from "debug";
import { hasMongoProtocol } from "./has-mongo-protocol";
import { Agenda } from ".";

const debug = createDebugger('agenda:database');
const debug = createDebugger("agenda:database");

/**
* Connect to the spec'd MongoDB server and database.
Expand All @@ -20,27 +20,35 @@ const debug = createDebugger('agenda:database');
* or use Agenda.mongo(). If your app already has a MongoDB connection then use that. ie. specify config.mongo in
* the constructor or use Agenda.mongo().
*/
export const database = function(this: Agenda, url: string, collection: string, options: MongoClientOptions, cb?: (error: Error, collection: Collection<any> | null) => void) {
export const database = function (
this: Agenda,
url: string,
collection: string,
options: MongoClientOptions,
cb?: (error: Error, collection: Collection<any> | null) => void
) {
if (!hasMongoProtocol(url)) {
url = 'mongodb://' + url;
url = "mongodb://" + url;
}

if (options?.useUnifiedTopology === undefined) {
options = { ...{ useUnifiedTopology: true }, ...options };
}

const reconnectOptions = options?.useUnifiedTopology ? {} : {
autoReconnect: true,
reconnectTries: Number.MAX_SAFE_INTEGER,
reconnectInterval: this._processEvery
};
const reconnectOptions = options?.useUnifiedTopology
? {}
: {
autoReconnect: true,
reconnectTries: Number.MAX_SAFE_INTEGER,
reconnectInterval: this._processEvery,
};

collection = collection || 'agendaJobs';
collection = collection || "agendaJobs";
options = { ...reconnectOptions, ...options };

MongoClient.connect(url, options, (error, client) => {
if (error) {
debug('error connecting to MongoDB using collection: [%s]', collection);
debug("error connecting to MongoDB using collection: [%s]", collection);
if (cb) {
cb(error, null);
} else {
Expand All @@ -50,7 +58,10 @@ export const database = function(this: Agenda, url: string, collection: string,
return;
}

debug('successful connection to MongoDB using collection: [%s]', collection);
debug(
"successful connection to MongoDB using collection: [%s]",
collection
);
this._db = client;
this._mdb = client.db();
this.db_init(collection, cb);
Expand Down

0 comments on commit 234fff0

Please sign in to comment.