Forums / General Questions / Automate (cron job) "Down for Maintenance"?

Automate (cron job) "Down for Maintenance"?

Locked

Views: 3,211

Results 1 to 11 of 11
This thread is locked. New replies are disabled.
24 Oct 2008, 7:50 PM
#1
zcnb avatar

zcnb

Zen Follower

Join Date:
Jun 2008
Posts:
338
Plugin Contributions:
0

Automate (cron job) "Down for Maintenance"?

Since store backup is crucial for business, I have created a cron job that does just that - daily. It works very nicely and saves me time by allowing me to avoid doing this manually, every day.

The problem is... if I understand correctly, in order for the database backup to be done properly, no activity must be going while that backup takes place (currently it takes less than a minute, but expected to take longer when the database grows as customers join and transactions are filled).

Which means that if I want to performed scheduled backups, the "Down for Mainteances" flag must be set as well, prior to the actual mysqldump command.

Is it possible to set that "Down for Mainteances" flag to 'true' from a shell script (run by a cron job)?

Thanks,
Daniel

24 Oct 2008, 9:56 PM
#2
kobra avatar

kobra

Black Belt

Join Date:
Aug 2005
Location:
Arizona
Posts:
31,500
Plugin Contributions:
4

Re: Automate (cron job) "Down for Maintenance"?

Supposing you cron is running a shell script - - add to it

add-locks other vars lock tables

add-locks takes care of unlocking

Zen-Venom Get Bitten

24 Oct 2008, 11:55 PM
#3
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,515
Plugin Contributions:
176

Re: Automate (cron job) "Down for Maintenance"?

zcnb:

Is it possible to set that "Down for Mainteances" flag to 'true' from a shell script (run by a cron job)?
Yes ... run a SQL statement to update the configuration_value to 'true' for configuration_key="DOWN_FOR_MAINTENANCE" in the configuration table.
Set it to 'false' again when done the backup.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

30 Oct 2008, 3:17 AM
#4
zcnb avatar

zcnb

Zen Follower

Join Date:
Jun 2008
Posts:
338
Plugin Contributions:
0

Re: Automate (cron job) "Down for Maintenance"?

DrByte:

Yes ... run a SQL statement to update the configuration_value to 'true' for configuration_key="DOWN_FOR_MAINTENANCE" in the configuration table.
Set it to 'false' again when done the backup.

OK, I finally got back to this challenge and, if I understand you correctly, you mean doing the following in a PHP script, right?

$db->Execute("update " . TABLE_CONFIGURATION . "
              set configuration_value = 'true', last_modified = '" . NOW . "'
              where configuration_key = 'DOWN_FOR_MAINTENANCE'");

Do you find any problem with the above?

If this is OK, then I am now facing another challenge:

It's great that I can automagically place the site in DOWN_FOR_MAINTENANCE mode from a cron job, but... what if at that exact moment there is a customer logged in performing a transaction?

Is that already handled by Zen Cart's code when the site is placed into DOWN_FOR_MAINTENANCE mode?

Or must I poll Who's Online until none is logged in?

30 Oct 2008, 7:54 AM
#5
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,515
Plugin Contributions:
176

Re: Automate (cron job) "Down for Maintenance"?

You want to use **now() **for your date, not NOW.

And, no, Zen Cart will be immediately down-for-maintenance regardless of customer shopping activity when you flip that switch. You'll want to do your own due diligence for handling active customers.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

4 Nov 2008, 5:42 PM
#6
zcnb avatar

zcnb

Zen Follower

Join Date:
Jun 2008
Posts:
338
Plugin Contributions:
0

Re: Automate (cron job) "Down for Maintenance"?

DrByte:

You want to use **now() **for your date, not NOW.
DrByte, thank you for the correction. I finally got to implementing this and I realize that this is not as simple as I had hoped:

First, this PHP script needs to be run from the shell script that is invoked by the cron job. So, in a way, it is standalone.

However, it is clear that it cannot be standalone, otherwise $db and other globals will not be accessible.

What approach would you recommend to tackle this challenge? Would an observer class be an overkill for this?

DrByte:

And, no, Zen Cart will be immediately down-for-maintenance regardless of customer shopping activity when you flip that switch. You'll want to do your own due diligence for handling active customers.
Which means a wait loop inside that cron job, waiting for the last customer to become inactive, right?

Do you know of sample code that monitors customer activity so that I will know from where to start?

This is becoming unexpectedly tricky and I am beginning to think that perhaps it would take me less time to simply backup the site manually every night. :blink:

5 Nov 2008, 7:26 PM
#7
kobra avatar

kobra

Black Belt

Join Date:
Aug 2005
Location:
Arizona
Posts:
31,500
Plugin Contributions:
4

Re: Automate (cron job) "Down for Maintenance"?

zcnb,

This is becoming unexpectedly tricky and I am beginning to think that perhaps it would take me less time to simply backup the site manually every night.
A backup is a snap shot in time. If immediately after you backup or there is activity - - or ALL activity between backups will not be contained and have to be recreated manaually anyway. IF and only if you have a larger issue of some type. So the steps you are attempting are mostly for naught.

Zen-Venom Get Bitten

6 Nov 2008, 10:54 AM
#8
zcnb avatar

zcnb

Zen Follower

Join Date:
Jun 2008
Posts:
338
Plugin Contributions:
0

Re: Automate (cron job) "Down for Maintenance"?

kobra, I agree with you - up to the "for naught" part. :smile:

If I am serious about operating my web store in a business-like manner, I have to have a contingency plan in place.

There is no way to guarantee that in the event of a disaster (web host server crash, hacking, etc.) all data will be saved.

However, what I can do is to attempt to minimize damage. In the case of a Zen Cart based store, this means loosing minimal amount of data (mainly customers and transactions).

If I do nightly backups, then the most I can loose is 24-hours worth of data. If I do weekly backups then the most I can loose is 7-days worth of data.

IMHO, backing up my data once a day is not an overkill. I would like, though, to automate this so that I don't have to wake up at 2:00 AM every night to do this manually.

In light of the above, do you still think such work is "for naught" ?

6 Nov 2008, 11:30 AM
#9
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,515
Plugin Contributions:
176

Re: Automate (cron job) "Down for Maintenance"?

How busy is your site at 2am?
Why do you believe you need to take the site down-for-maintenance in order to do a backup? If your daily backups are "precautionary", maybe the only time you need to ensure nobody is logged-in is during a weekly or monthly backup that you're saving for archive purposes?

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

6 Nov 2008, 1:05 PM
#10
zcnb avatar

zcnb

Zen Follower

Join Date:
Jun 2008
Posts:
338
Plugin Contributions:
0

Re: Automate (cron job) "Down for Maintenance"?

DrByte:

How busy is your site at 2am?
Right now, my site is not busy at all, because I believe that it's not ready yet to go public. I really hope that by next week it will be ready for at least beta testing. However, once it goes public, 2:00 AM here (EST) is 11:00 PM in California and 8:00 AM in Germany... I currently have no idea how busy it will be at any given time, so I will have to track this.

DrByte:

Why do you believe you need to take the site down-for-maintenance in order to do a backup?
Because I don't know whether Zen Cart transactions (as seen by the database) are atomic.

I am afraid of a situation where I restore Zen Cart's database to a state with no data integrity.

Also, I would prefer to back up the database in a state where there are no "transactions in progress". Do you happen to know whether in Zen Cart such scenario is possible?

6 Nov 2008, 3:14 PM
#11
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,515
Plugin Contributions:
176

Re: Automate (cron job) "Down for Maintenance"?

zcnb:

I would prefer to back up the database in a state where there are no "transactions in progress". Do you happen to know whether in Zen Cart such scenario is possible?

Zen Cart's current version doesn't use an "atomic" transaction-based start/rollback/commit methodology, as far as a strict "atomic" definition is concerned.
There are changes coming to support some aspects of that in the next version, but that doesn't address your fears right now.

As it stands now, a transaction is "in progress" for maybe 0.5 to 2.0 seconds depending on server speed and gateway response, when someone is making a payment to complete an order. Outside of that, everything else is merely poking around the store and adding things to the cart etc.

If you're bent on ensuring nobody's "in" your store and doing anything of any sort at the time you're doing a backup, you'll need to set it down for maintenance and then do your backup before finally turning off the down-for-maintenance.

Keep in mind that once the store is set to down-for-maintenance, any and all shopping activity that was in-limbo (shopper added 1 item to cart and was browsing for others) will be suspended until down-for-maintenance is released. There's nothing they can do to affect the "atomic" state once it's in down-for-maintenance. Nevertheless, you might want to wait 60 seconds before you start your backup if you're really wanting to be sure nothing's going on.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.