6

I've developed an ADHD management system for myself that's attempting to change multiple habits at once. I know this is counter to conventional wisdom, but I've tried the conventional for years & am now trying it my way. (just wanted to say that to try and prevent it from distracting people from the actual question)

Anyway, I'd like to write something to run on a remote server that monitors me, helps me build/avoid certain habits, etc.

What this amounts to is a system that:

  • runs 24/7
  • may have multiple independent tasks to run at once
  • may have tasks that require other tasks to run first
  • lets tasks be scheduled by specific time, recurrence (ie. "run every 5 mins"), or interval (ie. "run from 2pm to 3pm")

My first naive attempt at this was just a single PHP script scheduled to run every minute by cron (language was chosen in order to use a certain library, but no longer necessary). The logic behind when to run this or that portion of code got hairy pretty quick.

So my question is how should I approach this from here? I'm not tied to any one language, though I'm partial to python/javascript.

Thoughts:

  • Could be done as a set of scripts that include a scheduling mechanism with one script per bit of logic...but the idea just feels wrong to me.

  • Building it as a daemon could be helpful, but still unsure what to do about dozens of if-else statements for detecting the current time

closed as too broad by Tulains Córdova, Bart van Ingen Schenau, Kilian Foth, Martijn Pieters, Dan Pichelman Jun 30 '13 at 22:55

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

9

I would go about it like this:

  1. Every Task is encapsulated in its own class
    • A task only contains of a single method: Execute. This method executes the action of the task.
    • A task is not responsible for checking its requirements.
  2. Scheduling is done via Jobs
    • A job references a task
    • A job defines when its task is allowed to run
    • You would have one class per job type, i.e. you would have
      • a RecurringJob ("every 5 minutes")
      • an IntervalJob ("from 2pm to 3pm")
    • A job is an implementation of the Tester/Doer pattern.
  3. You would have an Job Registry. That is basically the scheduler that executes the jobs.
    • It periodically checks (every 10 seconds, every minute or whatever you need) all registered jobs of they can be executed.

The requirement "may have tasks that require other tasks to run first" can be implemented using the Composite pattern and passing the tasks in the correct order.

The requirement "may have multiple independent tasks to run at once" can be implemented by making the execution of a task asynchronous.

  • 1
    +1 This is exactly what we (actually I) have done for a command scheduler in a server application. Slightly different application/subject, but essence and solution are the same: incoming commands wrapped in scheduling items which are queued and taken off the queue by a scheduler that checks the scheduling item for its schedule and time constraints (execution window) and when that passes sends the item onto the execution thread(s). Recurrence is implemented in the scheduler simply by adding a clone of the scheduling item to its queue with an execution window that starts at now + interval. – Marjan Venema Jun 27 '13 at 19:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.