I created a cron job and use Strapi custom service that I wrote. But an error comes as: TypeError: Cannot read property 'services' of undefined at Job.1 * * * * * [as job] .
Here is my cron job code:
module.exports = {
'1 * * * * *': ({ strapi }) => {
strapi.services.account.myService();
},
};
I'm using strapi version 3.6.8.
The answer to this question is simple. You're using the syntax from StrapiV4 in StrapiV3. The correct syntax for cronjob in strapiv3 is as follows:
module.exports = {
/**
* Simple example.
* Every monday at 1am.
*/
'0 0 1 * * 1': () => {
// you can then reference you strapi custom service like so
await strapi.services.account.myServiceMethod();
},
};
References
Cron Jobs in Strapi V4
Cron Job in Strapi V3
firebase function deploy fail, but logs are confusing. Actually, logs does not provide useful info.
I have tried renaming the functions 3 times, but no luck. This is annoying.
/**
* Get random number
* #param {number} min
* #param {number} max
* #return {number} random
*/
function between(min, max) {
return Math.floor(
Math.random() * (max - min) + min
);
}
const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;
const client = require("twilio")(accountSid, authToken);
exports.sendSMS2=functions.pubsub.schedule("46 7 * * *")
.timeZone("America/New_York").onRun((context) =>{
return client.messages
.create({body: text +" " +between(1, 10), from: from, to: to})
.then((message) => console.log(message.sid));
});
console:
i deploying functions
Running command: npm --prefix "$RESOURCE_DIR" run lint
> lint
> eslint .
Functions deploy had errors with the following functions:
sendSMS2(us-central1)
i functions: cleaning up build files...
Error: There was an error deploying functions
I would like to change the implementation shown below to using a command line script in a node.js application. How do I do it?
var cron = require('node-cron');
cron.schedule('* * * * *', () => {
getBananas();
});
here is my code for scheduling a task. i have used separate routes for starting,stopping and changing the time as given below. please tell me if its correct. and also im getting an error for changing the time frequency.please help me.
const cronjob=require('cron').CronJob
var first='* * * * * *'
var task=function() {
console.log('last'+job.lastDate()+' next '+job.nextDates(1));
}
var complete=function(){
console.log('the end.........')
}
var job = new cronjob(first,task, complete, false, 'Asia/Kolkata');
app.get('/start',(req,res)=>{
job.start()
console.log('is job running? ', job.running);
res.send('cron started')
})
app.get('/set',(req,res)=>{
var time=req.headers.time /* input taken from user for changing the frequency*/
time='30 5 1-31 0-11 1-7' /*hard-coded temporary for testing*/
console.log(time)
job.setTime(time)
})
/set api is throwing an error
/Error: time must be an instance of CronTime./
we have to use instance of a cronTime i.e.
const CronTime = require('cron').CronTime
.
.
.
var time=req.query.time
//time='*/5 * * * * *'
job.setTime(new CronTime(time))
res.send('time changes')
Hi I was wondering If anyone got examples of using Cron Schedule functions on Strapi: https://strapi.io/documentation/3.x.x/configurations/configurations.html#functions
like sending email, accessing the strapi config, etc.
'*/1 * * * *': async() => {
console.log("I am running " + new Date(), Object.keys(strapi.config));
await strapi.services.article.publish();
}
In your-project/config/functions/cron.js. you can add as many functions in the above format.
The function name in itself is a cron expression which is parsed by strapi to execute at frequent intervals. There are many online tools that will tell you the cron expression that you want to create.
The above function runs every 1 minute, by which I am publishing a content type by using strapi.services. i.e. in file your-project/api/article/services/Article.js
I have written a service layer method that at the moment is publishing an article.
Similarly, you can send an email from your email content type or any utility file that you have made to trigger an email.
For accessing strapi config use: strapi.config object instead of strapi.services
Some example of CRON jobs for Strapi
add this line server.js
...
port: env.int('PORT', 1337),
cron :{
enabled: true
},
admin:
...
cron.js some examples
module.exports = {
/**
* Simple examples.
*/
'*/5 * * * * *': () => {
console.log("🚀 ~ file: cron.js ~ executing action ~Every 5sec");
},
'*/10 * * * * *': () => {
console.log("🚀 ~ file: cron.js ~ executing action ~Every 10sec");
},
'* */5 * * * *': () => {
console.log("🚀 ~ file: cron.js ~ executing action ~Every 5min");
},
'* * */5 * * *': () => {
console.log("🚀 ~ file: cron.js ~ executing action ~Every 5hour");
},
};
Strapi 3 has cron jobs turned off by default. Make sure you turn them on first :)
You also don't need to do */1 for every minute in a cron job, just *, as * means every, and it only checks once a minute.
My requirement was to fetch data from an external MSSQL DB (master data) hosted under RDS (AWS) and update the strapi product catolog (mongodb) every minute.
I've created a custom "cron" folder under "root" to keep all my custom modules in order keep a clean "cron.js".
Under "cron.js" i've simply imported my custom module to call the external module:
const fwSync = require('../../cron/dataSync');
If you want to call multiple cron jobs
import firstCronJob from '../src/plugins/first-cron-job';
import secondCronJob from '../src/plugins/second-cron-job';
export default ({ env }) => ({
host: env('HOST', '0.0.0.0'),
port: env.int('PORT', 1337),
app: {
keys: env.array('APP_KEYS'),
},
cron: {
enabled: true,
tasks: {
...firstCronJob,
...secondCronJob,
},
},
});