New Feature with cron and job

hey I have implementing a new feature on a component, anytime I create a component, there is this field with a default value of true, then at every day at midnight, it pass to false
my goal is to tell to my user if this composent is new or old (in the frontend)

Capture d’écran 2020-12-01 à 12.37.21

so I add this code in the backend in order to do that

'0 0 0 * * *': async () => {
    const drops = await strapi.services.drops.find()
    const res = await Promise.all(drops.map(async( drop, index) => {
      let dropcopy = {...drop}
      const secondRes = await Promise.all(drop.retailer_info.map(async(retailer, index) => {
        if(dropcopy.retailer_info[index].isnew = true){
          dropcopy.retailer_info[index].isnew = false
        }
      }))
      await strapi.services.drops.update({id :drop.id},dropcopy);
    }))
  }

the code is working (in local not in production) so before I push it in prod, I just anted to know if it is the good way or is there a way to do it cleaner ? thank you :blush:
N.B. my app is already deployed on heroku

So you use it just to display a label NEW in the frontend so users could know that article is new?

Then there is no need to use isNew field at all. You could just use published_at in the frontend, make some calculations like this:

if ( difference ( currentTime - article.published_at ) < 24 hours ) {
  article.isNew = true;
}

Moment example:

const current_time = moment( new Date() ); // current time
const published_at = moment( article.published_at ); //get moment object of published_at
const differenceHours = moment.duration( current_time.diff( published_at ) ).asHours(); // difference in hours
if ( differenceHours < 24 ) {
    article.isNew = true;
}

That way there is no need to process all the articles daily with cron and etc.

1 Like

Actually isNew is not for articles, I have to know if the composant inside the article is new or not, and there is no created_at or published_at when I add a composant

I know, that’s just a general example.

Wait, you actually want to display isNew in Strapi’s Admin when you added a new component? Not in the Frontend app?

"id": 1,
"brand_name": "adidas",
"drop_date": "2020-10-06",
"archive": false,
"created_at": "2020-11-02T21:12:03.834Z",
"updated_at": "2020-11-30T10:55:44.660Z",
"likes": 1,
"retailer_info": [
   {
   "id": 359,
   "drop_date": "lastTest",
   "drop_hour": "00:00:00.000",
   "link": "v",
   "retailer": {
    "id": 1,
    "retailler_name": "1",
    "slug": "1",
    "created_at": "2020-11-28T17:46:08.234Z",
    "updated_at": "2020-11-28T17:46:08.244Z",
    "retailler_image": null
   },
   "drop_type": "Raffle",
   "oui": null,
   "isnew": false
},
{
   "id": 360,
   "drop_date": "lastTest",
   "drop_hour": "23:00:00.000",
   "link": "lastTest",
   "retailer": {
    "id": 2,
    "retailler_name": "csaacs",
    "slug": "csaacs",
    "created_at": "2020-11-28T17:46:14.519Z",
    "updated_at": "2020-11-28T17:46:14.528Z",
    "retailler_image": null
   },
   "drop_type": "Instagram",
   "oui": null,
   "isnew": false
}
]
},

So for example I have this article, what I want is to whenever I add a retailer_info composant inside my article, to know if it is a new one or not. To do that I add a “isnew” field which is by default set as true and I change it to false every day with cron. But I want to display “isnew” in the frontend so if there is a way to do it with published_at or created_at I take it :blush: It is how I do but I don’t know if it the good way aha

I don’t know if we understand each other :grin:

1 Like

@sunnyson Sorry to remind you, it is the good way ?

Hello, the method itself is ok, but I would recommend a few suggestions:

Here you get all the drops, but you don’t need all of them. Get only those that have isNew true.

Here you update all the fields of the drops. But you need to update only the isNew. Change that.

Yes !! it’s true it would be better :sweat_smile:, thank you so much (btw this forum is so helpful)

1 Like

Hi Gabriel, im developing something and im deploying it on Heroku, i have a cron job that runs well in local, but on heroku does not work , or i see it’s not running, is there something else that i have to set up before?

I would advise against trying to use the built-in cronjobs while using heroku, much like the reason why we don’t support the CTB or SQLite on Heroku: Troubleshooting - Strapi Developer Documentation

Heroku instances have a limited lifetime, typically only a few hours before they go to “sleep”. At the point of sleeping the instance is effectively killed and no longer running, meaning cronjobs aren’t running. When Heroku detects new traffic, it spins up a new instance, pulls the code from Git, and cold boots the project.

Meaning if your instance is “sleeping” when your cronjob is supposed to run, it won’t. Attempting to force the instance from going to sleep will not be possible as Heroku specifically states they will forcefully kill the instance: Free Dyno Hours | Heroku Dev Center