This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Lilrock's profile picture

Shutdown Sequence

Started by Lilrock, 29 January 2016 - 03:19 AM
Lilrock #1
Posted 29 January 2016 - 04:19 AM
Hello,

I'm looking CC to introduce to their players a way to let them know when the function "os.shutdown()" is activated. This is possible since I know that the computer/turtle can startup when they have been on before powered down by the server or unloaded chunks. The following question is what I had in mind.

How do I create a shutdown script for my turtle? This script is used when the "os.shutdown()" function is used on the turtle. The os.shutdown() function should be run when the turtle is forced to turn off when it runs into unloaded chunks, chunks unload around it and when the server shuts down. Of course the server shutting down is a little tricky, since we don't want the turtle to have an infinite loop causing the server to hang while closing. So, give the turtle up to about 5 seconds to turn off. This may include computers as well!

This gives the developers more freedom in allowing their turtles not to loose focus. Alternatively, it seems like a race between setting the position and completing the movement, since we, as developers of cc don't know if the command will finish running a function or line of code before the turtle shuts down.

For example:
If (turtle.forward()) then
pos.x = pos.x + 1
end

Does the turtle complete both the if statement and what's in the if statement before shutting down or, does it complete the if statement and shutdown?

Having no idea when the computer/turtle shutsdown, we have no way of securing the required information… This problem is similar to a lock function for a file in operating systems. No two processes can access the same file at once… Having their own race program in the process.

I'm not sure how well or easily this can be done but, it would definitely be helpful for either an event or that the os.shutdown() runs the file located shutdown in the same manner that it would open startup when powered on.

Of course, this could be figured out if the terminate event fired when shutdown… I just tested and it doesn't…

Please enable something like this developers of Computer God Craft. I really like developing in Minecraft with all the unique and different mods!

Thanks for reading,

Lilrock
Lyqyd #2
Posted 29 January 2016 - 04:52 AM
This has been suggested before, and as I understand it, is unlikely to be added. If the main thing you're interested in is turtle location tracking, check out the "Location Aware Movement API", which can be found in one of the Programs subsections.
Bomb Bloke #3
Posted 29 January 2016 - 04:57 AM
Personally I've always been a fan of the GPS API when it comes to figuring out where a turtle is located. As of CC 1.76, it's now easier than ever to use - four computers rigged up with Ender Modems can effectively provide locational info to turtles across an entire dimension.
Lupus590 #4
Posted 29 January 2016 - 08:52 AM
If the main thing you're interested in is turtle location tracking, check out the "Location Aware Movement API", which can be found in one of the Programs subsections.
the one on the forums is quite old and may have bugs in new versions of CC, an updated version (by a different developer) is available here though: https://github.com/KingofGamesYami/LAMA
Lilrock #5
Posted 30 January 2016 - 05:35 PM
I'm also interested in knowing when the program will end. Let's say I'm sending orders to turtles from a computer terminal. If it shuts down, I would like it to know what the last task it was processing when it shutdown.

As I've said in the first post, if it will do a couple of commands or you can lock it from shutdown for a short time, it can save it's information then shutdown.
Lupus590 #6
Posted 30 January 2016 - 06:27 PM
overwrite os.shutdown so that it triggers an event and then delays before actually turning off the computer.

other scripts can then use this event to know to "clean up and prepare to exit"
Edited on 30 January 2016 - 05:28 PM
Bomb Bloke #7
Posted 31 January 2016 - 01:00 AM
I don't believe systems that're shut down by chunk unloads etc call os.shutdown(). Lilrock's suggesting that be changed, precisely so that such overrides can be performed.
Lilrock #8
Posted 31 January 2016 - 05:58 AM
Thanks Bomb Bloke!

I would like to know if it runs that way or that there could be changes so that it can run in that way.

As far as I know the computer/turtle doesn't shutdown like a normal computer when forced to shutdown. It's as if someone pulled the power cord on your physical machine when your house unloads chunks or when the world server shuts down (figurative meaning, no mean to start discussion on life).

It would be nice to have either ComputerCraft or Forge to include a "pre-system-exit" procedure that allows all turtles/computers to run a command to be executed. This will also give ComputerCraft and Forge some leverage on how long it has to shutdown the computer in case the player in-correctly coded their shutdown procedure.

This new procedure would also activate during chunk unload. This may hang some chunks while attempting to unload on the server until the process is complete. Not sure if this can have the same effect as when shutting down the server (time limit before force shutdown).

Lupus590: I like the idea but, I'm not sure if os.shutdown() is ever used like what Bomb Bloke mentioned. Confirmation would work if one of Computer Craft's developers came into this topic and talked about how the os.shutdown() procedure worked and "At our own risk" may change the function to shutdown the turtle/computer in a reasonable fashion while still shutting down the computer.

Thanks Lyqyd: I'm not just saying for position for the turtle but, a huge factory managed by one computer giving out all the orders. I would like to know how I can assure that the computer can save it's important information before shutdown. Even if that information is saying "Within the next 10 ticks for the chunk to unload/server to shutdown, the computer can process ~5 function calls" would be good enough for me. This would make my code ensure that each 4-6 functions need to save it's vital information to have a partially flawless factory process.

I'm kind of thinking I need to spend some time looking at the Computer Craft code and see if it's possible or jump over to Forge and see what's going on there to give a more clear explanation in detail (sudo-code and everything) to engage this suggestion.

Thanks for your input guys!

Lilrock
Bomb Bloke #9
Posted 31 January 2016 - 07:40 AM
There are some problems.

First off, ComputerCraft systems don't run simultaneously. Each is a coroutine being handled by the one Lua VM. If each gets to delay shutdown, even by a second, then the impact on server shutdown speeds could be dramatic. Malicious users would have a ball.

As for how many functions could be executed in a given time frame… well, that depends on the functions. Ten server ticks would generally allow triple digits' worth of calls, assuming they're all low-level stuff.

The code that handles shutdowns isn't open source, and distributing modified versions of the mod is against the license. I don't think this is worth your time to attempt to change yourself.

Personally I'm still of the view that systems shouldn't need to be able to recover from the exact point in time they were shut down, no matter how they were shut down. Real computers can't. I much prefer to code my turtles with the ability to make inferences.
Lupus590 #10
Posted 31 January 2016 - 02:34 PM
I would like to know how I can assure that the computer can save it's important information before shutdown.

file.flush() - Flushes the data to the specified file. (keeps the handle available afterwards)

just flush your file buffers after you write to it
Lilrock #11
Posted 31 January 2016 - 04:23 PM
After reading your comments. I really like how you guys explain the situation and concerns. That gives me one final way of completing this suggestion topic.

When computers in real life are forced to shutdown, they can start up and read the RAM for any important information. This is really nice but, Computer Craft doesn't have this feature. As long as the RAM has power or the computer was started up within a reasonable time before the RAM gets flushed.

I like the file.flush() but, I don't like keeping handlers open. I try to keep as little resources open while I program.

The final thought was similar to what someone had mentioned on another post. A new event called "Shutdown" or something. When the turtle/computer is being forced to shutdown due to unloading chunks or server shutdown the LUA VM can call this function and give each computer up to 3 seconds before shutting down. The default catch for this function would be os.shutdown() and players can change the event call as they wish. Once 3 seconds or so has gone by or maybe 500 ticks (Computer Craft's developers choice) the computer/turtle is forced to shut down even if the process wasn't completed. I'm hoping that this could be run simultaneously for each computer/turtle. If they don't need to save anything they'll take up to 2 ticks to shutdown each.

In this sense I'm trying to think of a server room full of server computers given a specific event code over the network and they all stop all functions, run a specific program and power is cut for each server in 1 minute. This doesn't matter if the server was completed their function or not.

So, this is my last thought on how to implement this suggestion… I'm probably hoping for the worst since one of the developers already redirected me to a program save my turtle's location.

Thanks for all your support guys and being on the grey areas for me to point out options and new ideas in attempt to include the feature.

Lilrock
HPWebcamAble #12
Posted 31 January 2016 - 05:13 PM
snip

The main problem is that it would require chunks to stay loaded while the computers shut down. Even if you limit the time for each computer, it can add up. Like Bomb Bloke mentioned, computers do NOT run in parallel, so the more computers you have, the longer it takes.

Especially on server shutdown, you'd have the game stuck, waiting for each computer. And what if a computer doesn't save correctly (gets cut off before it finishes)?

In fact, its usually possible to modify a program to work knowing that it might be stopped without any warning.
Lyqyd #13
Posted 31 January 2016 - 06:46 PM
I'm not a ComputerCraft developer, I'm a forum staff member. Only dan200 has final say on these things. :)/>

Originally, there were plans to make the computers suspend when their chunk unloads and resume when it loads again, so they wouldn't even notice anything had happened. This would be the best solution, and it makes the most in-game sense. Having computers boot fresh on chunk reload breaks the fourth wall a little bit, but warning them that they're about to be unloaded shatters it completely. From what I've observed in the past, Dan tends to steer away from things that break the fourth wall, which I why I think that this suggestion is unlikely to be implemented.

There are also feasibility concerns as others have mentioned, along with it being impossible to guarantee that you'd have an opportunity to resume any or all of the currently running computers with a shutdown event anyway. If the server crashed or was halted unexpectedly, no computers would have the opportunity to resume, and if an admin stopped the server and got impatient waiting for it to shut down, you could have only portions of the computers that were allowed to shut down cleanly.

In the end, the community has spent a lot of time and energy developing solutions to build resiliency against sudden shut downs into their programs. There are solutions available for many of the things one would need to do in a clean shutdown.
Lilrock #14
Posted 31 January 2016 - 08:24 PM
Thanks Lyqyd,

I'll be waiting for that moment where computer craft computers will retain memory of their "pause". That would be the best solution to this suggestion.

If you can, please lock this topic. I think it's resolved and put forward to the developer as an idea already in development.

Lilrock
Lupus590 #15
Posted 31 January 2016 - 08:38 PM
Thanks Lyqyd,

I'll be waiting for that moment where computer craft computers will retain memory of their "pause". That would be the best solution to this suggestion.

If you can, please lock this topic. I think it's resolved and put forward to the developer as an idea already in development.

Lilrock

If you really need built-in persistence then OpenComputers may be of interest for you. It does opt for a more complex progression system, which may be off putting.
Edited on 31 January 2016 - 07:38 PM
Bomb Bloke #16
Posted 31 January 2016 - 11:34 PM
When computers in real life are forced to shutdown, they can start up and read the RAM for any important information.

This is technically true, but in practise the technique is only used for cold boot attacks or emergency data recovery. Once a system is powered down nothing in RAM can be trusted 100%, and so under normal operations a computer ignores everything in there and treats the whole lot as unassigned.

You might have an interest in the mod OpenComputers. It uses a different Lua VM, one that can handle the sort of resuming you're after.
kolsto #17
Posted 10 February 2016 - 09:42 PM
Argh im stupid… if you manually save and re-load it, then data might have been changed in between… so you load old data xD

So when i load my databases the next day from manual saved cache…. yesterdays work "seems" lost, because i did not auto-save swap on shutdown. xD

Im sry the hole manual idea seems pointless now from this perspective…

Also when you auto-save to a swap on shutdown… i would have my actual databases…but…
on the next boot some data might have changed BEFORE the cache-driver loaded… so swapping cache-data might be dangerous, i dont know.

Do the developers think auto-save cache to swap/image-file and revoke afte rboot would work?

I dont think so.

So maybe completely forget it xD
COOLGAMETUBE #18
Posted 28 March 2016 - 05:34 PM

nativeShutdown = os.shutdown()
os.shutdown = function()
  print("Computer is shuttting down!")
  nativeShutdown()
end
Anavrins #19
Posted 28 March 2016 - 06:43 PM

nativeShutdown = os.shutdown()
os.shutdown = function()
  print("Computer is shuttting down!")
  nativeShutdown()
end
Though this won't affect ctrl+s, chunk unloading or server's shutdown.
COOLGAMETUBE #20
Posted 30 March 2016 - 10:47 AM

nativeShutdown = os.shutdown()
os.shutdown = function()
  print("Computer is shuttting down!")
  nativeShutdown()
end
Though this won't affect ctrl+s, chunk unloading or server's shutdown.

This affect Ctrl+S!

If you hold Ctrl+S then the computer run the function os.shutdown()

With the code i posted, you overwrite os.shutdown.
SquidDev #21
Posted 30 March 2016 - 11:22 AM
This affect Ctrl+S!
If you hold Ctrl+S then the computer run the function os.shutdown()

Changing os.shutdown doesn't change anything. All shutdown code is in Java: it never touches the Lua environment and so modifying functions will have no effect. There may be some emulators where this does work, but that is not the case with normal CC.
Edited on 30 March 2016 - 09:22 AM
Bomb Bloke #22
Posted 30 March 2016 - 11:28 AM
If you hold Ctrl+S then the computer run the function os.shutdown()

If you hold Ctrl+S, the system doesn't run anything. It simply turns off.

As I explained a couple of months back, this is what Lilrock would like to see changed.
wilcomega #23
Posted 03 April 2016 - 12:42 AM
If you hold Ctrl+S then the computer run the function os.shutdown()

If you hold Ctrl+S, the system doesn't run anything. It simply turns off.

As I explained a couple of months back, this is what Lilrock would like to see changed.

New post in Ask A Pro:
PLS HELP my computer wont turn off!!! whats happening??!?! HALP
EveryOS #24
Posted 05 April 2016 - 05:36 PM

local oldDown = os.shutdown
function _G.os.shutdown()
  print('Shutting down')
  if os.pullEvent ~= coroutine.yield then
	write('You can probably get away with terminating this program')
  end
  sleep(5)
  oldDown()
end
Edited on 06 April 2016 - 03:15 PM