Page 1 of 1

Difference Between Execute & Cron automate

PostPosted:Wed Oct 26, 2016 10:08 am
by Sebastien
Hi team,

I don't have the same execution if i launch my crontask with the "Execute" button or if I am waiting the cron is launch automatically.

The script create an export of my repository and he zip him.
It works properly if i click on Execute...

See the code :
Code: Select all
import java.io.File;
import java.io.FileOutputStream;
import java.io.StringWriter;
import com.openkm.util.FileUtils;
import com.openkm.util.impexp.RepositoryExporter;
import com.openkm.util.impexp.TextInfoDecorator;
import com.openkm.util.ArchiveUtils;
import org.apache.commons.io.IOUtils;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
//test de nouvelles classes
import com.openkm.api.*;
import com.openkm.core.*;

//formatage de la variable DOM
DateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd");
Calendar cal = Calendar.getInstance();
String DOM = dateFormat.format(cal.getTime());
 
// Destination 
File file = new File("/home/BACKUP/OpenKM/"+DOM+"_RepositoryExport.zip");
// OpenKM folder contents to be exported
String fldPath = "/okm:root";
 
FileOutputStream os = new FileOutputStream(file);
StringWriter out = new StringWriter();
File tmp = FileUtils.createTempDir();
RepositoryExporter.exportDocuments(null, fldPath, tmp, false, false, out, new TextInfoDecorator(fldPath));
ArchiveUtils.createZip(tmp, "import", os);
org.apache.commons.io.FileUtils.deleteDirectory(tmp);
IOUtils.closeQuietly(out);
os.flush();
os.close();
See the log who appear when the cron job is execute :
Code: Select all
2016-10-26 11:59:00,143 [Thread-6516] INFO  com.openkm.module.db.stuff.DbSimpleAccessManager- ***************************
2016-10-26 11:59:00,143 [Thread-6516] INFO  com.openkm.module.db.stuff.DbSimpleAccessManager- ***************************
2016-10-26 11:59:00,143 [Thread-6516] WARN  com.openkm.module.db.stuff.DbSimpleAccessManager- com.openkm.module.db.stuff.DbSimpleAccessManager -> isGranted (DbSimpleAccessManager.java:68)
2016-10-26 11:59:00,143 [Thread-6516] WARN  com.openkm.module.db.stuff.DbSimpleAccessManager- com.openkm.module.db.stuff.SecurityHelper -> checkRead (SecurityHelper.java:96)
2016-10-26 11:59:00,143 [Thread-6516] WARN  com.openkm.module.db.stuff.DbSimpleAccessManager- com.openkm.dao.NodeMailDAO -> findByParent (NodeMailDAO.java:130)
2016-10-26 11:59:00,143 [Thread-6516] WARN  com.openkm.module.db.stuff.DbSimpleAccessManager- com.openkm.module.db.DbMailModule -> getChildren (DbMailModule.java:530)
2016-10-26 11:59:00,143 [Thread-6516] WARN  com.openkm.module.db.stuff.DbSimpleAccessManager- com.openkm.util.impexp.RepositoryExporter -> exportDocumentsHelper (RepositoryExporter.java:153)
2016-10-26 11:59:00,143 [Thread-6516] WARN  com.openkm.module.db.stuff.DbSimpleAccessManager- com.openkm.util.impexp.RepositoryExporter -> exportDocuments (RepositoryExporter.java:70)
2016-10-26 11:59:00,143 [Thread-6516] WARN  com.openkm.module.db.stuff.DbSimpleAccessManager- com.openkm.util.ExecutionUtils -> runScript (ExecutionUtils.java:112)
2016-10-26 11:59:00,143 [Thread-6516] WARN  com.openkm.module.db.stuff.DbSimpleAccessManager- com.openkm.core.Cron$RunnerBsh -> run (Cron.java:103)
2016-10-26 11:59:00,143 [Thread-6516] INFO  com.openkm.module.db.stuff.DbSimpleAccessManager- ***************************
2016-10-26 11:59:00,143 [Thread-6516] INFO  com.openkm.module.db.stuff.DbSimpleAccessManager- ***************************
2016-10-26 11:59:00,144 [Thread-6516] WARN  com.openkm.core.Cron- Error executing crontab task 'Backup_repository': bsh.TargetError: Sourced file: inline evaluation of: ``import java.io.File; import java.io.FileOutputStream; import java.io.StringWrite . . . '' : Method Invocation RepositoryExporter.exportDocuments : at Line: 29 : in file: inline evaluation of: ``import java.io.File; import java.io.FileOutputStream; import java.io.StringWrite . . . '' : RepositoryExporter .exportDocuments ( null , fldPath , tmp , false , false , out , new TextInfoDecorator ( fldPath ) )

Target exception: java.lang.NullPointerException

2016-10-26 11:59:00,148 [Thread-6516] WARN  com.openkm.core.Cron- Error sending mail: Sourced file: inline evaluation of: ``import java.io.File; import java.io.FileOutputStream; import java.io.StringWrite . . . '' : Method Invocation RepositoryExporter.exportDocuments : at Line: 29 : in file: inline evaluation of: ``import java.io.File; import java.io.FileOutputStream; import java.io.StringWrite . . . '' : RepositoryExporter .exportDocuments ( null , fldPath , tmp , false , false , out , new TextInfoDecorator ( fldPath ) )

Target exception: java.lang.NullPointerException

here is the configuration of my cronJob, i think is it configure properly because i test every time with adapted settings "Expression":
Code: Select all
Backup_repository	59 11 * * *	application/x-bsh	Backup_OpenKM.bsh 	noreply@openkm.com 	26-10-2016 11:52:00 	26-10-2016 11:52:00 	Active 	Edit   Delete   Execute   Download

thank you in advance for your help :D

Re: Difference Between Execute & Cron automate

PostPosted:Fri Oct 28, 2016 2:35 pm
by jllort
Basically when you click on "Execute" button you are executing the crontab code with your user session ( that means you are ROLE_ADMIN user and the code into the crontab is executed with this grants ).

But when the code is executed by the crontab, really there's no session linked with that execution and you should not use token=null otherwise you will get problems with security. For it you must getting first the systemToken and then use into your API calls:

Something like it:
Code: Select all
String systemToken = DbSessionManager.getInstance().getSystemToken();
RepositoryExporter.exportDocuments(systemToken, fldPath, tmp, false, false, out, new TextInfoDecorator(fldPath));

Re: Difference Between Execute & Cron automate

PostPosted:Thu Nov 10, 2016 2:42 pm
by Sebastien
Hello @jllort,

Thank you for you're return, It's Work now !

I did not have understand before the "Token" mecanism in openKM.

I paste my new code, if anyone is interested by it :
Code: Select all
// Import des classes
import java.io.File;
import java.io.FileOutputStream;
import java.io.StringWriter;
import com.openkm.util.FileUtils;
import com.openkm.util.impexp.RepositoryExporter;
import com.openkm.util.impexp.TextInfoDecorator;
import com.openkm.util.ArchiveUtils;
import com.openkm.module.db.stuff.DbSessionManager;
import org.apache.commons.io.IOUtils;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

//formatage de la variable DOM
DateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd");
Calendar cal = Calendar.getInstance();
String DOM = dateFormat.format(cal.getTime());
 
// Destination 
File file = new File("/home/BACKUP/OpenKM/"+DOM+"_RepositoryExport.zip");
// OpenKM folder contents to be exported
String fldPath = "/okm:root";
 
FileOutputStream os = new FileOutputStream(file);
StringWriter out = new StringWriter();
File tmp = FileUtils.createTempDir();

// Get Token
String systemToken = DbSessionManager.getInstance().getSystemToken();

// Appel du RepositoryExporter
RepositoryExporter.exportDocuments(systemToken, fldPath, tmp, false, false, out, new TextInfoDecorator(fldPath));

// Creation du Zip
ArchiveUtils.createZip(tmp, "import", os);

// Delete des fichiers temporaires
org.apache.commons.io.FileUtils.deleteDirectory(tmp);
IOUtils.closeQuietly(out);
os.flush();
os.close();
Thank's a lot for you're reactivity, OpenKM is a very good OpenSource software !

Re: Difference Between Execute & Cron automate

PostPosted:Fri Nov 11, 2016 7:22 am
by jllort
Hope in new documentation this things will be more clearly explained. We have just finished it and are waiting for doing the final community release and new documentation at the same time.