Regular scheduled jobs

Any component used by your application can have jobs that need to be run at regular intervals. Executing the following runs all jobs that are scheduled to run:

reahl runjobs etc

The tutorial.jobsbootstrap example needs its Address.clear_added_flags() to run automatically, once a day.

class Address(Base):
    __tablename__ = 'jobsbootstrap_address'

    id            = Column(Integer, primary_key=True)
    email_address = Column(UnicodeText)
    name          = Column(UnicodeText)
    added_today   = Column(Boolean, nullable=False, default=True)

    @classmethod
    def clear_added_flags(cls):
        for address in Session.query(cls).filter_by(added_today=True):
            address.added_today = False

    @exposed
    def fields(self, fields):
        fields.name = Field(label='Name', required=True)
        fields.email_address = EmailField(label='Email', required=True)

    @exposed
    def events(self, events):
        events.save = Event(label='Save', action=Action(self.save))

    def save(self):
        Session.add(self)

To register this job with the component, add a <schedule> tag in the .reahlproject file:

 <schedule locator="reahl.doc.examples.tutorial.jobsbootstrap.jobsbootstrap:Address.clear_added_flags"/>

A job, such as Address.clear_added_flags() should include code that makes sure it only does its work when necessary. You can then use your operating system tools (such as cron) to run reahl runjobs etc very regularly – say once every 10 minutes. Each scheduled job will now be invoked regularly, and can check at each invocation whether it is time for it to do its work, or whether it should ignore the current run until a future time. This way, all jobs can get a chance to be run, and be in control of when they should run.