Skip to content

Commit

Permalink
fix: 🐛 exit job correctly
Browse files Browse the repository at this point in the history
Signed-off-by: Manuel Ruck <[email protected]>
  • Loading branch information
Manuel Ruck committed Oct 22, 2023
1 parent 3d2c7c7 commit b51254c
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 78 deletions.
96 changes: 39 additions & 57 deletions services/cron-jobs/import-named-poll-deputies/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,68 +1,50 @@
import mongoConnect from "./mongoose";
import mongoConnect from './mongoose';

import { Scraper } from "@democracy-deutschland/scapacra";
import { NamedPollDeputyScraper } from "@democracy-deutschland/scapacra-bt";
import { Scraper } from '@democracy-deutschland/scapacra';
import { NamedPollDeputyScraper } from '@democracy-deutschland/scapacra-bt';

import {
NamedPollModel,
setCronStart,
setCronSuccess,
setCronError,
} from "@democracy-deutschland/bundestagio-common";
import { NamedPollModel, setCronStart, setCronSuccess, setCronError } from '@democracy-deutschland/bundestagio-common';

const CRON_NAME = "NamedPollsDeputies";
const CRON_NAME = 'NamedPollsDeputies';

const start = async () => {
const startDate = new Date();
await setCronStart({ name: CRON_NAME, startDate });
try {
await Scraper.scrape(
new NamedPollDeputyScraper(),
async (dataPackage: any) => {
process.stdout.write(".");
// Construct Database object
const namedPoll: any = { webId: dataPackage.data.id };
// Add webId field, Remove id field
const deputies = dataPackage.data.votes.deputies.reduce(
(accumulator: any, deputy: any) => {
// Remove deputies without an id;
if (!deputy.id) {
return accumulator;
}
const dep = deputy;
dep.webId = dep.id;
delete dep.id;
return [...accumulator, dep];
},
[]
);

const existingNamedPoll = await NamedPollModel.findOne({
webId: namedPoll.webId,
});

// votes.deputies
if (
!existingNamedPoll ||
!existingNamedPoll.votes ||
!(
JSON.stringify(existingNamedPoll.votes.deputies) ===
JSON.stringify(deputies)
)
) {
namedPoll["votes.deputies"] = deputies;
await Scraper.scrape(new NamedPollDeputyScraper(), async (dataPackage: any) => {
console.log('id:', dataPackage.data.id);
// Construct Database object
const namedPoll: any = { webId: dataPackage.data.id };
// Add webId field, Remove id field
const deputies = dataPackage.data.votes.deputies.reduce((accumulator: any, deputy: any) => {
// Remove deputies without an id;
if (!deputy.id) {
return accumulator;
}
const dep = deputy;
dep.webId = dep.id;
delete dep.id;
return [...accumulator, dep];
}, []);

// Update/Insert
await NamedPollModel.findOneAndUpdate(
{ webId: namedPoll.webId },
{ $set: namedPoll },
{ upsert: true }
);
const existingNamedPoll = await NamedPollModel.findOne({
webId: namedPoll.webId,
});

return;
// votes.deputies
if (
!existingNamedPoll ||
!existingNamedPoll.votes ||
!(JSON.stringify(existingNamedPoll.votes.deputies) === JSON.stringify(deputies))
) {
namedPoll['votes.deputies'] = deputies;
}
);

// Update/Insert
await NamedPollModel.findOneAndUpdate({ webId: namedPoll.webId }, { $set: namedPoll }, { upsert: true });

return;
});
} catch (error) {
await setCronError({ name: CRON_NAME, error: JSON.stringify(error) });
throw error;
Expand All @@ -71,13 +53,13 @@ const start = async () => {
};

(async () => {
console.info("START");
console.info("process.env", process.env.DB_URL);
console.info('START');
console.info('process.env', process.env.DB_URL);
if (!process.env.DB_URL) {
throw new Error("you have to set environment variable: DB_URL");
throw new Error('you have to set environment variable: DB_URL');
}
await mongoConnect();
console.log("procedures", await NamedPollModel.countDocuments({}));
console.log('procedures', await NamedPollModel.countDocuments({}));
await start().catch(() => process.exit(1));
process.exit(0);
})();
4 changes: 2 additions & 2 deletions services/cron-jobs/push-send-queued/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ const start = async () => {
await start().catch((e) => {
throw e;
});
})().finally(async () => {
await mongoDisconnect();
process.exit(0);
})().finally(() => {
mongoDisconnect();
});
11 changes: 7 additions & 4 deletions services/cron-jobs/sync-deputy-profiles/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@ const start = async () => {
console.log('procedures', await DeputyModel.countDocuments({}));
await start();
await mongoDisconnect();
})().catch(async (e) => {
await mongoDisconnect();
throw e;
});
})()
.catch(async () => {
process.exit(1);
})
.then(async () => {
process.exit(0);
});
1 change: 1 addition & 0 deletions services/cron-jobs/sync-deputy-profiles/src/mongoose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ export const mongoDisconnect = () => {
if (connection) {
return connection.disconnect();
}
return;
};
3 changes: 3 additions & 0 deletions services/cron-jobs/sync-procedures/env.local
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
BUNDESTAGIO_SERVER_URL=http://localhost:3100
LIMIT=10
DB_URL=mongodb://localhost/democracy
11 changes: 4 additions & 7 deletions services/cron-jobs/sync-procedures/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mongoConnect, mongoDisconnect } from './mongoose';
import { mongoConnect } from './mongoose';
import _ from 'lodash';
import moment from 'moment';
import { forEachSeries } from 'p-iteration';
Expand Down Expand Up @@ -322,10 +322,7 @@ const start = async () => {
throw new Error('you have to set environment variable: BUNDESTAGIO_SERVER_URL & DB_URL');
}
await mongoConnect();
console.log('procedures', await ProcedureModel.countDocuments({}));
await start();
await mongoDisconnect();
})().catch(async (e) => {
await mongoDisconnect();
throw e;
});
console.info('END');
process.exit();
})();
8 changes: 0 additions & 8 deletions services/cron-jobs/sync-procedures/src/mongoose.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { mongoose } from '@democracy-deutschland/democracy-common';

let connection: typeof mongoose;

export const mongoConnect = async () =>
new Promise(async (resolve) => {
// Mongo Debug
Expand All @@ -19,9 +17,3 @@ export const mongoConnect = async () =>
throw e;
});
});

export const mongoDisconnect = () => {
if (connection) {
return connection.disconnect();
}
};

0 comments on commit b51254c

Please sign in to comment.