Cloudron makes it easy to run web apps like WordPress, Nextcloud, GitLab on your server. Find out more or install now.


    Cloudron Forum

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular

    Solved Cron tasks

    WordPress (Developer)
    3
    35
    362
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • mdreira
      mdreira translator last edited by

      Hi!

      I have an image optimization plugin in Wordpress (dev) that needs wp-cron to work. It's not working with Cloudron's default settings. I don´t know why.

      Is it possible that I have to call wp-cron every so often to get it to work? (2 times a day for example)
      How should I do it from the Cron tab of the application?

      Screenshot: https://prnt.sc/26q7kaa

      Thank you.

      girish 1 Reply Last reply Reply Quote 0
      • girish
        girish Staff @mdreira last edited by

        @mdreira wp-cron is automatically configured and you don't have to run/configure things manually. The cron section is only meant for any additional cron tasks that you want to run which are (often) unrelated to the app. You can manually run /app/pkg/cron.sh via the Web Terminal and check if the cron tasks are running or not. Can you please try?

        BTW, feel free to post those images inline here. Those links often lead me to some captcha links and hard to get the context for future readers.

        mdreira 1 Reply Last reply Reply Quote 0
        • Moved from Support by  girish girish 
        • mdreira
          mdreira translator @girish last edited by

          @girish Screeshot: https://prnt.sc/26qbnex

          girish 1 Reply Last reply Reply Quote 0
          • girish
            girish Staff @mdreira last edited by

            @mdreira right, that means that the app has no scheduled events. Is the plugin scheduling any event? Not a WP expert, but you also check with various cron commands like this:

            sudo -u www-data -i -- /app/pkg/wp --path=/app/data/public/ cron
            

            See https://developer.wordpress.org/cli/commands/cron/ for subcommands. For example, https://developer.wordpress.org/cli/commands/cron/event/

            1 Reply Last reply Reply Quote 1
            • girish
              girish Staff last edited by

              So, I think the command below will list the cron events and schedules. So, if the plugin scheduled it, it will appear below.

              sudo -u www-data -i -- /app/pkg/wp --path=/app/data/public/ cron event list
              
              mdreira d19dotca 3 Replies Last reply Reply Quote 1
              • mdreira
                mdreira translator @girish last edited by

                @girish It seems to me that the plugin task does not appear in the list.

                I'm going to discuss this with the plugin folks. I'll tell you something when they answer me.

                Thank you!

                1 Reply Last reply Reply Quote 0
                • mdreira
                  mdreira translator @girish last edited by mdreira

                  @girish Yes, the task was on the list.

                  The plugin people ask me what is the code with which Cloudron starts wp-cron to see if it is compatible.

                  EDIT: I have seen the error. Screenshot: https://prnt.sc/26qc3mk

                  1 Reply Last reply Reply Quote 0
                  • d19dotca
                    d19dotca @girish last edited by d19dotca

                    @girish Side note but FWIW, it may be worth updating/clarifying the documentation here: https://docs.cloudron.io/apps/wordpress-developer/#cron-tasks

                    The doc saying "The app is configured to run WordPress cron tasks every minute." seems a bit conflicted with the statement later that reads "WordPress' built-in cron task schedule wp-cron is disabled since it is not effective for low traffic websites."

                    I believe we're running our own cron tasks in Cloudron which manually invokes the WP-Cron every minute instead of the default behaviour of WP running it itself only when the website is visited, correct?

                    --
                    Dustin Dauncey
                    www.d19.ca

                    girish 1 Reply Last reply Reply Quote 0
                    • girish
                      girish Staff @d19dotca last edited by

                      @d19dotca Right... wp-cron with the hyphen refers to the file wp-cron.php which is this PHP code that runs WP cron jobs via apache. Of course, PHP code only runs if someone visits the web site. So, if no one visits your site for an hour, cron jobs are not run. So, on Cloudron (and most other WP hosts), we set up an external scheduler/timer to run the cron jobs.

                      I guess what is being said is The app is configured to run WordPress' cron tasks every minute without using the built-in wp-cron

                      mdreira 1 Reply Last reply Reply Quote 1
                      • mdreira
                        mdreira translator @girish last edited by mdreira

                        @girish have you seen anything in error log? https://prnt.sc/26qc3mk

                        EDIT: The plugin guys tell us that the problem is the way we call to wp-cron.

                        So even though Cloudron calls wp-cron every minute, I would like to call it twice a day more with the typical type of call from the cron tab. Something like:

                        0 */12 * * * /usr/bin/php -q /public/wp-cron.php >/dev/null 2

                        This is the type of call that the plugin needs. It's possible?

                        We are interested in this image optimization plugin because it optimizes images in the cloud without a limit of megabytes. I have to try to make it work.

                        If you know of another plugin that does the same and works on cloudron, we can switch.

                        girish 1 Reply Last reply Reply Quote 0
                        • girish
                          girish Staff @mdreira last edited by girish

                          @mdreira We use WP CLI to run cron jobs like so:

                          sudo -u www-data -i -- /app/pkg/wp --skip-themes --path=/app/data/public/ cron event run --due-now
                          

                          Are you able to run the command above manually and see if it produces the error message ? Also, remove the --skip-themes and check if that helps as well, maybe that's causing the problem. TBH, I am not 100% sure why we have --skip-themes there, it should probably be removed.

                          mdreira 1 Reply Last reply Reply Quote 0
                          • mdreira
                            mdreira translator @girish last edited by mdreira

                            @girish Here I send you the screenshot of the terminal after sending those commands:

                            Captura de pantalla 2022-02-09 a las 21.20.31.jpg

                            Running it like this did not make the plugin work.

                            The plugin cron task is this:

                            Captura de pantalla 2022-02-09 a las 21.20.42.jpg

                            When the cron executes this task it gives me this error:

                            Captura de pantalla 2022-02-09 a las 21.20.53.jpg

                            Is there any other way to execute that task? I have to get that plugin working. The plugin people don't know what's going on because our cloudron server is something special 😄

                            girish 2 Replies Last reply Reply Quote 0
                            • girish
                              girish Staff @mdreira last edited by

                              @mdreira please post the images inline in this forum. You can just ctrl+v the screenshot to paste in the forum. I am afraid those images will go away at some point and this post will be meaningless if those images go away....

                              1 Reply Last reply Reply Quote 0
                              • girish
                                girish Staff @mdreira last edited by

                                @mdreira Also, can you tell me which plugin this is? From the screenshot it says weimgoptimizer but https://wordpress.org/plugins/search/weimgoptimizer/ is a not found.

                                mdreira 1 Reply Last reply Reply Quote 0
                                • mdreira
                                  mdreira translator @girish last edited by

                                  @girish The plugin is optimizador.io (https://optimizador.io/)

                                  Right now I edit the links of the screenshots and upload the images directly.

                                  girish 3 Replies Last reply Reply Quote 0
                                  • girish
                                    girish Staff @mdreira last edited by

                                    @mdreira I will give it a try locally to see if I can reproduce the problem.

                                    1 Reply Last reply Reply Quote 0
                                    • girish
                                      girish Staff last edited by

                                      Struggling to get this API key. https://optimizador.io/registro-clave-api/ has no form to fill anything, not sure how to proceed.

                                      mdreira 1 Reply Last reply Reply Quote 0
                                      • mdreira
                                        mdreira translator @girish last edited by mdreira

                                        This post is deleted!
                                        girish 1 Reply Last reply Reply Quote 0
                                        • girish
                                          girish Staff @mdreira last edited by

                                          @mdreira apparently, that page doesn't work in firefox. works in chrome.

                                          1 Reply Last reply Reply Quote 1
                                          • girish
                                            girish Staff last edited by

                                            You can run the specific hook like this

                                            root@d2c84f40-ba29-47fd-87a5-48c873006e89:/app/code# sudo -u www-data -i -- /app/pkg/wp --skip-themes --path=/app/data/public/ cron event run my_schedule_hook
                                            

                                            I am getting the same exception you posted.

                                            mdreira 1 Reply Last reply Reply Quote 2
                                            • mdreira
                                              mdreira translator @girish last edited by

                                              @girish Yes, it seems that something is failing

                                              1 Reply Last reply Reply Quote 0
                                              • girish
                                                girish Staff @mdreira last edited by

                                                @mdreira It seems that global variables are not working. The clue is from this error message:

                                                WordPress database error Table 'c801e5ebba2fb24b.wp_' doesn't exist for query 
                                                        SELECT *
                                                        FROM wp_
                                                        WHERE meta_id = 6
                                                        AND thumb_size = 'we_original_size'
                                                        ORDER BY time DESC
                                                      made by include('phar:///app/pkg/wp/php/boot-phar.php'), include('phar:///app/pkg/wp/vendor/wp-cli/wp-cli/php/wp-cli.php'), WP_CLI\bootstrap, WP_CLI\Bootstrap\LaunchRunner->process, WP_CLI\Runner->start, WP_CLI\Runner->run_command_and_exit, WP_CLI\Runner->run_command, WP_CLI\Dispatcher\Subcommand->invoke, call_user_func, WP_CLI\Dispatcher\CommandFactory::WP_CLI\Dispatcher\{closure}, call_user_func, Cron_Event_Command->run, Cron_Event_Command::run_event, do_action_ref_array('my_schedule_hook'), WP_Hook->do_action, WP_Hook->apply_filters, oiwe_my_schedule_function, oiwe_optimize_concurrent_image_by_att_id, oiwe_isOptimized
                                                

                                                The relevant code is:

                                                function oiwe_get_optimization_data_by_attID($id){
                                                
                                                        global $wpdb;
                                                        global $OIWE_TABLENAME;
                                                
                                                        $table_name = $wpdb->prefix . $OIWE_TABLENAME ;
                                                
                                                         $querystr = "
                                                            SELECT *
                                                            FROM $table_name
                                                            WHERE meta_id = $id
                                                        AND thumb_size = 'we_original_size'
                                                                GROUP BY uuid
                                                            ORDER BY time DESC
                                                         ";
                                                
                                                        $optimizations = $wpdb->get_results($querystr, OBJECT);
                                                
                                                        return $optimizations ;
                                                }
                                                

                                                In the above, OIWE_TABLENAME is null . In fact, all the globals in that file are "null" which is why we later get the error that string is expected. My PHP skillz are being put to test...

                                                1 Reply Last reply Reply Quote 1
                                                • girish
                                                  girish Staff last edited by

                                                  So strange, I cannot figure why this is. There is a constants.php in the plugin. I put the logs below:

                                                  <?php
                                                  
                                                  $OIWE_API_URL = "http://apiv1.optimizador.io:80/";
                                                  global $OIWE_API_URL ;
                                                  $PLUGIN_VERSION = "1.0.22";
                                                  error_log('The version is ' . $PLUGIN_VERSION);
                                                  global $PLUGIN_VERSION;
                                                  error_log('The version is now ' . $PLUGIN_VERSION);
                                                  
                                                  $OIWE_TABLENAME =  "imgoptimizations";
                                                  global $OIWE_TABLENAME;
                                                  
                                                  

                                                  Now, if I run via PHP cli:

                                                  root@d2c84f40-ba29-47fd-87a5-48c873006e89:/app/data# php /app/data/public/wp-content/plugins/weimgoptimizer/includes/constants.php
                                                  The version is 1.0.22
                                                  The version is now 1.0.22
                                                  

                                                  So, the above works as expected. But if I now run this via WP CRON:

                                                  root@d2c84f40-ba29-47fd-87a5-48c873006e89:/app/code# sudo -u www-data -i -- /app/pkg/wp --path=/app/data/public/ cron event run my_schedule_hook
                                                  The version is 1.0.22
                                                  The version is now 
                                                  

                                                  The version is empty for the second one! In fact, all the global things like global $PLUGIN_VERSION; in constants.php become immediately empty . FWIW, the global usage above seems to be incorrect. In PHP AFAIK, global is to declare that you are going to use a global variable and not that a variable has to become global.

                                                  1 Reply Last reply Reply Quote 0
                                                  • girish
                                                    girish Staff @mdreira last edited by

                                                    @mdreira BTW, it seems the plugin uploads all images via http and not https (see the API URL in constants.php).

                                                    mdreira 1 Reply Last reply Reply Quote 0
                                                    • mdreira
                                                      mdreira translator @girish last edited by

                                                      @girish As I don't understand anything about php, I don't really know what's going on. 😅

                                                      Is the error you're seeing something on our side or is the side of plugin guys?

                                                      girish 2 Replies Last reply Reply Quote 0
                                                      • girish
                                                        girish Staff @mdreira last edited by

                                                        @mdreira I don't understand either. It seems that global variables get reset immediately after global xx when using WP CLI. I don't get why.

                                                        1 Reply Last reply Reply Quote 0
                                                        • girish
                                                          girish Staff @mdreira last edited by girish

                                                          @mdreira OK, I found the issue. The usage of global is incorrect in the plugin. The global keyword is for stating the use of a global variable (as opposed to 'make this variable global').

                                                          With that in mind, the code in the plugin's constant.php is like this:

                                                          <?php
                                                          
                                                          $OIWE_API_URL = "http://apiv1.optimizador.io:80/";
                                                          global $OIWE_API_URL ;
                                                          
                                                          

                                                          The above code will break when the above file is included from a "function". This is the case when we use WP CLI where entire WordPress is loaded into a function. So, for example, the rough equivalent is:

                                                          function load() {
                                                              require_once(".../constants.php");
                                                          }
                                                          

                                                          When it's like above, the global $OIWE_API_URL keyword then says, use OIWE_API_URL from global context, which is not the same as the one you defined one line above (it's a function local variable). So, the solution is to move the global keyword before setting it in constants.php. Like this:

                                                          global $OIWE_API_URL ;
                                                          $OIWE_API_URL = "http://apiv1.optimizador.io:80/";
                                                          
                                                          // .. do same for other constants in the file ..
                                                          

                                                          Of course, this is why globals are not used in WP plugins. You should use superglobals like $GLOBAL or define or database etc. See https://codex.wordpress.org/Global_Variables and https://onlinewebtutorblog.com/wordpress-global-variables-step-by-step-complete-guide/

                                                          mdreira 1 Reply Last reply Reply Quote 2
                                                          • mdreira
                                                            mdreira translator @girish last edited by

                                                            @girish Then.. what should I do?.

                                                            Do I have to tell the plugin guys that they should move the globals in constants.php file or rather use superglobals?

                                                            I have no idea how to tell them this. Can we do something from our side?

                                                            girish 1 Reply Last reply Reply Quote 0
                                                            • girish
                                                              girish Staff @mdreira last edited by

                                                              @mdreira said in Cron tasks:

                                                              @girish Then.. what should I do?.

                                                              Sorry, I wasn't clear. Please send the plugin authors the link to my comment. There is nothing we can do, they have to fix the plugin. They can easily reproduce this by running cron tasks using the WP CLI tool outside Cloudron. WP CLI is pretty much part of standard WP - https://make.wordpress.org/cli/handbook/

                                                              mdreira 2 Replies Last reply Reply Quote 2
                                                              • mdreira
                                                                mdreira translator @girish last edited by

                                                                @girish you are the best!!

                                                                1 Reply Last reply Reply Quote 0
                                                                • mdreira
                                                                  mdreira translator @girish last edited by mdreira

                                                                  @girish Just one more question. I guess the problem with global variables is not only in constants.php file, right? Surely there are several files that are involved...

                                                                  I'm saying this, because I've repositioned the variables in constants.php file like you said, and it still doesn't work.

                                                                  girish 1 Reply Last reply Reply Quote 0
                                                                  • girish
                                                                    girish Staff @mdreira last edited by

                                                                    @mdreira TBH, I was only "guessing" what the solution might be after identifying the issue. I don't 100% know for sure if it solves the issue. Are you getting the same error? Did the plugin authors have any comments?

                                                                    mdreira 1 Reply Last reply Reply Quote 0
                                                                    • mdreira
                                                                      mdreira translator @girish last edited by

                                                                      @girish What I did was modify the constants file like this:
                                                                      Captura de pantalla 2022-02-14 a las 20.02.52.jpg

                                                                      Then I ran the cron:
                                                                      Captura de pantalla 2022-02-14 a las 20.04.05.jpg

                                                                      This time it didn't give any specific error message.

                                                                      The plugin guys just thanked us for the feedback. I still don't have any concrete answer from them.

                                                                      girish 1 Reply Last reply Reply Quote 0
                                                                      • girish
                                                                        girish Staff @mdreira last edited by

                                                                        @mdreira I think one has to debug that plugin more to find other issues, I only found on of the main issues TBH. It's clear the plugin is not designed to work with WP CLI, so I think that it won't work inside Cloudron unless they fix the plugin.

                                                                        I think if you really need this exact plugin, you have to switch to LAMP stack and then configure cron as however it works with this plugin. Another idea is to maybe make it possible to disable Cloudron's cron and use WP_CRON but this is not really an option for production sites.

                                                                        mdreira 1 Reply Last reply Reply Quote 1
                                                                        • mdreira
                                                                          mdreira translator @girish last edited by

                                                                          @girish It's a shame, I'll have to change the plugin

                                                                          1 Reply Last reply Reply Quote 0
                                                                          • First post
                                                                            Last post
                                                                          Powered by NodeBB