Skip to content

Commit

Permalink
feat: allow synchronizing job stores manually
Browse files Browse the repository at this point in the history
BREAKING CHANGE: synchronize won't be running automatically
  • Loading branch information
yujiosaka committed Oct 29, 2023
1 parent 44e37a5 commit 5b7b26b
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 4 deletions.
70 changes: 70 additions & 0 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,24 @@ static connect(url: string, options: MongoClientOptions): Promise<MongodbJobStor
- `url`: [string] - MongoDB connection URL.
- `options`: [MongoClientOptions] - Options for MongoDB connection.

#### Methods

##### `sync`

Create indexes on joblocks collection manually.

```ts
sync(): Promise<Job>
```

##### `close`

Close connection to the MongoDB server

```ts
close(): Promise<Job>
```

### RedisJobStore Class

Connect to a Redis server.
Expand All @@ -131,6 +149,24 @@ static connect(options: RedisClientOptions): Promise<RedisJobStore>

- `options`: [RedisClientOptions] - Options for Redis connection.

#### Methods

##### `sync`

Do nothing. Provided for the consistency with other job stores.

```ts
sync(): Promise<Job>
```

##### `close`

Close connection to the Redis server

```ts
close(): Promise<Job>
```

### MysqlJobStore Class

Connect to a MySQL database.
Expand All @@ -145,6 +181,24 @@ static connect(options: MysqlConnectionOptions): Promise<MysqlJobStore>

- `options`: [MysqlConnectionOptions] - Options for MySQL connection.

#### Methods

##### `sync`

Create joblocks table and indexes manually.

```ts
sync(): Promise<Job>
```

##### `close`

Close connection to the MySQL server

```ts
close(): Promise<Job>
```

### PostgresJobStore Class

Connect to a PostgreSQL database.
Expand All @@ -159,6 +213,22 @@ static connect(options: PostgresConnectionOptions): Promise<PostgresJobStore>

- `options`: [PostgresConnectionOptions] - Options for PostgreSQL connection.

##### `sync`

Create joblocks table and indexes manually.

```ts
sync(): Promise<Job>
```

##### `close`

Close connection to the Postgres server

```ts
close(): Promise<Job>
```

[null]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/null "null"
[Array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array "Array"
[boolean]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type "Boolean"
Expand Down
2 changes: 2 additions & 0 deletions examples/job-stores/custom-job-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export default class MemoryJobStore implements BaseJobStore<string> {
return new MemoryJobStore();
}

async sync(): Promise<void> {}

async close(): Promise<void> {}

async fetchLastJobLock(jobName: string): Promise<BaseJobLock<string> | null> {
Expand Down
2 changes: 2 additions & 0 deletions examples/job-stores/mysql-job-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import Cronyx, { MysqlJobStore } from "../../src";

const jobStore = await MysqlJobStore.connect({ type: "mysql", url: Bun.env.MYSQL_URI });
// Syncronize joblocks table manually
await jobStore.sync();
const cronyx = new Cronyx({ jobStore });
await cronyx.requestJobExec(
{
Expand Down
1 change: 1 addition & 0 deletions src/job-store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type BaseJobLock from "../job-lock";
*/
export default interface BaseJobStore<I> {
close(): Promise<void>;
sync(): Promise<void>;
fetchLastJobLock(jobName: string): Promise<BaseJobLock<I> | null>;
activateJobLock(
jobName: string,
Expand Down
8 changes: 6 additions & 2 deletions src/job-store/mongodb.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MongoError } from "mongodb";
import type { MongoClientOptions } from "mongodb";
import type { ConnectOptions } from "mongodb";
import { createConnection } from "mongoose";
import type { Connection, Model, Types } from "mongoose";
import type BaseJobStore from ".";
Expand All @@ -22,12 +22,16 @@ export default class MongodbJobStore implements BaseJobStore<Types.ObjectId> {
this.#model = conn.model<MongodbJobLock>("JobLock", mongodbJobLockSchema);
}

static async connect(url: string, options?: MongoClientOptions): Promise<MongodbJobStore> {
static async connect(url: string, options?: ConnectOptions): Promise<MongodbJobStore> {
const conn = createConnection(url, options);
await conn.asPromise();
return new MongodbJobStore(conn);
}

async sync() {
await this.#conn.syncIndexes();
}

async close() {
await this.#conn.close();
}
Expand Down
4 changes: 4 additions & 0 deletions src/job-store/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ export default class RedisJobStore implements BaseJobStore<string> {
return new RedisJobStore(client);
}

async sync() {
// Do nothing
}

async close() {
await this.#client.quit();
}
Expand Down
4 changes: 4 additions & 0 deletions src/job-store/typeorm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export default abstract class TypeormJobStore implements BaseJobStore<string> {
this.#repository = dataSource.getRepository<TypeormJobLock>(TypeormJobLockEntity);
}

async sync() {
await this.#dataSource.synchronize();
}

async close() {
await this.#dataSource.destroy();
}
Expand Down
1 change: 0 additions & 1 deletion src/job-store/typeorm/mysql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export default class MysqlJobStore extends TypeormJobStore {
protected uniqueConstraintErrorCode: string = "ER_DUP_ENTRY";

static async connect(options: MysqlConnectionOptions | AuroraMysqlConnectionOptions): Promise<TypeormJobStore> {
if (!options.synchronize === false) throw new CronyxArgumentError("Option synchronize should be enabled");
if (options.entities) throw new CronyxArgumentError("Option entities should not be passed");

const dataSource = new DataSource({ ...options, entities: [TypeormJobLockEntity], synchronize: true });
Expand Down
1 change: 0 additions & 1 deletion src/job-store/typeorm/postgres.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export default class PostgresJobStore extends TypeormJobStore {
protected uniqueConstraintErrorCode: string = "23505";

static async connect(options: PostgresConnectionOptions | AuroraPostgresConnectionOptions): Promise<TypeormJobStore> {
if (!options.synchronize === false) throw new CronyxArgumentError("Option synchronize should be enabled");
if (options.entities) throw new CronyxArgumentError("Option entities should not be passed");

const dataSource = new DataSource({ ...options, entities: [TypeormJobLockEntity], synchronize: true });
Expand Down
1 change: 1 addition & 0 deletions test/job-runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ describe("JobRunner", () => {

beforeEach(() => {
jobStore = {
sync: mock(() => Promise.resolve()),
close: mock(() => Promise.resolve()),
fetchLastJobLock: mock((jobName) => {
switch (jobName) {
Expand Down
1 change: 1 addition & 0 deletions test/job.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe.each([[false], [true]])("Job", (noLock) => {

beforeEach(() => {
jobStore = {
sync: mock(() => Promise.resolve()),
close: mock(() => Promise.resolve()),
fetchLastJobLock: mock(() => Promise.resolve(lastJobLock)),
activateJobLock: mock(() => Promise.resolve(activatedJobLock)),
Expand Down

0 comments on commit 5b7b26b

Please sign in to comment.