Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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

share|improve this question

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

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

    
Why did it become hairy? –  Florian Margaine Jun 27 '13 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 Jun 27 '13 at 7:36
    
what does "ADHD" stand for? Attention deficit hyperactivity disorder‌​? –  gnat Jun 27 '13 at 7:43
    
@Florian - spaghetti code consisting of loads of if statements. –  Crawford Comeaux Jun 27 '13 at 7:49
    
@Zirak - I'll checkit out. –  Crawford Comeaux Jun 27 '13 at 7:50
show 2 more comments

1 Answer

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.

share|improve this answer
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
add comment

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