Run another function in controller class - Background process

#1
[eluser]cinewbie81[/eluser]
Hi all,

In my "Main" Controller index function, i want to call the index() function from "SendSMS" controller class. How can i do it ?
I know it can be done using redirect("SendSMS") statement, but I don't want it to be this way. Instead, i want the SendSms index() function to be running as background process(using the '&' symbol). How can i do it ? Anyone ??

Thanks.

Code:
<?php

class Main extends Controller {
    
    function index()
    {
        // I want to run mybackground_function() here from Runbackground controller
    }
}
?>


Code:
<?php
class SendSMS extends Controller {
    
    function index()
    {
        // sending thousands of SMS here
    }
}
?>

#2
[eluser]tonanbarbarian[/eluser]
try something like this (totally untested)

you use fsockopen to make a connection to the url, writing the data of the request, then just close the socket without reading the reply

Code:
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if (!$fp) {
  log_message('error', 'Unable to connect to server');
  return;
}
$request = "GET /SendSMS/index HTTP/1.1\r\n"
  ."Host: www.example.com\r\n"
  ."Connection: Close\r\n\r\n";
fwrite($fp, $request);
// normally now we would get the result like so
//$result = '';
//while (!feof($fp)) {
//  $result .= fgets($fp, 128);
//}

fclose($fp);

#3
[eluser]cinewbie81[/eluser]
[quote author="tonanbarbarian" date="1197028875"]try something like this (totally untested)

you use fsockopen to make a connection to the url, writing the data of the request, then just close the socket without reading the reply

Code:
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if (!$fp) {
  log_message('error', 'Unable to connect to server');
  return;
}
$request = "GET /SendSMS/index HTTP/1.1\r\n"
  ."Host: www.example.com\r\n"
  ."Connection: Close\r\n\r\n";
fwrite($fp, $request);
// normally now we would get the result like so
//$result = '';
//while (!feof($fp)) {
//  $result .= fgets($fp, 128);
//}

fclose($fp);
[/quote]

I'm totally lost ...
when and where should i write the statement to send the sms ??

#4
[eluser]xwero[/eluser]
The way i would do this is to set up a cron task for the sendsms (class???) function where i would check in the database the messages that haven't been send already.

Background tasks are less recourse demanding using cron than php alternatives.

#5
[eluser]cinewbie81[/eluser]
Hi xwero, mind to elaborate ? I dont quite understand what do u mean Sad

btw, if using the way i'm current working on, is there any solution ?

#6
[eluser]xwero[/eluser]
Instead of a background class you just write a function(s) and set up a cron task(s) to process the function.
Code:
class Main extends Controller {
    
    function index()
    {
        // I want to run mybackground_function() here from Runbackground controller
    }

    function sendonesms()
    {

    }

   function sendmultiplesms()
   {

   }
}
If you use the main controller for other pages the loading time of those pages will slow down because the background process needs recourses too and in the worst case those pages get offline because a background process fails. With a cron task the script is executed separately.

I don't know how you are going to trigger when an sms has to be send but i guess you want it send when they click submit that's why you want to run the background script. Does it matter if it's send that instant or after a minute or two ,or three. Using a cron script that runs every minute you can control the flood of messages so it doesn't overload the sms server/service. But you are free to choose your own interval of course Smile

#7
[eluser]cinewbie81[/eluser]
Thanks.
But may i know what is cron task ? and how to setup a cron task to process those function ??
P/S: It's acceptable if the SMS send a minute later !!

#8
[eluser]tonanbarbarian[/eluser]
put my code anywhere you want.
put it in a library maybe and then when you want the process to run call it

library/sms.php
Code:
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class sms {

  function sms() {
  }

  function background() {
    $fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
    if (!$fp) {
      log_message('error', 'Unable to connect to server');
      return;
    }
    $request = "GET /SendSMS/index HTTP/1.1\r\n"
      ."Host: www.example.com\r\n"
      ."Connection: Close\r\n\r\n";
    fwrite($fp, $request);
    fclose($fp);
  } // background()
} // class sms

then when you want to send the sms dont go to another controller just use the library
Code:
class Main extends Controller {
    
    function index()
    {
        $this->load->library('sms');
        $this->sms->background();
    }
}

as I stated before I am not sure this will work but it is fairly quick to setup and worth a try

#9
[eluser]cinewbie81[/eluser]
hi tonanbarbarian,

I need to use some of the CI built in function in function background(), for eg: this->db->query(), but inside the library it doesn't allow me to do so ...

#10
[eluser]nmweb[/eluser]
Sure it does, http://ellislab.com/codeigniter/user-gui...aries.html check under 'Utilizing CodeIgniter Resources within Your Library'


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.