APScheduler won't schedule jobs

22 Views Asked by At

I have two scripts

this one is to start Flask and the ASP scheduler

from flask import Flask
from messaging import sendmsg
from apscheduler.schedulers.background import BackgroundScheduler
from datetime import datetime, timedelta


app = Flask(__name__)


scheduler = BackgroundScheduler()

def process_webhooks(data):
    if data['event'] == 'order.created':
        sendmsg('order created')
    else:
        print(f'recieved {data["event"]}')
        scheduled_time = datetime.now() + timedelta(seconds=5)
        
        scheduler.add_job(sendmsg, 'date', run_date=scheduled_time, args=['other event'], misfire_grace_time=3600)

def sendmsg(msg):
    # Define the file path
    log_file = "file.txt"
    
    # Get the current timestamp to prepend to each message
    timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    
    # Construct the log message
    log_msg = f"{timestamp} - Sending message: {msg}\n"
    
    # Open the log file in append mode
    with open(log_file, "a") as file:
        # Append the message to the file
        file.write(log_msg)
    
    # Print the message to the console (optional)
    print(log_msg)

if __name__ == '__main__':
    scheduler.start()
    app.run(debug=True)

this one to simulate the sending of the webhooks:

from app import process_webhooks


process_webhooks({'event': 'order.updated', 'data': {'status': {'name': 'pending'}}})

when I run the second script on the terminal I get: recieved order.updated

and nothing else happens.

What I'm trying to achieve is to be able to schedule the tasks when the webhooks is received at a specific time the user picks

0

There are 0 best solutions below