I'm working on a product where the business logic changes based on the date and in order to help UAT testing it would be great if we could freeze the date/time on our Linux server.
Is it possible to suspend the date/time on the server from rolling over to the next day ?
Maybe the only way is to create a script which runs daily to adjust the date/time, any thoughts appreciated.
Thanks
Use LD_PRELOAD and redirect the library functions that retrieve time - An example can be found e.g. here
I think your best bet is, as you suggest, setting up a script to reset the time. There may be more exotic ways to do this but in the end the result is the same. Just be aware that there will be side-effects to "freezing" the time. Build systems that rely on file modification dates may be confused, as well as daemon processes that assume the clock is always moving forward.
Related
I am trying to implement a Django web application (on Python 3.8.5) which allows a user to create “activities” where they define an activity duration and then set the activity status to “In progress”.
The POST action to the View writes the new status, the duration and the start time (end time, based on start time and duration is also possible to add here of course).
The back-end should then keep track of the duration and automatically change the status to “Finished”.
User actions can also change the status to “Finished” before the calculated end time (i.e. the timer no longer needs to be tracked).
I am fairly new to Python so I need some advice on the smartest way to implement such a concept?
It needs to be efficient and scalable – I’m currently using a Heroku Free account so have limited system resources, but efficiency would also be important for future production implementations of course.
I have looked at the Python threading Timer, and this seems to work on a basic level, but I’ve not been able to determine what kind of constraints this places on the system – e.g. whether the spawned Timer thread might prevent the main thread from finishing and releasing resources (i.e. Heroku Dyno threads), etc.
I have read that persistence might be a problem (if the server goes down), and I haven’t found a way to cancel the timer from another process (the .cancel() method seems to rely on having the original object to cancel, and I’m not sure if this is achievable from another process).
I was also wondering about a more “background” approach, i.e. a single process which is constantly checking the database looking for activity records which have reached their end time and swapping the status.
But what would be the best way of implementing such a server?
Is it practical to read the database every second to find records with an end time of “now”? I need the status to change in real-time when the end time is reached.
Is something like Celery a good option, or is it overkill for a single process like this?
As I said I’m fairly new to these technologies, so I may be missing other obvious solutions – please feel free to enlighten me!
Thanks in advance.
To achieve this you need some kind of scheduling tasks functionality. For a fast simpler implementation is a good solution to use the Timer object from the
Threading module.
A more complete solution is tu use Celery. If you are new, deeping in it will give you a good value start using celery as a queue manager distributing your work easily across several threads or process.
You mentioned that you want it to be efficient and scalable, so I guess you will want to implement similar functionalities that will require multiprocessing and schedule so for that reason my recommendation is to use celery.
You can integrate it into your Django application easily following the documentation Integrate Django with Celery.
I am trying to understand how node-cron by kelektiv works.
Specifically, if your node app crashes, how does it remember the dates that you scheduled for an event? Does it store the jobs in a database somewhere or a somewhere locally on the machine?
Any recommended reading resources or an explanation will be very helpful.
Thank you in advance for your answer.
See this code: https://github.com/kelektiv/node-cron/blob/master/lib/cron.js
They are using methods to calculate when to send next by sendAt, how much time is left before sending next by getTimeout and then they are simply putting a setTimeout based on that in start.
It's a nice piece of code and I'll suggest you to check it out, it's very simple and written in very understandable way.
And no it doesn't stores the next time in Database, it's just setTimeout
I use gitlab-ci to run my test suite, in one test I would like to simulate a
change of system date (like, going to the day after) in order to see if
everything works as expected, for example:
redis keys are deleted (TTL)
some files are written with a new 'run number' since the date as changed
date and time calculation are ok
...
How do I simulate change of system date/time for unit-testing purposes?
EDIT: since my code base is in Python, I found freezegun and python-libfatketime; the former can probably do the trick for my Python code, and the latter is more powerful since it is intercepting system calls so theoretically I can use it for testing redis TTL if I start redis after the patches are applied.
You can override gettimeofday and clock_gettime to return any time you want. However, that would not affect pthread waiting functions with timeouts.
How about not picking the Date from System and providing a Date from a Variable you can change by a Script ?
When writing a (unit) test suite, you need to clearly define the system under test and its boundaries. In this case your application belongs to the system under test, redis does not.
Things which do not belong to the system under test should be mocked. For python start here: https://docs.python.org/3/library/unittest.mock.html
I have to deliver a command line Linux executable to a company as a trial. If they like it, they are supposed to buy it afterwards.
My worry is that the program is unprotected and they could keep it after the symbolic trial period, redistribute it or copy it.
Is there any easy way to provide basic protection to the executable? For example not being able to execute it after a given period...
Thanks!!!
Check the current timestamp on startup, and immediately exit if the trial period has elapsed. Of course, they could change the system date, but that's probably not worthwhile unless your program is very valuable to them.
If you want to go further, you could make a call to a server that you control, to determine whether the program should keep running. Or, for that matter, to a well-known NTP server.
Of course, they could still bypass that protection by running in a debugger and simply skipping the test. If they have that level of skill then there's nothing that you can do (but it's also unlikely that they'd need your programming skills in the first place).
I need to implement a daemon that needs to extract data from a database, load the data to memory, and according to this data
perform actions like sending emails or write/update files. These actions need to be performed every 30 minutes.
I really don't know what to decide. Compile a c++ program that will do the task or use scripts and miscellaneous Linux tools (sed/awk).
What will be the fastest way to do this? To save cpu and memory.
The dilemma is about marinating this process if it's script it does not need compilations and I can just drop it into any machine linux/unix
but if it's native it's more harder.
What do you think?
Use cron(1) to start your program every 30 minutes.
So called scripting languages will definitely enable you to write your program more quickly than C++. But doing this with shell and sed an/or awk, while definitly possible, is very difficult when you have to cope with all corner cases, particularly regarding strings escaping (think quotes, “&”’s “;”’s…).
I suggest you go with a more full featured “scripting” language such as Perl or Python.
Why are you trying to save CPU & Memory? Are you absolutely sure this is a real requirement (or just "premature optimization")?
Unless performance is critical, there's absolutely no reason to code such a thing in C++. It seems to be a sort of maintenance process (right?). I say write it in the highest level script language you know. Python or PHP seem like good candidates. Even if you don't know these languages, it would still take you less time to familiarize yourself with them than it would take you to do it in C++.
I'd go with a Python/Perl/Ruby implementation with a cron entry to schedule the script to run every 30 minutes.
If performance becomes an issue you can add a column to you DB that tracks the last time you ran calculations for the account and then split the processing of your records into groups of 2 or 3 or 4, running them ever 15, 10, 5 minutes respectively.
If after splitting your calculations into groups, you still have performance demands then consider C++/C/Java.
I'd still run this using cron though. No need to be a daemon unless you are providing on-demand services.