Running a Timer on Server - meteor

I have to code a timer mechanisam.
The idea is to have it countdown in sec on all clients screen & give the clients an option to increase the countdown period. The timer being common , I assume we need to run it on server.
A few ideas with sample code on Meteor Publish would be great.

Detect a future time ad get the timestamp
Save that timestamp on a collection
Create a simple timer; at each second, get he difference between the current time and the future date and display it (properly converted)
Create an event that increase the stored future timestamp by N milliseconds; the collection update should be propagated across all clients

Related

Firebase Timestamp Syncing

In my turn based online game I have a timer in-game, that ticks down from 24 hours to 0, when it reaches 0 for any player they have lost.
When a player makes their turn they write something like this to the database:
action: "not important"
timeStamp: 1670000000
What I want is for either of the two players to be able to get into the ongoing game at any time, read "timeStamp" and set the clock accordingly, showing how much time is left since the last action.
When writing to the database I am using ServerValue.TIMESTAMP (Android). I am aware of the ability to estimate the server time using ServerTimeOffset described here;
https://firebase.google.com/docs/database/android/offline-capabilities#server-timestamps
But I feel it's not always accurate when testing, so I wanted to explore if there is any other way to do this. What I really want is to get the actual server timestamp when reading the node:
timeLeft = actionTimeStamp - currentServerTime + 24h
Is this possible to do in ONE call? I am using RTDB, but I am open to moving to Firestore if it is possible there somehow.
There's no way to get the server timestamp without writing it to the database, but you can of course have each client write it and then immediately read it back.
That said, it shouldn't make much of a difference from using the initial start time that was written and the serverTimeOffset value.
For a working example, have a look at How to implement a distributed countdown timer in Firebase

How do I achieve Azure Cosmos DB item TTL from creation time?

We want to keep certain documents in our DB for a short duration. When a document is created, it doesn't matter how often its modified but it should be deleted after say X time units.
We looked at time to live in Cosmos DB but it seems to set the TTL from last edit and not creation.
One approach that we are considering is reduce the TTL everytime we update based on current time vs last update time of the document. It is hacky and inaccurate to errors due to clock skews.
Is there a better/accurate approach to achieving expiry from creation time? Our next approach will be to setup a service bus event that will trigger document deletion. Even that is more of best effort approach than an accurate TTL.
Every time you update a record you can derive a new TTL from the current TTL and the _ts field. So first get the item, derive the new TTL, and update the item together with the new (smaller) TTL.

Schedule function in firebase

The problem
I have a firebase application in combination with Ionic. I want the user to create a group and define a time, when the group is about to be deleted automatically. My first idea was to create a setTimeout(), save it and override it whenever the user changes the time. But as I have read, setTimeout() is a bad solution when used for long durations (because of the firebase billing service). Later I have heard about Cron, but as far as I have seen, Cron only allows to call functions at a specific time, not relative to a given time (e.g. 1 hour from now). Ideally, the user can define any given time with a datetime picker.
My idea
So my idea is as following:
User defines the date via native datepicker and the hour via some spinner
The client writes the time into a seperate firebase-database with a reference of following form: /scheduledJobs/{date}/{hour}/{groupId}
Every hour, the Cron task will check all the groups at the given location and delete them
If a user plans to change the time, he will just delete the old value in scheduledJobs and create a new one
My question
What is the best way to schedule the automatic deletion of the group? I am not sure if my approach suits well, since querying for the date may create a very flat and long list in my database. Also, my approach is limited in a way, that only full hours can be taken as the time of deletion and not any given time. Additionally I will need two inputs (date + hour) from the user instead of just using a datetime (which also provides me the minutes).
I believe what you're looking for is node schedule. Basically, it allows you to run serverside cron jobs, it has the ability to take date-time objects and schedule the job at that time. Since I'm assuming you're running a server for this, this would allow you to schedule the deletion at whatever time you wish based on the user input.
An alternative to TheCog's answer (which relies on running a node server) is to use Cloud Functions for Firebase in combination with a third party server (e.g. cron-jobs.org) to schedule their execution. See this video for more or this blog post for an alternative trigger.
In either of these approaches I recommend keeping only upcoming triggers in your database. So delete the jobs after you've processed them. That way you know it won't grow forever, but rather will have some sort of fixed size. In fact, you can query it quite efficiently because you know that you only need to read jobs that are scheduled before the next trigger time.
If you're having problems implementing your approach, I recommend sharing the minimum code that reproduces where you're stuck as it will be easier to give concrete help that way.

How to keep expiring items in sync on the client/server

My site allows users to post items for sale. Each item has an expiration date and time, at which point I plan on marking it as expired and removed from the view. Right now, the client has a helper function that determines the time remaining, and marks it as expired once time remaining reaches 0. The issue with this is that the item still appears on the user's view until they have reloaded the page.
I have considered running a cron job to mark expired items, but was concerned this may be too costly as it would have to run very often to be an efficient method.
Is there a more efficient way to handle this? I was hoping to get each item reactively remove itself from the list once the time expires.
I had a similar requirement in an app. I ended up using the remcoder:chronos package to make time reactive. This removed the need for an expiration key as well as any crown jobs. I used reactive time in my Collection.find() query which was returning the cursor of documents to display. At the expiration time they disappear automatically.

Time based triggers in Windows Workflow Foundation

I'm looking at using Windows WF to implement a system, which includes the need to support workflows that have either transitions or activities that need to be scheduled to execute at either a certain date/time in the future, or after a certain period of time has elapsed.
I found a question along these lines already:
Windows Workflow Foundation - schedule activities to run at certain times
Which suggests using the Delay task and calculating the duration of the delay in order to proceed at the required time. I have a few additional questions however, but don't have enough reputation to add comments so I'm posting a secondary question:
How can I implement it such that it can recover from a crash? For example, let's say the task is currently in the delay task and the process hosting the workflow engine crashes. When it is restarted, will it resume waiting in the delay task, and trigger at the time required? Do you have to do something special to get this to happen?
Another question is, again let's say a workflow instance is already mid-way through the delay task. At that point, you suddenly need to alter the time/date at which the workflow progresses to the next activity/task. Is it possible to programmatically update the duration on the in-play delay task to effect this?
If you store started instances in the SQL Store, you can add Persist activity each time you want to force save it. After crash activity will continue from last persist.
You can replace single big Delay with While cycle. In condition you check if you need to delay or not, in body you place Delay activity with short interval. You can add persist after that short Delay and instance will not wait full time again after crash (it depends on While condition, you need to store somewhere state of your instances. For example in variable).

Resources