Stark9837,
@Stark9837@techhub.social avatar

Is the best way to run periodic functions, not necessary time-critical, just needs to happen in the correct hour or day,

I am running a bot in a container, but want weekly or daily things to happen. Adding this to the main loop seems inefficient, because checks will unnecessary be done.

Should I rather use a image, run the script in the container and then just have cronjobs running in the container, rather than using a Python image.

Storage, memory and processing isn't actually a problem, so using a larger and beefier image is possible.

orsinium,
@orsinium@fosstodon.org avatar

@Stark9837 It depends on lots of things, TBH. From the hashtags I assume that it's self-hosted telegram/discord bot somewhere on raspberry pi in your home. How many jobs? How often do they need to be run? Is it ok if sometimes they don't? Can they overlap? How long a job may take? Is the bot running all the time? Does it have any webhooks exposed? Is it asyncio? Does it have to be in the same container?

josephgruber,

deleted_by_author

  • Loading...
  • Stark9837,
    @Stark9837@techhub.social avatar

    @josephgruber

    Part of the app, so it adds functionality, but it is more like scripts rather than functions that will be called. Call it maintenance scripts.

    etam,
    @etam@im-in.space avatar

    @Stark9837 You might find this interesting: https://crazymax.dev/swarm-cronjob/
    I use it and it works fine.

    EngineerDaryl,

    @Stark9837 isn't it possible in this situation to run a K8s CronJob?

    ATurnOfTheNut,

    @Stark9837 running cron in the Docker container has its own drawbacks.

    I have multiple containers running automated weekly compile jobs. Rather than run cron in each container, my master loop in each just looks for the presence and content of trigger files in container /tmp. When it detects the trigger files, it removes them and runs.

    The trigger files are set by a script run by system cron.

    This is actually much lighter and simpler than putting cron in the container.

    locasta,

    @Stark9837 there are crontab images that can manage the creation of new containers on a schedule if that's any help?

    Stark9837,
    @Stark9837@techhub.social avatar

    @locasta

    Not entirely what I am searching for. The container constantly runs a script. I just want to run additional scripts periodically.

    locasta,

    @Stark9837 I think I understand.

    Looking at the willfarell/crontab image on dockerhub it appears to allow you to run commands in other, running, containers on a schedule. So could potentially access your long running python bot's container when required?

    Stark9837,
    @Stark9837@techhub.social avatar

    @locasta

    Actually, it doesn't need to access any runtime variable or data. Only the config of the basebase credentials. So I could use docker compose, run two containers, and just share it with a volume maybe

    setebos,
    @setebos@mastodon.online avatar

    @Stark9837 can't you just add RUN apt-get -y install cron into your :X container?

    Stark9837,
    @Stark9837@techhub.social avatar

    @setebos

    I am learning docker at the moment, so I actually thought that the images are too minimal to allow such commands.

    dozymoe,
    @dozymoe@mastodon.social avatar

    I think you can't run apt because it's not a Debian distro, but Alpine distro.

    @Stark9837 @setebos

    tshirtman,
    @tshirtman@mas.to avatar

    @Stark9837 @setebos when you run docker, you actually run one process, (which can start other processes) not a full OS, so installing cron is not useful is cron is not the process you run, it’s not uncommon to run a task scheduler as process, and configure it to run whatever processes you need, e.g supervisord, or you could architect your app around apscheduler or similar, or build your bot process with some thread with a sleep function to run tasks regularly, if you don’t need anything complex.

    mekuso,
    @mekuso@fosstodon.org avatar

    @Stark9837 My approach for something like that would be to use cron on the host, and make that start up the Docker container when it's actually needed and then perform stuff. Unless you have another reason why the container would need to be running anyway (like if it's a webserver or something)

    Stark9837,
    @Stark9837@techhub.social avatar

    @mekuso

    The container is running constantly because the bot constantly runs checks. However, I want the bot to generate some stuff for me, which I can totally do manually once a week, but where is the fun in that?

    So I thought, why not add to the container, allow it to run automatically in parallel with the bot without requiring additional containers.

    Also, the credentials are in a .ENV, so having the jobs running in the same environment is easier, than running an entirely different config.

    mekuso,
    @mekuso@fosstodon.org avatar

    @Stark9837 If you've already got asynchronous Python code running, it's pretty trivial to also make it run your timed things, without the need for cron. But using cron inside the container should also be an option.

    Stark9837,
    @Stark9837@techhub.social avatar

    @mekuso

    The thing is. If the bot runs in a main-loop. Then a check needs to be made to check for the time and data. Simplest why is an if-statement and ASCII time.

    Yes, such a check is very lightweight and trivial. But if the bot runs 24/7 and this check is done every 5 seconds, but is only executed once a week, it just feels weird to me.

    spodzone,

    @Stark9837 Why not add cron to your otherwise-minimalist container?

    RUN apk update \
    && apk add supercronic

    (alpine linux; adjust for other distros)

    Stark9837,
    @Stark9837@techhub.social avatar

    @spodzone

    I am actually doing this in to improve my skills, so trying something like that would be fun. Thanks!

    spodzone,

    @Stark9837 I'm right in the middle of [yelling at] a python bot in docker atm as it is. Good luck :D

  • All
  • Subscribed
  • Moderated
  • Favorites
  • python
  • DreamBathrooms
  • mdbf
  • ethstaker
  • magazineikmin
  • GTA5RPClips
  • rosin
  • thenastyranch
  • Youngstown
  • osvaldo12
  • slotface
  • khanakhh
  • kavyap
  • InstantRegret
  • Durango
  • provamag3
  • everett
  • cisconetworking
  • Leos
  • normalnudes
  • cubers
  • modclub
  • ngwrru68w68
  • tacticalgear
  • megavids
  • anitta
  • tester
  • JUstTest
  • lostlight
  • All magazines