Skip to content

List all jobs and remove all jobs #83

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/content/job-scheduler/0.installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ jobs.scheduleJob({
jobs.scheduleJob({
id: 'job2',
type: 'interval',
interval: DAY, // Runs every 24 hours
duration: 2, // Runs every 2 minutes
execute: () => {
console.log('Executed job on interval');
},
Expand Down
82 changes: 82 additions & 0 deletions packages/job-scheduler/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,88 @@ describe('defineJobScheduler', () => {
});
});

describe('removeAllJobs', () => {
it('no more jobs should exist once called', async () => {
const minutes = 2;
const jobs_to_schedule = [
{
id: 'once',
type: 'once' as const,
date: Date.now() + 60e3,
execute: vi.fn(),
},
{
id: 'interval',
type: 'interval' as const,
duration: minutes * 60e3,
immediate: true,
execute: vi.fn(),
},
{
id: 'cron',
type: 'cron' as const,
expression: '0 0/2 * * *', // every 2 hours on the 0th minute of the hour
execute: vi.fn(),
},
];

const jobs = defineJobScheduler({ logger: null });

for (const job of jobs_to_schedule) {
await jobs.scheduleJob(job);
await fakeBrowser.alarms.onAlarm.trigger({ name: job.id, scheduledTime: job.date });
}

await jobs.removeAllJobs();

for (const job of jobs_to_schedule) {
await fakeBrowser.alarms.onAlarm.trigger({ name: job.id, scheduledTime: job.date });
expect(job.execute).toBeCalledTimes(1);
}

const alarm = await fakeBrowser.alarms.getAll();

expect(alarm).toBeUndefined();
});
});

describe('listJobs', () => {
it('should list jobs that exist', async () => {
const minutes = 2;
const jobs_to_schedule = [
{
id: 'once',
type: 'once' as const,
date: Date.now() + 60e3,
execute: vi.fn(),
},
{
id: 'interval',
type: 'interval' as const,
duration: minutes * 60e3,
immediate: true,
execute: vi.fn(),
},
{
id: 'cron',
type: 'cron' as const,
expression: '0 0/2 * * *', // every 2 hours on the 0th minute of the hour
execute: vi.fn(),
},
];

const jobs = defineJobScheduler({ logger: null });

for (const job of jobs_to_schedule) {
await jobs.scheduleJob(job);
}

let allJobs = await jobs.listJobs();

expect(allJobs.length).toEqual(3);
});
});

describe('on', () => {
it('should call success listeners when a job finishes', async () => {
const result = Math.random();
Expand Down
23 changes: 23 additions & 0 deletions packages/job-scheduler/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,31 @@ export interface JobScheduler {
* Schedule a job. If a job with the same `id` has already been scheduled, it will update the job if it is different.
*/
scheduleJob(job: Job): Promise<void>;

/**
* Un-schedules a job by it's ID.
*/
removeJob(jobId: string): Promise<void>;

/**
* Un-schedules all jobs
*/
removeAllJobs(): Promise<void>;

/**
* Listen for a job to finish successfully.
*/
on(event: 'success', callback: (job: Job, result: any) => void): RemoveListenerFn;

/**
* Listen for when a job fails.
*/
on(event: 'error', callback: (job: Job, error: unknown) => void): RemoveListenerFn;

/**
* List all the scheduled jobs.
*/
listJobs(): Promise<Record<Job['id'], Job>>;
}

/**
Expand Down Expand Up @@ -227,6 +239,13 @@ export function defineJobScheduler(options?: JobSchedulerConfig): JobScheduler {
}
}

async function listJobs() {
logger?.debug('Listing jobs');
const alarms = await browser.alarms.getAll();

return alarms.map(alarm => jobs[alarm.name]);
}

/**
* Some jobs need to immediately schedule the next alarm, some don't. This function handles each
* type and calls `scheduleJob` if needed.
Expand Down Expand Up @@ -258,6 +277,10 @@ export function defineJobScheduler(options?: JobSchedulerConfig): JobScheduler {
await browser.alarms.clear(jobId);
},

async removeAllJobs() {
await browser.alarms.clearAll();
},

on(event, callback) {
const listeners = event === 'success' ? successListeners : errorListeners;
listeners.push(callback);
Expand Down