-
Notifications
You must be signed in to change notification settings - Fork 6
/
Sync.php
183 lines (160 loc) · 5.53 KB
/
Sync.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
abstract class Klevu_Search_Model_Sync extends Varien_Object
{
/**
* Limit the memory usage of the sync to 70% of the memory
* limit. Considering that the minimum memory requirement
* for Magento at the time of writing is 256MB, this seems
* like a sensible default.
*/
const MEMORY_LIMIT = 0.7;
/**
* Return the cron job code used for the sync model.
*
* @return string
*/
abstract function getJobCode();
/**
* Perform the sync.
*/
abstract function run();
/**
* Run a sync from cron at the specified time. Checks that a cron is not already
* scheduled to run in the 15 minute interval before or after the given time first.
*
* @param DateTime|string $time The scheduled time as a DateTime object or a string
* that is going to be passed into DateTime. Default is "now".
*
* @return $this
*/
public function schedule($time = "now")
{
if (! $time instanceof DateTime) {
$time = new DateTime($time);
} else {
// Don't modify the original parameter
$time = clone $time;
}
$time_str = $time->format("Y-m-d H:i:00");
$before_str = $time->modify("15 minutes ago")->format("Y-m-d H:i:00");
$after_str = $time->modify("30 minutes")->format("Y-m-d H:i:00"); // Modifying the same DateTime object, so it's -15 + 30 = +15 minutes
// Check if Product Sync is not already scheduled to run around (±15 minutes) that time
$collection = Mage::getResourceModel('cron/schedule_collection')
->addFieldToFilter("job_code", $this->getJobCode())
->addFieldToFilter("status", Mage_Cron_Model_Schedule::STATUS_PENDING)
->addFieldToFilter(
"scheduled_at", array(
"from" => $before_str,
"to" => $after_str
)
);
if ($collection->getSize() == 0) {
$schedule = Mage::getModel('cron/schedule');
$schedule
->setJobCode($this->getJobCode())
->setCreatedAt($time_str)
->setScheduledAt($time_str)
->setStatus(Mage_Cron_Model_Schedule::STATUS_PENDING)
->save();
}
return $this;
}
/**
* Check if a sync is currently running from cron. A number of running copies to
* check for can be specified, which is useful if checking if another copy of sync
* is running from sync itself.
*
* Ignores processes that have been running for more than an hour as they are likely
* to have crashed.
*
* @param int $copies
*
* @return bool
*/
public function isRunning($copies = 1)
{
$time = new Datetime("1 hour ago");
$time = $time->format("Y-m-d H:i:00");
$collection = Mage::getResourceModel('cron/schedule_collection')
->addFieldToFilter("job_code", $this->getJobCode())
->addFieldToFilter("status", Mage_Cron_Model_Schedule::STATUS_RUNNING)
->addFieldToFilter("executed_at", array("gteq" => $time));
return $collection->getSize() >= $copies;
}
/**
* Check if the current memory usage is below the limit.
*
* @return bool
*/
protected function isBelowMemoryLimit()
{
$helper = Mage::helper('klevu_search');
$php_memory_limit = ini_get('memory_limit');
$usage = memory_get_usage(true);
if($php_memory_limit < 0){
$this->log(
Zend_Log::DEBUG, sprintf(
"Memory usage: %s of %s.",
$helper->bytesToHumanReadable($usage),
$php_memory_limit
)
);
return true;
}
$limit = $helper->humanReadableToBytes($php_memory_limit);
$this->log(
Zend_Log::DEBUG, sprintf(
"Memory usage: %s of %s.",
$helper->bytesToHumanReadable($usage),
$helper->bytesToHumanReadable($limit)
)
);
if ($usage / $limit > static::MEMORY_LIMIT) {
return false;
} else {
return true;
}
}
/**
* Check if the memory limit has been reached and reschedule to run
* again immediately if so.
*
* @return bool true if a new process was scheduled, false otherwise.
*/
protected function rescheduleIfOutOfMemory()
{
if (!$this->isBelowMemoryLimit()) {
Mage::getSingleton('core/session')->setMemoryMessage("There may be few products left which will be synced during the next sync process.");
$this->log(Zend_Log::INFO, "Memory limit reached. Stopped and rescheduled.");
if(Mage::helper("klevu_search/config")->isExternalCronEnabled()) {
$this->schedule();
}
return true;
}
return false;
}
/**
* Return the table name for the given model entity.
*
* @param string $entity
*
* @return string
*/
protected function getTableName($entity)
{
return Mage::getModel('core/resource')->getTableName($entity);
}
/**
* Write a message to the log file.
*
* @param int $level
* @param string $message
*
* @return $this
*/
protected function log($level, $message)
{
Mage::helper('klevu_search')->log($level, sprintf("[%s] %s", $this->getJobCode(), $message));
return $this;
}
}