I am able to accomplish the tasks that I would like to have using the rails console. However, these tasks need to run periodically and I don’t want to type each command one by one to the rails console. How can I run a ruby script on my self hosted discourse instance?
In addition, the script will read a csv file, where should I put this csv file so that it is still accessible inside the docker container and will not disappear after updates?
One approach is to upload the file and then store the url in a setting. Uploading a file will prevent it getting deleted.
You can have a job run in a plugin. There are loads of examples of jobs in discourse and Pavilion plugins.
I wonder if my usecase is simpler than I explained initially. I normally do the following:
./launcher enter app
> u= User.find(1)
> some more code here
What I would like to do:
./launcher enter app
where my_code.rb contains the contents of what I execute on the rails console before.
you stated you wanted “the tasks to run periodically”.
if that is ‘for the life of the community’ potentially, then it’s worth writing your own plugin to do this. It’s not much harder than going into the rails console (messy and more risky!).
It will also allow you to store the code and manage it properly and refine it over time.
alternatively you could define a rake task, but you will need to store that somewhere, potentially also in a plugin!
Yes, it was a poor choice of wording. What I meant was the following: I need to run this job multiple times throughout the lifetime of the community. However, there is no fixed period and when it needs to be run is determined by some outside event. So I will be running the task myself multiple times without any regular time gap in between.
So rake task might be your goto solution. That’s purposely designed for ad hoc script running. I would still put that into a plugin though, as you can then manage it nicely on GitHub and you’d never forget where you put it or delete it accidentally.
Here’s a load of examples: discourse/lib/tasks at 1472e47aae5bfdfb6fd9abfe89beb186c751f514 · discourse/discourse · GitHub
You could either make it a rake task or it it in a job that you should start manually from
You can either upload your csv from the UX and pass it with a setting value as suggested, or upload it into a topic and have your script get the most recent post/upload in the topic, or upload it to /var/discourse/shared/standalone/xxx.csv and the access it as /shared/xxx.csv in the script. Or maybe you should be doing the whole thing with the API rather than uploading a csv file.
You don’t describe what you’re trying to do, just your solution that might not be the best one.