Asynchronious building documents using API?

Hey

I use the API to create a Proposal and I want to build a document of that Proposal. But building the document takes time :frowning:

However I can build it at an earlier state than I actually need it such that it would be ready later.

What happens if I PUT to the API endpoint at /api/index.php/documents/builddoc, and then immediately drops the connection?

Will it still build the document?

Hi,

you could add a hidden extrafield which can be updated in API, and add a script to check if a proposal has extrafield at 1 to be build with a cron task

here is a exemple of script to put in scripts/doc/ forlder

proposal_generation.php
#!/usr/bin/env php
<?php

if (!defined('NOSESSION')) {
    define('NOSESSION', '1');
}

$sapi_type = php_sapi_name();
$script_file = basename(__FILE__);
$path = __DIR__ . '/';

// Test if batch mode
if (substr($sapi_type, 0, 3) == 'cgi') {
    echo "Error: You are using PHP for CGI. To execute " . $script_file . " from command line, you must use PHP for CLI mode.\n";
    exit(-1);
}

@set_time_limit(0); // No timeout for this script
define('EVEN_IF_ONLY_LOGIN_ALLOWED', 1); // Set this define to 0 if you want to lock your script when dolibarr setup is "locked to admin user only".

// Include and load Dolibarr environment variables
require_once $path . "../../htdocs/master.inc.php";

require_once DOL_DOCUMENT_ROOT . '/comm/propal/class/propal.class.php';


$res = $db->query("SELECT fk_object as id from " . MAIN_DB_PREFIX . "propal_extrafields where need_build = 1");
if (!$res) {
    dol_syslog("Error proposal generation: " . $db->lasterror(), LOG_ERR);
    exit();
}
$hidedetails = !getDolGlobalString('MAIN_GENERATE_DOCUMENTS_HIDE_DETAILS') ? 0 : 1;
$hidedesc = !getDolGlobalString('MAIN_GENERATE_DOCUMENTS_HIDE_DESC') ? 0 : 1;
$hideref = !getDolGlobalString('MAIN_GENERATE_DOCUMENTS_HIDE_REF') ? 0 : 1;

while ($obj = $db->fetch_object($res)) {
    $proposal = new Propal($db);
    $resF = $proposal->fetch($obj->id);
    if ($resF <= 0) {
        dol_syslog("Error proposal generation: " . $proposal->errorsToString(), LOG_ERR);
        continue;
    }
    $proposal->generateDocument($proposal->last_main_doc, $langs, $hidedetails, $hidedesc, $hideref);
}

This text will be hidden

i choose need_build as column name, il you choose another, change the where filter according

then create a cron job that run this script periodicaly.

1 Like

yeah, that might work, but I need the document a few minutes later, so a cron job does seem to be too much time

What I ended up doing was to spawn a new thread doing only the document generation, and then I just continue in my main thread