#How to implement Tasks/CronJobs

1 messages · Page 1 of 1 (latest)

floral quarry
#

Hey!

In the old Backend System, I already have a backend plugin with Knex migrations and a router.
Now I need something like a CronJob/Task. It should be triggered for example every 4 hours, check something in the DB and eventually delete from DB. As I have multiple Instances, the Job should only be triggered in one Instance.
What should I add where in my plugin? Thanks!

#

| |____reservation-backend
| | |____migrations
| | | |____20240214000000_init.js
| | |____README.md
| | |package.json
| | |
.eslintrc.js
| | |____src
| | | |____database
| | | | |____DatabaseReservationStore.ts
| | | | |____ReservationStore.ts
| | | | |____index.ts
| | | |____shared
| | | | |____shared.ts
| | | |____setupTests.ts
| | | |____index.ts
| | | |____service
| | | | |____router.ts
| | | | |____router.test.ts
| | | | |____standaloneServer.ts
| | | | |____index.ts
| | | |____run.ts

marsh loom
#

You add a dep on coreServices.scheduler, and use that

#

you can set frequency: { cron: '5 4 * * *' } for example

floral quarry
#

You link navigates to the New Backend System, thats why I am asking: Can I do it like this in the old System?

I added a file to my service/houskeeping.ts

import {
coreServices,
createBackendPlugin,
} from '@backstage/backend-plugin-api';

createBackendPlugin({
pluginId: 'plugin-reservation-backend',
register(env) {
env.registerInit({
deps: {
scheduler: coreServices.scheduler,
},
async init({ scheduler }) {
await scheduler.scheduleTask({
frequency: { minutes: 1 },
timeout: { seconds: 30 },
id: 'housekeeping-reservations',
fn: async () => {
console.log("TEST")
},
});
},
});
},
});

But nothing happens..

#

I am using the PluginTaskScheduler now, it works

await options.task.scheduleTask({
    frequency: { minutes: 1 },
    timeout: { seconds: 30 },
    id: 'housekeeping-reservations',
    fn: async () => {
        console.log("LALSDLALSDL")
    },
});
marsh loom
#

oh i thought this was part of a migration to the new system

#

yeah exactly, it exists in the env for each plugin too in the old system

#

it's the same service, just accessed in a slightly different way