Problem creating custom scheduled job

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • knox7
    Junior Member
    • Jun 2021
    • 6

    Problem creating custom scheduled job

    Hi,
    I am trying to create a custom scheduled job, following the guidance on this page: https://github.com/espocrm/documenta...heduled-job.md

    After clearing cache/rebuilding, I can see the new job and add it to the scheduled list, but when it tries to run it, it fails with this error in the logs:

    Code:
    [2021-06-04 08:46:00] ERROR: CronManager: Failed job running, job [60b9e80c42e674ae8]. Error Details: InjectableFactory: Class 'Espo\Custom\Jobs\YourJobName' does not exist. at /var/www/html/application/Espo/Core/InjectableFactory.php:88 [] []
    I have literally copied the exact code from the documentation and put it in the place it says (custom/Espo/Custom/Jobs/YourJobName.php).

    Can someone tell me what else I need to do to get this to work?
  • esforim
    Active Community Member
    • Jan 2020
    • 2204

    #2
    My guess would be that YourJobName does not exist. Documentation is just an example, rarely do they use an actual copy/pastable example.

    Secondly if you copy and paste then there is no "logic" you meant to write what it do in that section.

    Third I don't think you got any entity/field/whatever call Job either.

    To be honest these are advance coding, unless you can and know how to write code you be scratching your heads for hours trying to figure it out. I'm certainly not able to, but just reading that example code I can see what I written above.

    Comment

    • knox7
      Junior Member
      • Jun 2021
      • 6

      #3
      I know I need to add my own logic for the job to actually do something, but when I do I get the same error - I am just trying to get the basic framework in place so I can implement something.

      My guess is that the class (temporarily called 'YourJobName') is not autoloaded properly so the InjectableFactory cannot find it, but I don't know how that autoloading is supposed to work and I cannot find any explanation in the documentation. However, my job does appear in the scheduled job table, so it does apparently exist.

      By the way, I should have mentioned I am using the latest version of Espocrm (6.1.7)

      Comment

      • esforim
        Active Community Member
        • Jan 2020
        • 2204

        #4
        Perhaps if you can post your code and someone with experience might be able to point you in the right direction. Since you're using the (almost) latest version I don't think it is a compatibility issue with code.

        Comment

        • knox7
          Junior Member
          • Jun 2021
          • 6

          #5
          My code is exactly the same as in the documentation: https://github.com/espocrm/documenta...heduled-job.md

          Comment

          • telecastg
            Active Community Member
            • Jun 2018
            • 907

            #6
            The correct url for the custom job is: custom/Espo/Custom/Jobs/YourJobName.php NOT Espo\Custom\Jobs\YourJobName.php which your error message is pointing out, try correcting this and see if it works.

            Comment

            • knox7
              Junior Member
              • Jun 2021
              • 6

              #7
              I did define the job in custom/Espo/Custom/Jobs/YourJobName.php (as described in the documentation) and it is available in the scheduled tasks list, so I can't see how that could be wrong.

              My guess is that it is something to do with how the PHP classes are loaded so that the InjectableFactory knows about them - if there is anyone out there who has successfully created a custom job or something similar that gets created by the InjectableFactory, I'd really appreciate any other pointers to anything else not mentioned on that documentation page.

              Note that if I put the job into the official Espo folder: application/Espo/Jobs/ then it runs as expected, so it is something to do with how it discovers custom classes that is not working for me.
              Last edited by knox7; 06-09-2021, 08:35 PM.

              Comment

              • telecastg
                Active Community Member
                • Jun 2018
                • 907

                #8
                I tried this (I also have version 6.1.7) and it works:

                1) Created script: custom/Espo/Custom/Jobs/YourJobName.php
                PHP Code:
                <?php
                
                namespace Espo\Custom\Jobs;
                
                use Espo\Core\Jobs\Job;
                
                class YourJobName implements Job
                {
                // Pass dependencies through the constructor using DI.
                public function __construct()
                {
                }
                
                public function run() : void
                {
                // Write your logic here.
                $GLOBALS['log']->warning('YourJobName.php executed ');
                
                }
                }
                2) Clear cache and rebuild from CLI

                Code:
                php rebuild.php
                3) Run job from CLI
                Code:
                php command.php run-job YourJobName
                4) Check your log file and you will se this:
                data/logs/espo-2021-06-10.log
                Click image for larger version  Name:	Log Capture.PNG Views:	0 Size:	3.7 KB ID:	71505
                Last edited by telecastg; 06-10-2021, 05:19 AM.

                Comment

                • knox7
                  Junior Member
                  • Jun 2021
                  • 6

                  #9
                  Thank you for trying that - when I run the job from the CLI it works for me too! I had not tried it that way.

                  Code:
                  [2021-06-10 17:23:18] WARNING: Custom job running! [] []
                  But unfortunately, when I set the scheduling to run it every minute so it runs from the Cron manager, it fails:

                  Code:
                  [2021-06-10 17:26:00] ERROR: CronManager: Failed job running, job [60c24aec723b47c96]. Error Details: InjectableFactory: Class 'Espo\Custom\Jobs\YourJobName' does not exist. at /var/www/html/application/Espo/Core/InjectableFactory.php:88 [] []
                  [2021-06-10 17:29:00] ERROR: CronManager: Failed job running, job [60c24ba066b7d13bb]. Error Details: InjectableFactory: Class 'Espo\Custom\Jobs\YourJobName' does not exist. at /var/www/html/application/Espo/Core/InjectableFactory.php:88 [] []
                  [2021-06-10 17:32:00] ERROR: CronManager: Failed job running, job [60c24c5466781c413]. Error Details: InjectableFactory: Class 'Espo\Custom\Jobs\YourJobName' does not exist. at /var/www/html/application/Espo/Core/InjectableFactory.php:88 [] []
                  So I wonder if there is a different class loading mechanism/context for the scheduler, and how to update that to include my custom job?

                  Comment

                  • telecastg
                    Active Community Member
                    • Jun 2018
                    • 907

                    #10
                    Sorry, I am not familiar with the CronManager, maybe Maximus can help with that or maybe you can figure it out checking the class code.

                    In my installation (6.1.7) the script is located at application/Espo/Core/CronManager.php but I couldn't find it in the Github repository ??????? (not sure why)

                    Comment

                    • item
                      Active Community Member
                      • Mar 2017
                      • 1476

                      #11
                      Hello,
                      have you add Job here :

                      custom/Espo/Custom/Resources/i18n/en_US/ScheduledJob.json ?
                      clear_cache
                      Then have you create a Job .. and set inactive ?

                      I do always so.

                      edit : my job : class MyNewJob extends \Espo\Core\Jobs\Base
                      Last edited by item; 06-10-2021, 09:37 PM.
                      If you could give the project a star on GitHub. EspoCrm believe our work truly deserves more recognition. Thanks.​

                      Comment

                      • telecastg
                        Active Community Member
                        • Jun 2018
                        • 907

                        #12
                        item is correct, as usual , that is how it is instructed in the "published" documentation.

                        The example that you and I were referring to, is in the latest but unpublished Github documentation.

                        This is the link to the "official published documentation" for custom jobs: https://docs.espocrm.com/development/scheduled-job/

                        Comment

                        • knox7
                          Junior Member
                          • Jun 2021
                          • 6

                          #13
                          I'm afraid that using
                          Code:
                          class YourJobName extends \Espo\Core\Jobs\Base
                          instead of
                          Code:
                          use Espo\Core\Jobs\Job;
                          
                          class YourJobName implements Job
                          gives me the same error.

                          I can run the job on the CLI, and I can add it to the Scheduled Jobs in the GUI (I set the schedule as */1 * * * * to run every minute) but when it runs through the Cron manager it gives the error from InjectableFactory that I posted above.

                          [Note that I want it to be Active so that it will run automatically by cron.]

                          Comment

                          Working...