CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Post-installation Setup and Configuration (Moderator: Dave Greenberg) »
  • Scheduled jobs via cron.php will only run jobs with api entity="job"; solution
Pages: [1]

Author Topic: Scheduled jobs via cron.php will only run jobs with api entity="job"; solution  (Read 2145 times)

flug

  • I post frequently
  • ***
  • Posts: 126
  • Karma: 12
Scheduled jobs via cron.php will only run jobs with api entity="job"; solution
March 29, 2013, 02:08:04 pm
I've created a few routines intended to be run as scheduled jobs.  So they are set up as APIs with a custom API entity and API action.

This works fine from the scheduled jobs page, as I just select:

 API entity = mycustomentity
 API action = mycustomaction

And it works fine.

However for various reasons I want to call this entity/action via URL using cron.php as described here:

 http://wiki.civicrm.org/confluence/display/CRMDOC43/Running+Command-line+Scripts+via+URL

Trying this, I discovered that cron.php is hard-coded to use api entity "job" and it won't allow any other api entity.

This is perhaps sensible for security reasons.  However I wanted to be able to run my own api actions via cron.php so I came up with this mod of cron.php:

Code: [Select]
<?php
/*
 +--------------------------------------------------------------------+
 | CiviCRM version 4.2                                                |
 +--------------------------------------------------------------------+
 | Copyright CiviCRM LLC (c) 2004-2012                                |
 +--------------------------------------------------------------------+
 | This file is a part of CiviCRM.                                    |
 |                                                                    |
 | CiviCRM is free software; you can copy, modify, and distribute it  |
 | under the terms of the GNU Affero General Public License           |
 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
 |                                                                    |
 | CiviCRM is distributed in the hope that it will be useful, but     |
 | WITHOUT ANY WARRANTY; without even the implied warranty of         |
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
 | See the GNU Affero General Public License for more details.        |
 |                                                                    |
 | You should have received a copy of the GNU Affero General Public   |
 | License and the CiviCRM Licensing Exception along                  |
 | with this program; if not, contact CiviCRM LLC                     |
 | at info[AT]civicrm[DOT]org. If you have questions about the        |
 | GNU Affero General Public License or the licensing of CiviCRM,     |
 | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
 +--------------------------------------------------------------------+
*/


require_once '../civicrm.config.php';
require_once 
'CRM/Core/Config.php';
require_once 
'CRM/Utils/Request.php';
$config = CRM_Core_Config::singleton();

CRM_Utils_System::authenticateScript(TRUE);

$job = CRM_Utils_Request::retrieve('job', 'String', CRM_Core_DAO::$_nullArray, FALSE, NULL, 'REQUEST');

//mod, 2013/03/29, added next 4 lines
//added 'entity' parameter to allow any api action to be run via cron/remote call
$entity = CRM_Utils_Request::retrieve('entity', 'String', CRM_Core_DAO::$_nullArray, FALSE, NULL, 'REQUEST');
if (
$entity === NULL) {
  
$entity='job';
}

require_once 
'CRM/Core/JobManager.php';
$facility = new CRM_Core_JobManager();

if (
$job === NULL) {
  
$facility->execute();
}
else {
  
//mod, added , "entity"
  
$ignored = array("name", "pass", "key", "job", "entity");
  
$params = array();
  foreach (
$_REQUEST as $name => $value) {
    if (!
in_array($name, $ignored)) {
      
$params[$name] = CRM_Utils_Request::retrieve($name, 'String', CRM_Core_DAO::$_nullArray, FALSE, NULL, 'REQUEST');
    }
  }
  
$facility->setSingleRunParams($entity, $job, $params, 'From cron.php');
  
$facility->executeJobByAction($entity, $job);
}


Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Scheduled jobs via cron.php will only run jobs with api entity="job"; solution
March 29, 2013, 05:25:33 pm

hey flug:

can you post a diff of the two files rather than the entire file? makes it easier to see the differences

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

jorich_2000

  • I’m new here
  • *
  • Posts: 11
  • Karma: 0
  • CiviCRM version: 4.4.3
  • CMS version: 2.5.14
  • MySQL version: 5.0.95
  • PHP version: 5.3.23
Re: Scheduled jobs via cron.php will only run jobs with api entity="job"; solution
January 24, 2015, 11:49:32 pm
This a really useful modification and makes debugging custom jobs so much easier.  I can now set netbeans with alternate debug urls to directly call each job.  This has the added benefit of not needing me to login to joomla/civicrm first. (slight gotcha is that $entity is case sensitive)
« Last Edit: January 25, 2015, 12:00:23 am by jorich_2000 »

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Post-installation Setup and Configuration (Moderator: Dave Greenberg) »
  • Scheduled jobs via cron.php will only run jobs with api entity="job"; solution

This forum was archived on 2017-11-26.