Gérer et configurer les tâches
10 min
Démarrer une tâche
import { runTask } from '@fasstech/oip-core/task';
export const runTask1 = async (projectId) => {
await runTask({ projectId, taskName: 'task1' });
};
Arrêter une tâche
import { killTask } from '@fasstech/oip-core/task';
export const killTask1 = async () => {
await killTask({ taskName: 'task1' });
};
Récupérer une liste de tâches
import { getTasks } from '@fasstech/oip-core/task';
export const getAllTasks = async () => {
return getTasks();
};
Récupérer une tâche
import { getTasks } from '@fasstech/oip-core/task';
export const getTask = async ({ projectId, taskName }) => {
return getTasks({ projectId, taskName });
};
Mettre à jour l'état d'une tâche
import { Entities } from '@fasstech/oip-core';
import { getCustomerHasCertifiedOnTheHonor } from '@fasstech/oip-core/entities/Customer';
export default {
tasks: {
task1: async ({ metadata, taskHandler }) => {
const project = await Entities('Project').get({ id: metadata.projectId });
const customer = await Entities('Customer').get(project);
if (getCustomerHasCertifiedOnTheHonor(customer) ?? false) {
await taskHandler.updateState({
metadata,
state: { status: 'FOO', statusCode: 42 }
});
}
}
}
}
Déclarer une tâche dans votre fichier de configuration
export const taskContext = {};
export default {
tasks: {
// Tâche synchrone
task1: () => {
taskContext.x += 1;
},
// Tâche asynchrone
// avec mise à jour du statut de la tâche
task2: async ({ metadata, taskHandler }) => {
taskContext.msg = `Hello World: ${taskHandler.get().taskName}!`;
await taskHandler.updateState({ metadata, state: { status: 'ONGOING', statusCode: 42 } });
}
}
};
Déclarer une tâche récurrente dans votre fichier de configuration
export const taskContext = {};
export default {
tasks: {
// Tâche récurrente
// avec une exécution tous les jours à minuit
task1: {
callback: async () => {
taskContext.y += 2;
},
cron: '0 0 * * * *'
}
}
};Pour déterminer la valeur à donner à la propriété cron vous pouvez utiliser https://crontab.guru/.