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.


  • 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

  • Why did it become hairy? Commented Jun 27, 2013 at 7:32
  • I don't have time to offer a complete answer, but it seems like you want good concurrency. Erlang (website, wikipedia) was designed to run things like this, so give it a look over.
    – Zirak
    Commented Jun 27, 2013 at 7:36
  • what does "ADHD" stand for? Attention deficit hyperactivity disorder?
    – gnat
    Commented Jun 27, 2013 at 7:43
  • @Florian - spaghetti code consisting of loads of if statements. Commented Jun 27, 2013 at 7:49
  • @Zirak - I'll checkit out. Commented Jun 27, 2013 at 7:50

1 Answer 1


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 a 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 if 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. Commented Jun 27, 2013 at 19:54

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