-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
119 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Mail } from '@athenna/mail' | ||
import type { User } from '#src/models/user' | ||
|
||
type Item = { | ||
user: User | ||
} | ||
|
||
export class UserConfirmJob { | ||
public static queue() { | ||
return 'user:confirm' | ||
} | ||
|
||
public async handle({ user }: Item) { | ||
await Mail.from('[email protected]') | ||
.to(user.email) | ||
.subject('Athenna Account Confirmation') | ||
.view('mail/confirm', { user }) | ||
.send() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Mail } from '@athenna/mail' | ||
import type { User } from '#src/models/user' | ||
|
||
type Item = { | ||
user: User | ||
email: string | ||
token: string | ||
} | ||
|
||
export class UserEmailJob { | ||
public static queue() { | ||
return 'user:email' | ||
} | ||
|
||
public async handle({ user, email, token }: Item) { | ||
await Mail.from('[email protected]') | ||
.to(user.email) | ||
.subject('Athenna Email Change') | ||
.view('mail/change-email', { user, email, token }) | ||
.send() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Mail } from '@athenna/mail' | ||
import type { User } from '#src/models/user' | ||
|
||
type Item = { | ||
user: User | ||
email: string | ||
password: string | ||
token: string | ||
} | ||
|
||
export class UserEmailPasswordJob { | ||
public static queue() { | ||
return 'user:email:password' | ||
} | ||
|
||
public async handle({ user, email, password, token }: Item) { | ||
await Mail.from('[email protected]') | ||
.to(user.email) | ||
.subject('Athenna Email & Password Change') | ||
.view('mail/change-email-password', { user, email, password, token }) | ||
.send() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Mail } from '@athenna/mail' | ||
import type { User } from '#src/models/user' | ||
|
||
type Item = { | ||
user: User | ||
password: string | ||
token: string | ||
} | ||
|
||
export class UserPasswordJob { | ||
public static queue() { | ||
return 'user:password' | ||
} | ||
|
||
public async handle({ user, password, token }: Item) { | ||
await Mail.from('[email protected]') | ||
.to(user.email) | ||
.subject('Athenna Password Change') | ||
.view('mail/change-password', { user, password, token }) | ||
.send() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,46 @@ | ||
import { Mail } from '@athenna/mail' | ||
import { Log } from '@athenna/logger' | ||
import { ServiceProvider } from '@athenna/ioc' | ||
import { Exec, Module } from '@athenna/common' | ||
import { Queue } from '#src/providers/facades/queue' | ||
|
||
export default class QueueWorkerProvider extends ServiceProvider { | ||
public intervals = [] | ||
|
||
public async boot() { | ||
this.processByQueue('user:confirm', async user => { | ||
return Mail.from('[email protected]') | ||
.to(user.email) | ||
.subject('Athenna Account Confirmation') | ||
.view('mail/confirm', { user }) | ||
.send() | ||
}) | ||
|
||
this.processByQueue('user:email', async ({ user, token, email }) => { | ||
return Mail.from('[email protected]') | ||
.to(user.email) | ||
.subject('Athenna Email Change') | ||
.view('mail/change-email', { user, email, token }) | ||
.send() | ||
}) | ||
const jobs = Config.get<string[]>('rc.jobs', []) | ||
|
||
this.processByQueue('user:password', async ({ user, token, password }) => { | ||
return Mail.from('[email protected]') | ||
.to(user.email) | ||
.subject('Athenna Password Change') | ||
.view('mail/change-password', { user, password, token }) | ||
.send() | ||
}) | ||
await Exec.concurrently(jobs, async jobPath => { | ||
const Job = await Module.resolve(jobPath, import.meta.url) | ||
const alias = `App/Jobs/${Job.name}` | ||
|
||
this.processByQueue( | ||
'user:email:password', | ||
async ({ user, token, email, password }) => { | ||
return Mail.from('[email protected]') | ||
.to(user.email) | ||
.subject('Athenna Email & Password Change') | ||
.view('mail/change-email-password', { user, email, password, token }) | ||
.send() | ||
} | ||
) | ||
} | ||
const queueName = Job.queue() | ||
const job = this.container.transient(Job, alias).use(alias) | ||
|
||
public async shutdown() { | ||
this.intervals.forEach(interval => clearInterval(interval)) | ||
} | ||
const interval = setInterval(async () => { | ||
const queue = Queue.queue(queueName) | ||
|
||
public processByQueue( | ||
queueName: string, | ||
processor: (data: any) => any | Promise<any> | ||
) { | ||
const interval = setInterval(async () => { | ||
const queue = Queue.queue(queueName) | ||
if (queue.isEmpty()) { | ||
return | ||
} | ||
|
||
if (queue.isEmpty()) { | ||
return | ||
} | ||
Log.info(`Processing jobs of ({yellow} "${queueName}") queue`) | ||
|
||
Log.info(`Processing jobs of ({yellow} "${queueName}") queue`) | ||
await queue.process(job.handle.bind(job)) | ||
|
||
await queue.process(processor) | ||
const jobsLength = queue.length() | ||
|
||
const jobsLength = queue.length() | ||
if (jobsLength) { | ||
Log.info( | ||
`Still has ({yellow} ${jobsLength}) jobs to process on ({yellow} "${queueName}")` | ||
) | ||
} | ||
}, 5000) | ||
|
||
if (jobsLength) { | ||
Log.info( | ||
`Still has ({yellow} ${jobsLength}) jobs to process on ({yellow} "${queueName}")` | ||
) | ||
} | ||
}, 5000) | ||
this.intervals.push(interval) | ||
}) | ||
} | ||
|
||
this.intervals.push(interval) | ||
public async shutdown() { | ||
this.intervals.forEach(interval => clearInterval(interval)) | ||
} | ||
} |