A process initializer runs when a sandbox process of your account starts, before it serves any of your code. It is the place for work which must exist in every process : a subscription to a message broker, a client kept for the life of the process, a warm cache.
Page
APIs & Logic → Initializers : name, order, timeout in minutes, active, and the code.
Runs
In every sandbox process, when it starts ; the active initializers in the order given, one after the other.
Code
The function main of any language, with the same g as a custom API.
import*asTfrom'types';asyncfunctionmain(g:T.IAMGlobal){// Read once per process : every request this process serves finds them in g.shared.constrates=awaitg.sys.db.getAll({instance:'mysql_8',database:'shop',collection:'rates'});g.shared.rates=rates;g.logger.log(`${rates.length} rates loaded`);}module.exports=main;
fromamimportGasyncdefmain(g:G):# Read once per process : every request this process serves finds them in g.shared.rates=awaitg.sys.db.get_all('mysql_8','shop','rates')g.shared['rates']=ratesg.logger.log(len(rates),'rates loaded')
<?phpfunctionmain(AmG$g){// Read once per process : every request this process serves finds them in $g->shared.$rates=$g->sys->db->getAll('mysql_8','shop','rates');$g->shared->rates=$rates;$g->logger->log(count($rates),'rates loaded');returnnull;}
filestaticclassApi{publicstaticasyncTask<object?>Main(Gg){// Read once per process : every request this process serves finds them in g.Shared.varrates=awaitg.Sys.Db.GetAll(newGetAll("mysql_8","shop","rates"));g.Shared["rates"]=rates;g.Logger.Log(rates.Count,"rates loaded");returnnull;}}
packagecodeimport"apimaker/am"funcMain(g*am.G)(any,error){// Read once per process : every request this process serves finds them in g.Shared.rates,err:=g.Sys.Db.GetAll(am.GetAll{Instance:"mysql_8",Database:"shop",Collection:"rates"})iferr!=nil{returnnil,err}g.Shared.Set("rates",rates)g.Logger.Log(len(rates),"rates loaded")returnnil,nil}
useam::{Errors,G,GetAll,Value,json};pubfnmain(g:&mutG)->Result<Value,Errors>{// Read once per process : every request this process serves finds them in g.shared.letrates=g.sys.db.get_all(GetAll::new("mysql_8","shop","rates"))?;g.logger.log(format!("{} rates loaded",rates.len()));g.shared.set("rates",json!(rates));Ok(Value::Null)}
importam.G;importam.GetAll;importam.JsonObject;importjava.util.List;classApi{staticObjectmain(Gg){// Read once per process : every request this process serves finds them in g.shared.List<JsonObject>rates=g.sys.db.getAll(newGetAll("mysql_8","shop","rates"));g.shared.put("rates",rates);g.logger.log(rates.size(),"rates loaded");returnnull;}}
importam.Gimportam.GetAllfunmain(g:G):Any?{// Read once per process : every request this process serves finds them in g.shared.valrates=g.sys.db.getAll(GetAll("mysql_8","shop","rates"))g.shared["rates"]=ratesg.logger.log(rates.size,"rates loaded")returnnull}
An initializer is also where what lives as long as the process starts : the client of a message queue which listens to a topic (MQTT, Kafka, RabbitMQ…), with the package of its language added on the Sandbox Settings page.
Other code runs on an action : an HTTP call, a schedule, an event. An initializer runs on its own, once per process, so keep it light and idempotent.
The number of sandbox processes is set on the Sandbox Settings page : each one runs the initializers.
Timeout in minutes is how long each process waits for the initializer, every time it runs it (empty : sandboxReqTimeout of the configuration, 13 seconds). A process whose initializer did not end in time goes on to the next one, and then serves your code : keep what lasts as long as the process (a listener, a subscription) out of the promise the initializer returns.