Batch automation

22 views
Skip to first unread message

Fabio Lenzarini

unread,
Aug 4, 2025, 7:02:54 AMAug 4
to Jam.py Users Mailing List
Hello everyone,
I am trying to create a function to schedule automatic functions with jam.py V7.

The idea is to implement apscheduler to schedule the automatic sending of emails.

For example:
a function that automatically generates customer statements every night and sends payment reminders by email.

I installed apscheduler and tried to modify the local “server.py” file to launch a function contained in the local “wsgi.py” file, but I can't seem to “hook up” to the items.

This is the modified server.py file

#!/usr/bin/env python
print(f"User Guide: https://jampy-docs-v7.readthedocs.io/")
if __name__ == '__main__':
    from jam.wsgi import create_application
    from jam.wsgi_server import run

    application = create_application(__file__)        
   
    # start sched

    from apscheduler.schedulers.background import BackgroundScheduler
    from apscheduler.triggers.interval import IntervalTrigger
    import atexit

    # Importa la tua funzione
    from wsgi import start_batch
   
    # from jam.wsgi import App    
    # esegui_task = App.esegui_task    
   
    # Setup APScheduler
    scheduler = BackgroundScheduler()
    scheduler.add_job(
        func = start_batch,
        trigger = IntervalTrigger(seconds = 60),
        id = 'stark_task_job',
        name ='Execute esegui_task() every 60 seconds',
        replace_existing = True
    )
    scheduler.start()
    atexit.register(lambda: scheduler.shutdown())

    # end sched
   
    run(application)



and this is wsgi.py  

from jam.wsgi import create_application

application = create_application(__file__)



# start sched
def start_batch():
    print("wsgi.start_batch")
   
    task_id = 12

    item = application. ??????  .items(['batch_job'])  # Sostituisci con il nome corretto

    item.open()  # Apre il dataset (eventualmente con filtro vuoto)
    for r in item:
        print(r['nome_campo'])  # Sostituisci con un campo esistente
 
   
#    if 'batch_job' in app.items:
#        batch_job = app.items['batch_job']
       

def new_func():
    return create_application(__file__)
        # puoi aggiornare lo stato del task ad "eseguito", ecc. 



I'm not an expert in Python... in fact, I know very little about it.

Can anyone give me an idea?

What should I put where there are ????

If I succeed, I'll post everything.

Thanks

Translated with DeepL.com (free version)

Dean D. Babic

unread,
Aug 4, 2025, 7:42:34 AMAug 4
to Jam.py Users Mailing List
Ciao Fabio, 
You just need a OS scheduler - a CRON job on Linux, or similar on Wintel.
In that cron job, u specify running every minute.
And you run something like:
curl -k https://jampy.pythonanywhere.com/ext/bla -d '{"id": "1"}' -H  "Content-Type: application/json"
Then you develop on_ext_request. For above it is:

def on_ext_request(task, request, params):
    reqs = request.split('/')
    if reqs[2] == 'bla':
        users = task.customers.copy(handlers=False)
        users.set_where(id=params['id'])
        users.open()
        if users.rec_count == 1:
                return {
                    'id': users.id.value,
                    'firstname': users.firstname.value,
                }
I did have a similar curl to reset the app data to default every 24h at midnight.

Ciao
D.

Fabio Lenzarini

unread,
Aug 4, 2025, 7:51:36 AMAug 4
to Jam.py Users Mailing List
Where I can write:  on_ext_request?

Dean D. Babic

unread,
Aug 4, 2025, 8:16:40 AMAug 4
to Jam.py Users Mailing List
On Task/Server Module

Fabio Lenzarini

unread,
Aug 4, 2025, 10:19:48 AMAug 4
to Jam.py Users Mailing List
Thanks Dean

I've add:
def on_ext_request(task, request, params):
   
    print("on_ext_request")

   
    reqs = request.split('/')
    if reqs[2] == 'bla':
        batch_job = task.batch_job.copy(handlers=False)
        batch_job.set_where(id=params['id'])
        batch_job.open()
        if batch_job.rec_count == 1:
            return {
                'id': batch_job.id.value,
                'firstname': batch_job.name.value,
            }
on my task server module

then I've write this python code:


import requests
# url = "http://127.0.0.1:8080/ext/bla -d '{\"id\": \"1\"}' -H  \"Content-Type: application/json\""



url = "http://127.0.0.1:8080/ext/bla"
data = {"id": 1}


try:
#    response = requests.get(url)

    response = requests.post(url, json=data)
   
    # Controlla il codice di stato
    if response.status_code == 200:
        print("Risposta ricevuta:")
        print(response.text)  # o .json() se restituisce JSON
    else:
        print(f"Errore: codice {response.status_code}")
except requests.exceptions.RequestException as e:
    print(f"Errore di connessione: {e}")


but I get this error:

User Guide: https://jampy-docs-v7.readthedocs.io/
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on all addresses (0.0.0.0)
 * Running on http://127.0.0.1:8080
 * Running on http://192.168.5.178:8080
Press CTRL+C to quit
127.0.0.1 - - [04/Aug/2025 16:12:07] "GET /ext/bla%20-d%20'{"id":%20"1"}'%20-H%20%20"Content-Type:%20application/json" HTTP/1.1" 500 -
Traceback (most recent call last):
  File "C:\Python310\lib\site-packages\werkzeug\local.py", line 232, in application
    return ClosingIterator(app(environ, start_response), self.cleanup)
  File "C:\Python310\lib\site-packages\werkzeug\middleware\shared_data.py", line 249, in __call__
    return self.app(environ, start_response)
  File "C:\Python310\lib\site-packages\jam\wsgi.py", line 264, in __call__
    return self.on_ext(request)(environ, start_response)
TypeError: 'NoneType' object is not callable
on_ext_request
127.0.0.1 - - [04/Aug/2025 16:14:58] "POST /ext/bla HTTP/1.1" 200 -

the text "on_ext_request" are printed, so, the function run fine, but what is the error?

a wrong/old version of werkzeug ?

Fabio Lenzarini

unread,
Aug 4, 2025, 10:29:20 AMAug 4
to Jam.py Users Mailing List
No more error.. i' don't know why.. :-)

--
You received this message because you are subscribed to the Google Groups "Jam.py Users Mailing List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jam-py+un...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/jam-py/34e67e02-f3fb-4f8a-93cf-ddc83ef02342n%40googlegroups.com.

Drazen Babic

unread,
Aug 4, 2025, 10:45:19 AMAug 4
to Fabio Lenzarini, Jam.py Users Mailing List
No need for Python client code.
Curl command is on Windows 11 now.



You received this message because you are subscribed to a topic in the Google Groups "Jam.py Users Mailing List" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jam-py/ZOUPSSUj8l0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jam-py+un...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/jam-py/CAFnDzjP90u3brT3Mwqt6Da6yiq3D-2epzYUeGXA0Zd%2BgpBSyfQ%40mail.gmail.com.

Fabio Lenzarini

unread,
Aug 4, 2025, 11:11:30 AMAug 4
to Drazen Babic, Jam.py Users Mailing List
Haha, thanks! 
Reply all
Reply to author
Forward
0 new messages