bianca
bianca

Reputation: 7214

Better solution to update Glass timeline card

I'm working on a mirror api based app for Glass, which does following:

  1. Inserts a timeline card at the very first time after installing/allowing app.
  2. Updates timeline card at a certain interval, say once a day.

I got the 1) working. For 2), I've used a timer which keeps running and updates timeline card per day. Here is my code for updating the timeline card:

def update_card(self, service, delay):
    while True:
      item_id = 'timeline card id goes here';
      timestamp = int(time.time())
      text = 'Timeline card updated for today'
      self.update_timeline_item(service, item_id, text, 'DEFAULT');
      time.sleep(delay)

And, this is how, I call it:

t = Thread(target=self.update_card, args=(self.mirror_service, 86400,))
t.start()  

Is this the only way to update timeline card periodically, or there are better ways of doing this? This solution really keeps program running forever and if server gets restarted, then there is no direct way of restarting this thread.

Upvotes: 1

Views: 136

Answers (1)

Tony Allevato
Tony Allevato

Reputation: 6429

If you're using Google App Engine to host your application, you'll probably want to set up periodic updates like this as a cron job instead of running your own thread. Their documentation about scheduling tasks with cron for Python should describe what you need.

If you're not using App Engine, then you'll need to look into what kind of support your framework or server provides for recurring tasks.

Upvotes: 3

Related Questions