Skip to content
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

Allow scheduling of job execution in queues #3004

Open
dfernandesbsolus opened this issue Aug 9, 2024 · 5 comments
Open

Allow scheduling of job execution in queues #3004

dfernandesbsolus opened this issue Aug 9, 2024 · 5 comments

Comments

@dfernandesbsolus
Copy link
Contributor

dfernandesbsolus commented Aug 9, 2024

Is your feature request related to a problem? Please describe.
Sometimes it may be necessary to define an execution date or a delay (in the case of Redis) for executing a job, for example we can allow the publish or unpublish a product automatically, it is not ideal to create a cron that will do a findAll every minute.

Describe the solution you'd like

  • Create a field (readyAt) in the job record that allows you to say when the job is available for execution.
  • Create a method that allows, using a known and dynamic key, for example product_id, to delete, cancel or update a job that is scheduled. For example, if a user has scheduled the publication of a product, a new job will be created, with readyAt filled in with the date chosen by the user. However, if for some reason the user decides to reschedule for another date, it must be possible to change the schedule. There must be an efficient way to search for this job to modify it.
import { JobState } from '@vendure/common/lib/generated-types';
import { DeepPartial } from '@vendure/common/lib/shared-types';
import { Column, Entity, Index } from 'typeorm';

import { VendureEntity } from '../../entity/base/base.entity';

@Entity()
export class JobRecord extends VendureEntity {
    constructor(input: DeepPartial<JobRecord>) {
        super(input);
    }

    @Column()
    queueName: string;

    @Index()
    @Column('varchar')
    key: string; // known identifier

    @Column('simple-json', { nullable: true })
    data: any;

    @Column('varchar')
    state: JobState;

    @Column()
    progress: number;

    @Column('simple-json', { nullable: true })
    result: any;

    @Column({ nullable: true })
    error: string;

    @Column({ nullable: true, precision: 6 })
    startedAt?: Date;

    @Column({ nullable: true, precision: 6 })
    settledAt?: Date;

    @Column()
    isSettled: boolean;

    @Column()
    retries: number;

    @Column({ nullable: true })
    readyAt?: Date; // date when available for execution

    @Column()
    attempts: number;
}

Example of adding a job

this.jobQueue.add(key, {productId: product.id}, {retries: 2, readyAt: new Date('2024-08-08 23:46:00')});

Describe alternatives you've considered
N/A

Additional context
This process can be used in several situations and in several strategies, such as Sql, in memory, redis (delay), Rabbit MQ.

@dfernandesbsolus dfernandesbsolus changed the title Allows you to set the execution date for jobs in queues. Allows to set the execution date for jobs in queues. Aug 9, 2024
@dfernandesbsolus dfernandesbsolus changed the title Allows to set the execution date for jobs in queues. Allow scheduling of job execution in queues Aug 9, 2024
@dfernandesbsolus
Copy link
Contributor Author

@michaelbromley I've already done this development locally, I can send a PR to evaluate if you think it's an interesting feature for Vendure.

@michaelbromley
Copy link
Member

Hi, you are welcome to open a PR (you can mark as a draft if you like) so I can review your implementation.

@mschipperheyn
Copy link
Collaborator

What I think any scheduled execution should also include is a "last cookie counts" style functionality by default. If the same job is created 10 times with the same job signature any job that didn't run yet except the last one should be dropped.

@dlhck
Copy link
Collaborator

dlhck commented Sep 24, 2024

@dfernandesbsolus could you provide the PR and link it to this issue?

@dlhck
Copy link
Collaborator

dlhck commented Sep 24, 2024

To delay the execution of a job I would rather opt for the implementation approach of Symfony. They have introduced something like stamps: https://symfony.com/doc/current/messenger.html#envelopes-stamps

@dlhck dlhck moved this to 👀 Under consideration in Vendure OS Roadmap Sep 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: 👀 Under consideration
Development

No branches or pull requests

4 participants