How to use portal functions on Jobs?

I’m trying to use the cronjob app but since the job has not context, I need to create one and run on it, but how can I say that the context I’m using is refered to a Site, so functions like “portal.getSite()” could be used, right? Other functions like “serviceUrl” which is the one I’ll use most, in case of scheduling service calls on the job gives me an error…

If you don’t know what parameters to pass to:
http://repo.enonic.com/public/com/enonic/xp/docs/6.9.0/docs-6.9.0-libdoc.zip!/module-lib_xp_context.html#.run

You could log.info what
http://repo.enonic.com/public/com/enonic/xp/docs/6.9.0/docs-6.9.0-libdoc.zip!/module-lib_xp_context.html#.get

returns in some controller in one of the apps on that site.

Then ‘hardcode’ the parameters to run based on that information.

1 Like

You cannot say that it’s from a site when running in a task. It has nothing to do with the portal and therefore portal-functions will fail.

1 Like

Really unsure why you would want to create url’s to services within a task? Maybe you could explain what you are trying to solve here? Are you building a newsletter or similar?

We already had services created, since the project in question has started before we have this Cronjob app. Some imports from external API’s were made by these services, so the site just needed to run them. Kinda noob thing, I know, but we have to keep these on services because if the client decides to run any of these in a time that is not scheduled, he could do via url.

You don’t need portal url stuff for this, simply require(/service/myservice) in your task and run an exported function…

2 Likes

Hi !

I have the same problem. I’m using cronjobApp and I need to have an access to portal functions. In site config we have a list of parent nodes that should be used during import. I tried to set admin user via contextLib.run, but I still have same error:

Caused by: java.lang.NullPointerException: null
at com.enonic.xp.lib.portal.current.GetCurrentSiteConfigHandler.execute(GetCurrentSiteConfigHandler.java:18) ~[na:na]
at jdk.nashorn.internal.scripts.Script$Recompilation$12611$10235$portal.L:1#getSiteConfig(com.norsedigital.nibio:/site/lib/xp/portal.js:252) ~[na:na]

Is there any option/workaround ?

Thanks !

P.S.: During development I used services to execute my script and it worked as expected.

I’ve created on a lib the getSite function:
exports.getSite = function(){ var site; var sitesResult = contentLib.query({ query: "_path LIKE '/content/*' AND data.siteConfig.applicationKey = '"+app.name+"'", contentTypes: ["portal:site"] }); //log.info(sitesResult); site = sitesResult.hits[0]; return site; }

And the getSiteConfig:
exports.getSiteConfig = function(site, applicationKey){
if(!site || site == null || typeof site === “undefined”){
site = exports.getSite();
}
if(site) {
if (site.data) {
if (site.data.siteConfig) {
var siteConfigs = seeds.forceArray(site.data.siteConfig);
var siteConfig = {};
siteConfigs.forEach(function (cfg) {
if(applicationKey && cfg.applicationKey == applicationKey){
siteConfig = cfg;
} else if (!applicationKey && cfg.applicationKey == app.name) {
siteConfig = cfg;
}
});

				return siteConfig.config;
			}
		}
	}
};

That’s working for me now. It should works for you too :stuck_out_tongue:

3 Likes

Thank you !!! Everything works :slight_smile:

2 Likes