databasemacosshell4d-database

Can a 4D database function be started from a unix shell script (Mac)?


We have large data sets in a 4D database. Recently we needed to automate certain tasks to export, import, or process data, which requires some connection between 4D and the unix shell (bash and zsh). We did fine whenever a 4D function had to call a shell command or shell script, using the LAUNCH EXTERNAL PROCESS instruction. But I am having trouble doing the opposite, starting a 4D function from a shell script. The on-line documentation did not give me any suggestion on how to do this.

How would I run a 4D function at a specific date+time? 4D itself does not seem to provide a way to schedule an action. OS X provides a perfectly good way to start a command at any future time (weekly or monthly). I just do not know 4D well enough to figure out how to call a 4D function from a shell script. If it was MySQL or PostgreSQL then I had no problem, because I could call the mysql or pgsql client, which can work without a GUI. Can I do something similar with 4D?


Solution

  • There are a couple of ways you could accomplish this in 4D. The way I would typically do this, would be to use the web server in 4D. Then I would trigger that service with a GET or POST using whatever scheduler (cron, windows scheduler, etc) that I like. This does require some web server licensing from 4D.

    This gives you flexibility to trigger the event at given times or from an external process. Say your data has been cleaned and is now ready to be imported, you don't have to wait until the next import time, it could just be triggered.

    Alternatively you could create a 4D method that runs in its own process. You launch the process at database started and then it would loop indefinitely, checking to see if it needed to trigger whatever action you want.

    Take a look at the New process, DELAY PROCESS, and While...End While commands. You could launch the new process to run on the server or a client depending your needs.

    Basically your On Startup method calls New Process to run your 'scheduler' method. In that method you have a While (some true condition) loop that checks to see if it should run. Then it sleeps (delay process) for a bit so it doesn't hog resources, wakes up again and runs the loop.

    If this is something you want to be able to stop programmatically you can use SET PROCESS VARIABLE to set the value of a variable in the scheduler process from outside. Then have the loop check that value to see if it should exit.