ERPNext.com Frappe Cloud Support Partners Foundation Frappe School

How to run python script once on bench start?

Hi there,

how to run a custom python script on bench start or on supervisor when in production environment?

I figured out it’s possible to add a schedule hook as “all”, but i can’t get how to stop it …

Any hint appreciated, thx

@JoEz

I see two possible ways of doing it though I am unsure which would be the best way to follow,

  • On bench start, you can run the script and at the end you store a value in frappe cache which can be checked at start of the script to disallow the execution more than once.
  • Alternatively, in hooks you can write an trigger on on_session_creation as shown below,

Hope this helps :slight_smile:

@shreyasp thx for the hint, do u have an example on first solution, run at bench start and cache?

Basically what I need kind of bench schedule running only once

Thx

@shreyasp

any chance to add 'startup' event on scheduler, other than all, daily, etc, to run script at bench startup only?

Thx

@JoEz

scheduler.py is the file which governs the scheduler_events that are declared in the hooks. In case you want to add startup as scheduler event then I don’t see a problem with it. That might be a simple code routine. Moreover, you can send a PR to Frappé as it seem to be nice addition to the system.


I will try to find some suitable example for you. Till then you can explore the scheduler_events. :smiley:

1 Like

Yup, scheduler would be the right place on where to add run at startup

I’d like to be able to send a PR …but actually not that experienced in python :sweat:

…i’m trying to learn as fast as i can …

Send your PR, the person who reviews your PR might help you in setting up the things in proper coding standard and format before merging the PR.

Hope to see your PR :smile:

It seems to be not quite strightforward …at least for me :smile:

scheduler run at every interval:

schedule.every(interval).seconds.do(enqueue_events_for_all_sites)

it should run first time how u detect that’s first run?

So I asked you do it as part of on_session_creation. So that it runs only when session for the user is created and not rest of the time.

In case you are expecting the script to run just once in a complete script’s lifetime then you will have to store some value in Database against the user record to maintain persistency and not execute the script for any sessions after the first executions.

You can keep a field in user record that can be hidden, which will maintain the script execution status.

Problem here is that if no user will connect for days script will never run …i think the solution is to schedule on “startup” event on scheduler …and set the script on hooks …what i need is to figure out how to enqueue it only on startup; seems it’s needed to pass the event to enqueue_events_for_all_sites

there’s still no way to do this, right? [execute something just once per launch]