Forked from alexllnk/gist:9246d5bd28883bf356983579f5db0176
Last active
June 28, 2019 10:28
-
-
Save m4tlch/6cfd1656acfeda31d0a16653a41906d6 to your computer and use it in GitHub Desktop.
queue requests with flock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function request_queue_handler(Request $request) | |
{ | |
header($_SERVER['SERVER_PROTOCOL'].' 200 OK'); | |
$amo_subdomain = $_POST['account']['subdomain']; | |
$path_structure = '../'.$amo_subdomain.'/queue_files'; | |
$queue_file_prefix = $path_structure.'/queue_file_'; | |
$queue_flag_file = $path_structure.'/queue_flag'; | |
$lead_name = $_POST['leads']['add'][0]['name']; | |
if (!file_exists($path_structure)) { | |
if (!mkdir($path_structure, 0777, true)) { | |
die('Failed to create folders...'); | |
} | |
} | |
file_put_contents($queue_file_prefix.microtime(true), json_encode($_POST)); | |
if (!file_exists($queue_flag_file)) { | |
//file_put_contents($queue_flag_file, true); | |
$fp = fopen($queue_flag_file, "w"); | |
if (flock($fp, LOCK_EX)) { // acquire an exclusive lock | |
sleep(1); | |
while (!empty($files = glob($queue_file_prefix.'*'))) { | |
if (!isset($account_main_settings)) { | |
$account_main_settings = Account::where('amo_domain', $amo_subdomain)->first()->toArray(); | |
$amo = Amoapi::setInstance([ | |
'id' => $account_main_settings['amo_account_id'], | |
'domain' => $account_main_settings['amo_domain'], | |
'login' => $account_main_settings['amo_login'], | |
'hash' => $account_main_settings['amo_api_key'] | |
]); | |
$break_point = true; | |
} | |
if (isset($amo)) { | |
foreach ($files as $file) { | |
$file_data = json_decode(file_get_contents($file), true); | |
sleep(1); | |
$lead = $amo->leads()->create((int) $file_data['leads']['add'][0]['id']); | |
$lead->name = $lead_name.'__'.microtime(true); | |
$lead->updated_at = time() + 1; | |
$leads_to_update[] = $lead; | |
$break_point = true; | |
unlink($file); | |
if (count($leads_to_update) > 499) { | |
try { | |
$amo->leads()->update($leads_to_update); | |
$break_point = true; | |
} catch (Exception $e) { | |
file_put_contents($path_structure.'/exception_data.log', $e->getMessage()); | |
$break_point = true; | |
} finally { | |
flock($fp, LOCK_UN); | |
fclose($fp); | |
unlink($queue_flag_file); | |
$leads_to_update = []; | |
file_put_contents($path_structure.'/logic_init_by_'.$lead_name, true); | |
} | |
} | |
} | |
$break_point = true; | |
} | |
} | |
if (isset($amo)) { | |
if (!empty($leads_to_update)) { | |
try { | |
$amo->leads()->update($leads_to_update); | |
$break_point = true; | |
} catch (Exception $e) { | |
file_put_contents($path_structure.'/exception_data.log', $e->getMessage()); | |
$break_point = true; | |
} finally { | |
flock($fp, LOCK_UN); | |
fclose($fp); | |
unlink($queue_flag_file); | |
file_put_contents($path_structure.'/logic_init_by_'.$lead_name, true); | |
} | |
} | |
} | |
} else { | |
echo "Couldn't get the lock!"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment