Skip to content

Commit

Permalink
Try fixing cron job, error caused by Strapi reloading after updating db
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Graule committed Aug 7, 2024
1 parent 44f3712 commit 37456ad
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 32 deletions.
29 changes: 15 additions & 14 deletions backend/config/functions/cronTask.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,28 @@ module.exports = {
updateAllSatellitesData: {
task: async ({ strapi }) => {
try {
// Fetching all satellites
const satellites = await strapi.entityService.findMany('api::satellite.satellite');

// Waiting for all promises to be resolved
await Promise.all(
satellites.map(async satellite => {
try {
setTimeout(async () => {
await fetchOrbitalData(strapi, satellite.id);
}, 10000);
} catch (error) {
console.error(error);
// Fetch all satellites
const satellites = await strapi.entityService.findMany('api::satellite.satellite', {
fields: ['id', 'catalogNumberNORAD'],
filters: {
catalogNumberNORAD: { $ne: null },
}
});
await Promise.all(satellites.map(async satellite => {
const historicalOrbitalData = await fetchOrbitalData(strapi, satellite.catalogNumberNORAD);
await strapi.entityService.update('api::satellite.satellite', satellite.id, {
data: {
historicalOrbitalData: historicalOrbitalData,
}
})
);
}));
} catch (error) {
console.error(error);
return;
}
},
options: {
rule: "0 0 0 3 * *", // Every month on the 3rd at midnight
rule: "0 0 0 8 * *", // Every month on the 3rd at midnight
},
},
};
22 changes: 4 additions & 18 deletions backend/config/functions/satelliteUtils.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
// backend/utils/satelliteUtils.js
const axios = require('axios');

async function fetchOrbitalData(strapi, contextId) {
async function fetchOrbitalData(strapi, noradId) {
try {
// Fetching the satellite
const satellite = await strapi.entityService.findOne('api::satellite.satellite', contextId);
const noradId = satellite.catalogNumberNORAD;

// Authentication to Space-Track
const authResponse = await axios.post('https://www.space-track.org/ajaxauth/login', {
identity: 'floridg@stud.ntnu.no',
Expand All @@ -15,7 +11,8 @@ async function fetchOrbitalData(strapi, contextId) {

if (authResponse.status === 200) {
// Fetching data from Space-Track
const satelliteResponse = await axios.get(`https://www.space-track.org/basicspacedata/query/class/gp_history/NORAD_CAT_ID/${noradId}/orderby/TLE_LINE1%20ASC/EPOCH/1950-07-02--2024-07-02/format/json`, {
const today = new Date();
const satelliteResponse = await axios.get(`https://www.space-track.org/basicspacedata/query/class/gp_history/NORAD_CAT_ID/${noradId}/orderby/TLE_LINE1%20ASC/orderby/TLE_LINE1%20ASC/format/json`, {
headers: {
Cookie: authResponse.headers['set-cookie']
}
Expand All @@ -30,19 +27,8 @@ async function fetchOrbitalData(strapi, contextId) {
eccentricity: data.ECCENTRICITY,
semiMajorAxis: data.SEMIMAJOR_AXIS
}));

// Updating the satellite with the new data
const updatedSatellite = await strapi.entityService.update('api::satellite.satellite', contextId, {
data: {
historicalOrbitalData: historicalOrbitalData,
},
});
return updatedSatellite;
} else {
throw new Error('Error while fetching data from Space-Track');
return historicalOrbitalData;
}
} else {
throw new Error('Authentication failed');
}
} catch (error) {
console.error('Error while fetching data to Space-Track: ', error);
Expand Down

0 comments on commit 37456ad

Please sign in to comment.