mmucklo / DtcQueueBundle

Symfony2/3/4/5 Queue Bundle (for background jobs) supporting Mongo (Doctrine ODM), Mysql (and any Doctrine ORM), RabbitMQ, Beanstalkd, Redis, and ... {write your own}
MIT License
120 stars 38 forks source link

[Question/Bug] About error catching #51

Closed Gounlaf closed 6 years ago

Gounlaf commented 6 years ago

Hi @mmucklo,

I'm encountering a weird issue: when an error happen, not "all the stack" is logged. As example, I have an issue with a doctrine entity, which prevent it to be persisted correctly.

If I catch and dump manually the exception, I got full details.

But in logs, I got only:

[2018-01-25 22:14:12] console.ERROR: Error thrown while running command "dtc:queue:run myjobname -i jobid". Message: "The EntityManager is closed." {"error":"[object] (Doctrine\ORM\ORMException(code: 0): The EntityManager is closed. at /path/to/my/project/vendor/doctrine/orm/lib/Doctrine/ORM/ORMException.php:144)","command":"dtc:queue:run myjobname -i jobid","message":"The EntityManager is closed."} []

Everything is rollback, entity manager is closed, so the job status is not set to "exception".

If I don't "logs" manually the exception, I can't understand why the EM was closed.

Have you any idea how to log "everything" in the system logs? (I don't know if "you" are logging it, or if it's the symfony console component that handle it itself.) It seems that an EM can't be re-opened when it was closed: I can create another EM so there is one "independent" for the workers, and another for the "job" itself. But I don't know if it's a good idea.

If I'm not under-stable... I will try to explain it better =/

Thanks,

Regards.

mmucklo commented 6 years ago

@Gounlaf - Sorry for the delay in reply.

If there's an exception it can sometimes close the entity manager and it has to be reopened to continue. Is it your code causing the exception or the queue itself?

Gounlaf commented 6 years ago

Hi @mmucklo,

Don't worry for the delay :)

Is it your code causing the exception or the queue itself? It's my code! And yup, I got a problem with my entity; trying to flush it cause the em to be closed. Since it's the same em as the queue (the "default" one), the queue is not updated.

So... I saw "nothing", and the only error logged was "the em is closed".

Edit: issue closed by missclick ><

mmucklo commented 6 years ago

@Gounlaf - I suppose there’s a trick I could do to see if the Entity Manager was closed and then try to reopen it. The only problem is it might mask other issues - but it may help your problem...

Gounlaf commented 6 years ago

The only problem is it might mask other issues

@mmucklo What do you mean?

Gounlaf commented 6 years ago

I can create another EM so there is one "independent" for the workers, and another for the "job" itself. But I don't know if it's a good idea.

Btw, since EM is defined in config (dtc_queue[orm][entity_manager]=default), Now I think I should use two differents EM: one for the queue (that will have no reason to be closed), another one the job itself. So queue can continue to execute jobs, even if there are error during their execution

mmucklo commented 6 years ago

@Gounlaf - I have a PR open which should provide a fix, regardless.

I will probably merge it later today after trying to write a test for it.

mmucklo commented 6 years ago

@Gounlaf - can you see if 4.4.0 fixes the issue for you?

mmucklo commented 6 years ago

@Gounlaf, any word?

Gounlaf commented 6 years ago

Sorry @mmucklo I haven't tested yet. Will do today evening

Gounlaf commented 6 years ago

@mmucklo It seems to be working as expected :)

I've deliberately create a UniqueConstraintViolationException with my entity, and job status correctly set to "exception".

Thanks for taking care of my request!

mmucklo commented 6 years ago

@Gounlaf, you’re welcome!

mmucklo commented 6 years ago

@Gounlaf, you’re welcome!

kralos commented 6 years ago

Hi all, I just came across this bundle and this issue. I'd like to add my 2 cents.

It's generally a bad idea to mix an application's entity / document manager from doctrine with an ancillary tool such as this queue. The reasons are:

A better solution is to create a separate entity manager or document manager for the queue. This does not mean we need 2 connections. You can share 1 DBAL connection between 2 EntityManagers.

E.g.:

MongoODM config.yml

doctrine_mongodb:
    connections:
        default:
            server: '%mongo_server%'
    default_database: '%mongo_database%'
    document_managers:
        default:
            mappings:
                App:
                    type: annotation
                    dir: '%kernel.project_dir%/src/Document'
                    prefix: App\Document
        queue:
            connection: default
            mappings:
                DtcQueueBundle: ~

dtc_queue:
    odm:
        document_manager: queue
    manager:
        job: odm

DoctrineORM config.yml

doctrine:
    dbal:
        driver:   pdo_mysql
        host:     '%database_host%'
        dbname:   '%database_name%'
        user:     '%database_user%'
        password: '%database_password%'
    orm:
        entity_managers:
            default:
                mappings:
                    App:
                        type: annotation
                        dir: '%kernel.project_dir%/src/Entity'
                        prefix: App\Entity
            queue:
                mappings:
                    DtcQueueBundle: ~

dtc_queue:
    orm:
        entity_manager: queue
    manager:
        job: orm