Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/agenda/jobs.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const DEFAULT_OPTIONS = {
module.exports = function (agenda) {
agenda.on('ready', async function() {
agenda.every('2 minutes', 'syncMatriculas', {}, DEFAULT_OPTIONS)
agenda.every('5 days', 'clearAgendaDatabase', {}, DEFAULT_OPTIONS)
agenda.start()
})
}
28 changes: 28 additions & 0 deletions app/agenda/processors/clearAgendaDatabase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const app = require('@/app')
const MongoClient = require('mongodb')
const moment = require('moment')

module.exports = function (agenda) {
agenda.define('clearAgendaDatabase', app.helpers.agenda.wrap(clearAgendaDatabase))
}

async function clearAgendaDatabase () {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@FeoSilva vê se eu não fiz nenhuma merda, ai pode mandar pra prod

try {
const client = await MongoClient.connect(
app.config.MONGO_URL,
{ useNewUrlParser: true, useUnifiedTopology: true },
)


// Keep the past 4 months (quad) of agenda history
const cutoffDate = moment().subtract(4, 'months').toDate()
const agendaCollection = client.db('ufabc-matricula').collection('agenda')
await agendaCollection.deleteMany({ lastRunAt: { $lte: cutoffDate }})

await client.close()
} catch(e) {
console.log(e)
}
}

module.exports.clearAgendaDatabase = clearAgendaDatabase