-
I created a function to handle click button like this const [published, setPublished] = useState(false);
const handlePublishFlows = async () => {
const newPublished = !published;
setPublished(newPublished);
await axios
.get(`/api/schedule?published=${newPublished}`, {})
.then((res) => {
toast.success(res.data.message);
});
}; And this is an api export async function GET(request) {
const { searchParams } = new URL(request.url);
const published = searchParams.get("published") === "true";
console.log(published);
console.log("Before job instantiation");
const job = new CronJob("* * * * * *", function () {
const d = new Date();
console.log("Every second:", d);
});
if (published){
job.start();
return NextResponse.json({
message: "Schedule started",
});
}
else{
job.stop();
return NextResponse.json({
message: "Schedule stopped",
});
}
} My problem is the job still running when i click button to change "published" to "false". How can i fix it ? |
Beta Was this translation helpful? Give feedback.
Answered by
sheerlox
Mar 6, 2024
Replies: 1 comment 5 replies
-
Looks like you're creating a new job on every button click. You would need a way to manage the jobs in your backend so you can find the right one and call the |
Beta Was this translation helpful? Give feedback.
5 replies
Answer selected by
nsnhan127
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Looks like you're creating a new job on every button click. You would need a way to manage the jobs in your backend so you can find the right one and call the
stop
/start
methods on it. If you have only one job to manage, maybe moving the job instantiation outside of theGET
function declaration would do the trick, though not in a very clean way.