schmittjoh / JMSJobQueueBundle

Run and Schedule Symfony Console Commands as Background Jobs
http://jmsyst.com/bundles/JMSJobQueueBundle
335 stars 255 forks source link

Add a «Progress» field for jobs #200

Closed chindit closed 5 years ago

chindit commented 6 years ago

When running long jobs, it's cool to have an idea of the command's progress.

So, with a new progress field in Job table, we're able to set progression in the run command by using jms-job-id argument.

For example, a command could look like this:

protected function execute(InputInterface $input, OutputInterface $output)
{
       // ……
       // Do stuff
       // ……
        $argument = $input->getOption('jms-job-id');
        $job = $this->entityManager->getRepository(Job::class)->find($argument);
        $job->setProgress(0.3);
        $this->entityManager->flush($job);
        // ……
        // Do other stuff
        // ……
        $job->setProgress(0.6);
        $this->entityManager->flush($job);

        // Etc
        // ……
}

Of course, when a job is finished, its progress is automatically set to 1. If job fails or is terminated without success, its last progress is kept.

schmittjoh commented 6 years ago

Could you share a bit more background how this is used? Can you keep the progress in the entity that the command is working on?

chindit commented 6 years ago

When running long commands, I need to get the progression of this command. Since there can be many commands working on many entities, it makes sense to set the progress on the job itself because it's the job which is progressing towards its completion, not the entit(y/ies) on which the job may work.

Given any command receive its jms-job-id, is easy to update the progress from any command simply by retrieving and updating job entity.

With the progress, we have a single point of truth for command progress: the job.

olix21 commented 6 years ago

Hey! Interesting feature, I also have some "long duration jobs", it sounds like an interesting idea to know "where"the job is in its work.

But why a float, not an integer? From 0 -> 100

For now I'm trying to set the computation in the entity the command is working on but it's more a workaround. I need this because my frontend have a progress bar, but the job has no idea of its progress

In general, I feel a little stuck with this architecture. It could be great to let every dev extend this job class to be more accurated with what they want from their jobs. But a progression is already a nice feature to have.

olix21 commented 6 years ago

Hey! Any news? What about this PR?

schmittjoh commented 6 years ago

For the moment, I would consider this outside the scope of this bundle.

As an alternative, you could either use the entity you are working on, or build something more complex around the Job class that is re-usable - I just do not want to add something like this to this bundle at the moment.

olix21 commented 6 years ago

Yeah but extending your entity is very hard because it's not a doctrine super mapped entity, and there is no interface we can extend or working with. The only way for me is to make a fork but I dislike this idea too

schmittjoh commented 5 years ago

How about adding a JSON field where users can store arbitrary metadata? I think that should cover this use-case?

olix21 commented 5 years ago

Why don't you want to work with an interface. With doctrine mapping? we could use any entity of ours to extends your model

schmittjoh commented 5 years ago

I was suggesting to add this field to the entity of the bundle.

olix21 commented 5 years ago

But why adding a field when we can provide an interface. It's more flexible this way and you can let everyone extends your job entity

schmittjoh commented 5 years ago

I think that would add a lot of complexity to this bundle and also to users of the bundle. Do you have some code that shows what you propose?

schmittjoh commented 5 years ago

I'm closing this here, but we could still have some general "metadata" field if someone wants to propose that.