Schedule daily function in Corona - lua

Is there a way you can schedule a function to run daily in a Corona SDK App?
function DailyFunction()
print("daily function")
end -- run this function every 24 hours

A literal answer to your question would be:
function DailyFunction()
print("daily function")
end -- run this function every 24 hours
timer.performWithDelay( (1000 * 60 * 60 * 24), DailyFunction )
but I suspect you aren't expecting the user to keep the app open all the time which this would require.
Quite a bit more complex but perhaps what you'll need to do is to schedule a local notification 24 hours in the future. Details of how to do that can be found at http://docs.coronalabs.com/guide/events/appNotification/index.html#ios
Remember that you'll need to reschedule the notification each time it fires.

Related

After end of daylight saving time, no CRON triggers occur except one

In our .NET application, we have several hourly job triggers:
XX:00
XX:05
XX:10
XX:15
During the end of DST (25 October 2020 at 03:00) the triggers stopped working except the first trigger (XX:00).
Hour 00 did well. Hour 01 also did well. But starting with hour 02 only the trigger at 02:00 worked. The three other triggers never got triggered, even later during the day it didn't work. They completely stopped working except the trigger for XX:00 kept working.
I can't clarify why it behaved like this, because DST ended at 03:00 so I think 02:00 should have worked normally until 03:00. Probably after 03:00 I should have seen some misbehaviour but the misbehaving happened at hour 02 as I mentioned earlier.
I'm very flexible in solving this issue. I can change the schedules to semi-hourly or to 2-hourly if it solves the issue. The 4 triggers just have to keep triggering instead of stopping completely... It doesn't matter when they run, as long as they trigger approximately every hour continuously. But SimpleTrigger isn't the one that I want to use, because then all the jobs start immediately in parallel when service starts and I don't want that...
I know there are similar questions here on SO, but in most of those questions the timing is stubborn. In my case, I'm very flexible and I'm looking for the easiest workaround.
Is there any quick workaround to solve this issue? Can I for example completely ignore DST and just use UTC?
Using library version: 3.0.7
The CRON triggers:
0 0 * ? * * *
0 5 * ? * * *
0 10 * ? * * *
0 15 * ? * * *

Rufus scheduler fires at the scheduled time (expected ) and fires again after completion of previous run

I have scheduled rufus cron to run a method (a method to post a complex query in remote DB, retrieves result and, do some processing ) every hour and 99% of time the methods completes execution <1 hr.
Rufus fires the method fine at every expected time, but the problem is some times (1% fo time) the method wont complete in a hour; the scheduler fires the second one at the expected time and fires the second one again, on completion of previous run.
For example,
pin_sampling_job_scheduler = Rufus::Scheduler.new
pin_sampling_job_scheduler.cron("22 * * * *") do
puts "sampling starts"
DatafetchRedshift.pin_sampling_job
end
for hour = 00, the method fires at 22nd min(as expected and assume the job is running for 1.5 hour) For hour =01, method is fires at 22nd min and again at 52nd min (after completion of first run). Why is it happening like this? Any help or suggestion will be appreciated.
Rufus version -3.3.4
Rails - 4.1.6
Ruby 2.2.0
P.S - I wont be able to change the scheduler runs to be more than an hour, since 99% it completes
https://github.com/jmettraux/rufus-scheduler#overlap--false
pin_sampling_job_scheduler = Rufus::Scheduler.new
pin_sampling_job_scheduler.cron("22 * * * *", overlap: false) do
ts = Time.now.strftime('%Y%m%d%H%M')
puts "#{Time.now} - sampling #{ts} starts..."
DatafetchRedshift.pin_sampling_job
puts "#{Time.now} - sampling #{ts} stopped."
end
for hour = 00, the method fires at 22nd min(as expected and assume the job is running for 1.5 hour) For hour =01, method is fires at 22nd min and again at 52nd min (after completion of first run). Why is it happening like this?
You can report issues at https://github.com/jmettraux/rufus-scheduler/issues (please read https://github.com/jmettraux/rufus-scheduler#getting-help before filing an issue)

Countdown timer for Lua script

I am completely new to the Lua programming language, I've only been working with it a few days in total. Although, I do have some experience in Python, C# and Ada.
I am currently trying to make a racing game, one of the aspects of the game I'm trying to include is a countdown timer that ends the game after 90 seconds, then returning to a high score screen, regardless of whether or not the player has eliminated all of their lives already. I haven't a clue what functions to include nor where the place the text in the main script. Some advice would be much appreciated.
When the game starts do:
local startTime = os.time()
local endTime = startTime+90
Then regularely do:
if os.time() >= endTime then
-- exit game
-- return to high score screen
end
until the game ends. Probably this would be in a callback function that gets called regularely. Depending on how your framework works, a loop might also work.
Use timer.performWithDelay to call a specified function after a delay.
Example
local function countdown( event )
print( "listener called" )
end
timer.performWithDelay( 1000, countdown, 90 )

How to implement a stopwatch in Lua?

How would you make a stopwatch in Lua?
I don't know Codea at all I'm afraid and you will most likely find that there are functions provided by that library for gettime and sleep. However as a pure Lua option (with the proviso that you use luasocket) the following code implements an example that could be built on.
socket = require('socket')
-- Define the stop watch
local start_time
function start()
-- Start the stop watch
start_time = socket.gettime() - 3800
end
function seconds_ellapsed()
-- Return the number of seconds since the stop watch was started
return socket.gettime() - start_time
end
-- As an example run the stop watch indefinately
start()
while true do
-- Get the time ellapsed and convert it to hours, minutes and seconds
ellapsed = seconds_ellapsed()
hours = math.floor(ellapsed / 3600)
minutes = math.floor((ellapsed - (hours * 3600)) / 60)
seconds = math.floor((ellapsed - (hours * 3600) - (minutes * 60)))
-- Print the time ellapsed to the command line
print(hours .. 'h', minutes .. 'm', seconds .. 's')
-- Wait a second between each update
socket.sleep(1)
end
You could look to use os.clock also but Lua has no builtin mechanism for setting the thread to sleep for a period (which was required for my example so I choose to use luasocket). There is a useful article on possible approaches for implementing sleep in lua here: http://lua-users.org/wiki/SleepFunction
Here is a very basic stopwatch, it starts/resets when the user taps the screen. This example shows elapsed minutes and seconds. If you want to count milliseconds, you can use ElapsedTime instead of os.time() and compute the number of hours, minutes etc. yourself instead of os.date(). Also, I don't have my iPad so there may be a bug.
function setup()
fontSize(20)
background(100, 120, 160)
fill(255)
toggle_timer()
end
function toggle_timer()
timer_on = not timer_on
if timer_on then
start = os.time()
end
end
function draw()
if timer_on then
text(os.date("%M:%S", os.difftime(os.time(), start)),
WIDTH / 2, HEIGHT / 2)
end
end
function touched(touch)
if touch.state == BEGAN then
toggle_timer()
end
end

What to use in order to count down in seconds from a fixed minute in Lua?

I am trying to make a countdown from a fixed minute like 1:00 to 0:00 in Lua.
How can I do that?
I assume you are using Corona SDK.
Corona provides a simple way
local time = 60
local function decreaseTime()
time = time-1
print(time)
end
timer.performWithDelay(1000,decreaseTime,60)
have a read here
http://developer.anscamobile.com/reference/index/timerperformwithdelay

Resources