@qourses/nestjs-pulse
v1.0.2
Published
The modern MongoDB-powered scheduling library pulse for NestJS
Readme
Overview
Pulse module for NestJS, working with nestjs v9.x, v10.x and v11.x.
Getting Started
Installation
npm install --save @pulsecron/nestjs-pulse @pulsecron/pulseExample
app.module.ts
// src/app.module.ts
import { Module } from '@nestjs/common';
import { PulseModule } from '@pulsecron/nestjs-pulse';
import { NotificationsModule } from './notification/notification.module';
@Module({
imports: [
PulseModule.forRoot({
db: {
address: 'mongodb://localhost:27017/pulse',
},
}),
NotificationsModule,
],
providers: [],
})
export class AppModule {}
notification.module.ts
// src/notification/notification.module.ts
import { Module } from '@nestjs/common';
import { PulseModule } from '@pulsecron/nestjs-pulse';
import { NotificationsQueue } from './notification.processor';
@Module({
imports: [
PulseModule.registerQueue('notifications', {
processEvery: '1 minutes',
autoStart: false, // default: true
}),
],
providers: [NotificationsQueue],
exports: [],
})
export class NotificationsModule {}
notification.processor.ts
// src/notification/notification.processor.ts
import { Job } from '@pulsecron/pulse';
import { Every, Queue, Define, Schedule } from '@pulsecron/nestjs-pulse';
@Queue('notifications')
export class NotificationsQueue {
@Every({ name: 'send notifications', interval: '1 minutes' })
async sendNotifications(job: Job) {
console.log('Sending notifications[1]');
}
@Schedule({ name: 'send notifications', when: 'tomorrow at noon' })
async sendNotifications(job: Job) {
console.log('Sending notifications[2]');
}
@Now()
async sendNotifications(job: Job) {
console.log('Sending notifications[3]');
}
@Define({ name: 'emailJob' })
async test(job: Job) {
console.log('Sending email to:', job.data.to);
}
}
notification.service.ts
import { Inject, Injectable } from '@nestjs/common';
import { Pulse } from '@pulsecron/pulse';
@Injectable()
export class NotificationService {
constructor(@Inject('notifications') private pulse: Pulse) {}
async scheduleEmail(email: string) {
const emailJob = this.pulse.create('emailJob', { to: email });
emailJob.unique(
{
'data.to': email,
},
{
insertOnly: true,
}
);
emailJob.schedule('5 seconds').save();
}
}
Development
Platform-Specific Optional Dependencies
If you encounter an error when running npm ci in CI/CD that mentions missing platform-specific packages (e.g., @swc/core-linux-x64-gnu), this is a known npm issue where the package-lock.json was generated on a different platform than your CI environment.
The Problem:
- When
package-lock.jsonis generated on macOS, it only includes macOS-specific optional dependencies - When CI runs on Linux, those Linux packages are missing from the lock file
npm cifails because it requires exact synchronization betweenpackage.jsonandpackage-lock.json
The Solution:
Regenerate your package-lock.json on the same platform as your CI environment (Linux). You can do this using Docker:
docker run --rm -v "$(pwd):/workspace" -w /workspace node:lts npm install --package-lock-onlyThis will regenerate the lock file with all platform-specific optional dependencies included, making it compatible with both your local development environment and CI.
Alternative: If you have access to a Linux machine or VM, you can simply run npm install there and commit the updated package-lock.json.
Contributing
Contributions are welcome! Here are several ways you can contribute:
- Report Issues: Submit bugs found or log feature requests for the
pulseproject. - Submit Pull Requests: Review open PRs, and submit your own PRs.
- Join the Discussions: Share your insights, provide feedback, or ask questions.
Fork the Repository: Start by forking the project repository to your github account.
Clone Locally: Clone the forked repository to your local machine using a git client.
git clone https://github.com/pulsecron/nestjs-pulseCreate a New Branch: Always work on a new branch, giving it a descriptive name.
git checkout -b new-feature-xMake Your Changes: Develop and test your changes locally.
Commit Your Changes: Commit with a clear message describing your updates.
git commit -m 'Implemented new feature x.'Push to github: Push the changes to your forked repository.
git push origin new-feature-xSubmit a Pull Request: Create a PR against the original project repository. Clearly describe the changes and their motivations.
Review: Once your PR is reviewed and approved, it will be merged into the main branch. Congratulations on your contribution!
License
This project is protected under the MIT License. For more details, refer to the LICENSE file.
