While / Do while (infinite) loops

Hello dear wappler superstars !

I’m looking for a way to implement a while loop in server connect.
Without any luck so far…

Use case : I need to produce OHLC data from trades history.
So the script should run until all history has been exhausted and formated into OHLC data for every hour of trade.
(+ in case of script timeout, work is resumed where it stops…)

It seems like the repeater only accepts integer values and not boolean expressions ?

I tried several variants of the following repeat expression :
infinite_loop

The repeat expression provided in my previous screenshot would give 4 loops :
infinite_loop_output

I tried several variant of the boolean expression (without curly braces, true directly, etc…)
My loop goes from 1 to 21 iterations maximum.

My best workaround so far would be something silly like 99999999 as a condition.
But I don’t event know :

  • how to exit the loop in that instance.
  • what’s the max integer I can input in here.

Thank you very much for your help and have a great evening !

P.S. By the way, I’m wondering if I’m the only one who feels like a “manual for coders converting to wappler” would be very much needed ? Like a topic where we could centralise all those “little things” we are used to do in 2 minutes with code but are counter-intuitive or just not so obvious to do in Wappler.

Please give an example of what and how you are setting the boolean expression, it should work

Thanks for your help @Hyperbytes

Here is a simplified version of the current server connect action I’m trying to loop :


The idea is to run the following algorithm :

bool hasMoreHistory = true;
while (hasMoreHistory){

  // Do we have enough history to process ?
  if ( ! loadHistory() ){

    // If there is no more history to process, exit loop
    hasMoreHistory = false;   

  }else{
    // if there is, process data
    formatOHLCdata(); 
  }
}

Hello,
So what do you want to check - is it a query which returns database records?

Yes, i am guessing you are trying to use what is effectively a FOR loop as a DO WHILE and i don’t think that will work as I am not sure the repeat condition is re-evaluated after initialisation.

Not tried this but perhaps calling the server action recursively based on the condition may be a way to go, you may be able to do this via the Core Actions->Redirect to re-call the server action within a Conditional statement. I have to add I have never tried this, perhaps @Teodor may know if this can be done.
Alternatively, if this is effectively a housekeeping action, a method I have used is to run it on a single record but from a server scheduler (cron job) on a regular basis but this would depend on the number of iterations required and how time critical everything is

Hey Brian,
I will be able to answer this, as soon as i understand what @jeoff75 is trying to achieve.

If the action needed is - loop through database records and do something with them, then repeat is the way to go.

Hello @Teodor !

It’s a database record (in that case) but…
I need the condition to be evaluated at each iteration as we have another process loading data through CRON while this one will format it.

So the number of records has to be checked inside each iteration to know when to stop execution.

The repeat step will execute the steps inside, as many times as its expression value is.

So if you use your query as an expression (and say your query returns 15 records), then it will run 15 times and then it will stop.

I think the issue is he is trying to use a variable as the repeat condition, not a data query

So he sets a variable called “HasMoreHistory” to true initially (so repeat will be infinite)

He then changes the value of “HasMoreHistory” dynamically within the repeat in the hope that this will effectively exit the repeat due to the condition change

So he is trying to do this (pseudocode) :

HasMoreHistory = true
do while HasMoreHistory
if ‘condition is not satisified’ then HasMoreHistory = False // exit loop
while end

2 Likes

My intention is to run the process through cron at a regular interval but I have millions of rows to process so I obviously need a way to loop inside the script and not just relying on multiplying cron executions !

I’ll look into “re-calling” the server action, didn’t think about this.

Performance wise though, will that open and close DB connections each time ? :slight_smile:

Exactly what I’m trying to achieve. :slight_smile:

Good point, not sure but that may be the case, never thought i would miss the days when the old fashioned goto statement in Basic would be missed -:grinning:

I am not sure if that can be done exactly the way you need it.
Maybe @patrick can advise here with a solution :slight_smile:

Would be useful Teodor as I actually has the same issue last week, fortunately I was able to resolve it as i said above, single action called every 5 mins via cron job but that was only possible because numbers were low

We are currently checking the best way to integrate while, do…while and switch/case within server connect UI so they are available as steps there :slight_smile:
Will keep you updated about this.

5 Likes

Awesome, thank you very much for your help ! :slight_smile:

1 Like

Superstars

wowwww coming “switch case” , “while” and “do-while” this is reaally good news @Teodor thanks

While step has been added in Server Connect in Wappler 2.3.2

2 Likes

Awesome !
Thank you very much @Team Wappler ! :wink: