├── .gitignore ├── CONTRIBUTING.md ├── example ├── run.php ├── enqueue.php ├── lib.php └── stats.php ├── composer.json ├── composer.lock ├── README.md ├── src └── Caterpillar.php └── LICENSE.txt /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | vendor/ 3 | example/log/ -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | By submitting code to this project, you agree to irrevocably release it under the same license as this project. See README.md for more details. -------------------------------------------------------------------------------- /example/run.php: -------------------------------------------------------------------------------- 1 | run_workers(2); 10 | -------------------------------------------------------------------------------- /example/enqueue.php: -------------------------------------------------------------------------------- 1 | queue('TestTask', 'run', [rand(1000,9999)]); 10 | } 11 | -------------------------------------------------------------------------------- /example/lib.php: -------------------------------------------------------------------------------- 1 | print_stats(); 6 | 7 | /* 8 | This will output information about the beanstalk queues, for example: 9 | 10 | tube urgent ready delayed buried using watching 11 | default 0 0 0 0 1 1 12 | caterpillar-test 0 2 0 0 0 0 13 | */ 14 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "p3k/caterpillar", 3 | "description": "Caterpillar is a background queue manager", 4 | "require": { 5 | "php": ">5.4.0", 6 | "pda/pheanstalk": "3.*" 7 | }, 8 | "license": "Apache-2.0", 9 | "authors": [ 10 | { 11 | "name": "Aaron Parecki", 12 | "homepage": "http://aaronparecki.com/" 13 | } 14 | ], 15 | "autoload": { 16 | "psr-0": { 17 | "Caterpillar": "src/" 18 | } 19 | }, 20 | "repositories": [ 21 | { 22 | "type": "git", 23 | "url": "file:///Users/aaronpk/Code/Monocle/vendor/pda/pheanstalk/.git" 24 | } 25 | ] 26 | } 27 | -------------------------------------------------------------------------------- /composer.lock: -------------------------------------------------------------------------------- 1 | { 2 | "_readme": [ 3 | "This file locks the dependencies of your project to a known state", 4 | "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", 5 | "This file is @generated automatically" 6 | ], 7 | "hash": "a0940694bce239d7abcfd1e6435fb249", 8 | "content-hash": "6eaa4b2f79164912133716c65500d4ce", 9 | "packages": [ 10 | { 11 | "name": "pda/pheanstalk", 12 | "version": "v3.1.0", 13 | "source": { 14 | "type": "git", 15 | "url": "https://github.com/pda/pheanstalk.git", 16 | "reference": "430e77c551479aad0c6ada0450ee844cf656a18b" 17 | }, 18 | "dist": { 19 | "type": "zip", 20 | "url": "https://api.github.com/repos/pda/pheanstalk/zipball/430e77c551479aad0c6ada0450ee844cf656a18b", 21 | "reference": "430e77c551479aad0c6ada0450ee844cf656a18b", 22 | "shasum": "" 23 | }, 24 | "require": { 25 | "php": ">=5.3.0" 26 | }, 27 | "require-dev": { 28 | "phpunit/phpunit": "~4.0" 29 | }, 30 | "type": "library", 31 | "extra": { 32 | "branch-alias": { 33 | "dev-master": "3.0-dev" 34 | } 35 | }, 36 | "autoload": { 37 | "psr-4": { 38 | "Pheanstalk\\": "src/" 39 | } 40 | }, 41 | "notification-url": "https://packagist.org/downloads/", 42 | "license": [ 43 | "MIT" 44 | ], 45 | "authors": [ 46 | { 47 | "name": "Paul Annesley", 48 | "email": "paul@annesley.cc", 49 | "homepage": "http://paul.annesley.cc/", 50 | "role": "Developer" 51 | } 52 | ], 53 | "description": "PHP client for beanstalkd queue", 54 | "homepage": "https://github.com/pda/pheanstalk", 55 | "keywords": [ 56 | "beanstalkd" 57 | ], 58 | "time": "2015-08-07 21:42:41" 59 | } 60 | ], 61 | "packages-dev": [], 62 | "aliases": [], 63 | "minimum-stability": "stable", 64 | "stability-flags": [], 65 | "prefer-stable": false, 66 | "prefer-lowest": false, 67 | "platform": { 68 | "php": ">5.4.0" 69 | }, 70 | "platform-dev": [] 71 | } 72 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Caterpillar 2 | =========== 3 | 4 | Caterpillar is a queuing mechanism based on beanstalkd. It handles enqueuing and processing tasks, and managing multiple concurrent workers that can process the same queue. 5 | 6 | You can use Caterpillar to quickly parallelize background tasks simply by running many workers. 7 | 8 | 9 | Installation 10 | ------------ 11 | 12 | Download from source or use Composer. 13 | 14 | `git clone git@github.com:aaronpk/caterpillar.git` 15 | 16 | Add this to your composer.json file in the "require" section: 17 | 18 | `"p3k/caterpillar": "0.1.*"` 19 | 20 | 21 | Usage 22 | ----- 23 | 24 | The working example in the `example` folder is explained in more detail below. 25 | 26 | ### Queuing Tasks 27 | 28 | To queue a task, first create a new `Caterpillar` object and pass in the beanstalkd tube name you want to use, as well as the beanstalkd server and a path to write log files. 29 | 30 | ```php 31 | $c = new Caterpillar('caterpillar-test', '127.0.0.1', 11300, $logdir); 32 | ``` 33 | 34 | You can queue any static method of any class to run. For illustration purposes, we'll use this test task below. All it does is says it's running, waits a random amount of time, and then finishes. 35 | 36 | ```php 37 | class TestTask { 38 | public static function run($val) { 39 | echo "Running task $val ...\n"; 40 | usleep(rand(750000,2000000)); 41 | echo "finished!\n"; 42 | } 43 | } 44 | ``` 45 | 46 | Now we'll queue up 10 tasks and each one is identified by a random number just so they show up better in the logs. 47 | 48 | ``` 49 | for($i=0; $i<10; $i++) { 50 | $c->queue('TestTask', 'run', [rand(1000,9999)]); 51 | } 52 | ``` 53 | 54 | ### Running Workers 55 | 56 | To run the workers, create a script that will run the `run_workers` command on the `Caterpillar` class. You'll first need to make a Caterpillar object the same way you did to queue jobs to set up its config. Then just run the `run_workers` method with the number of concurrent processes you want to run as the first argument. The script will fork this many children and they will all run in parallel, managed by the parent process. 57 | 58 | ```php 59 | require_once(__DIR__.'/vendor/autoload.php'); 60 | 61 | // Make sure you load your environment and any of the classes that are being used as workers. 62 | 63 | $logdir = __DIR__.'/log/'; 64 | 65 | $c = new Caterpillar('caterpillar-test', '127.0.0.1', 11300, $logdir); 66 | $c->run_workers(2); // Runs two workers in the foreground 67 | ``` 68 | 69 | When you run this from the console, the parent process stays in the foreground. You can quit all the workers by pressing CTRL+C and waiting a couple seconds for them to finish. 70 | 71 | If you want to run the parent process in the background, such as when using some system init methods, you can pass `true` as the second argument, e.g. `$c->run_workers(2, true)`. 72 | 73 | #### Running as a System Service 74 | 75 | For production use, you'll likely want to run the workers as a system service so that they start when the server boots, and continue running automatically. How you do this will depend on the particular operating system you're using. You'll need to configure a system-level startup script to run your worker file that we described above. 76 | 77 | ##### systemd 78 | For Ubuntu using systemd, you can create an init script like the below, and save as `/etc/init/yourservice.conf` 79 | 80 | ``` 81 | description "caterpillar worker" 82 | 83 | start on runlevel [2345] 84 | stop on runlevel [016] 85 | 86 | respawn 87 | exec sudo -u ubuntu /usr/bin/php /web/sites/example.com/scripts/worker.php >> /web/sites/example.com/scripts/logs/init.log 2>&1 88 | ``` 89 | 90 | ##### supervisord 91 | Using supervisord, you can create a config file like the below, and save as `/etc/supervisor/conf.d/yourservice.conf` 92 | 93 | ``` 94 | [program:example] 95 | process_name=%(program_name)s_%(process_num)02d 96 | command=php /web/sites/example.com/scripts/worker.php 97 | autostart=true 98 | autorestart=true 99 | user=www-data 100 | numprocs=4 101 | redirect_stderr=true 102 | stdout_logfile=/web/sites/example.com/logs/init.log 103 | 104 | ``` 105 | 106 | API Documentation 107 | ----------------- 108 | 109 | ### `new Caterpillar($tube, $server, $port, $log_path)` 110 | 111 | Creates the Caterpillar object and configures it to use a specific tube, beanstalkd server and log path. 112 | 113 | * `$tube`: (default is the "default" tube) The name of the beanstalkd tube to use 114 | * `$server`: (default 127.0.0.1) The IP address or hostname of the beanstalkd server 115 | * `$port`: (default 11300) The port of the beanstalkd server 116 | * `$log_path`: (default "./log/") The path to use for all log files 117 | 118 | ### `queue($class, $method, $args, $opts)` 119 | 120 | Enqueues a new job onto the tube. 121 | 122 | * `$class`: (string) The class name of the task to run 123 | * `$method`: (string) The name of the static method in the class to run 124 | * `$args`: (array) This array is passed in as arguments to the static function. If you include 3 items in the array, your function must take 3 arguments. 125 | * `$opts`: (array) 126 | ** `delay`: (default 0) The number of seconds to wait before the job will be available to workers 127 | ** `ttr`: (default 300) The "time to run" of the job in seconds. If the job is not completed before the time is up, it will be put back onto the queue by beanstalkd. 128 | ** `priority`: (default 1024) The beanstalkd priority number to set for the job. 129 | 130 | ### `run_workers($num, $background)` 131 | 132 | Runs the number of workers specified as separate child processes. 133 | 134 | * `$num`: (integer) The number of children to run 135 | * `$background`: (true, false) Whether to run the parent process in the background or foreground 136 | 137 | 138 | ### `print_stats()` 139 | 140 | Outputs info about the number of jobs ready, delayed, buried, and the number of processes watching beanstalkd tubes. 141 | 142 | Sample output: 143 | 144 | ``` 145 | tube urgent ready delayed buried using watching 146 | default 0 0 0 0 1 1 147 | caterpillar-test 0 2 0 0 0 0 148 | ``` 149 | 150 | See the beanstalkd documentation for more details on what each of these mean. 151 | 152 | 153 | TODO 154 | ---- 155 | 156 | * Fix log rotation. Figure out why the children are unable to catch the HUP signal the parent sends. 157 | 158 | 159 | 160 | License 161 | ------- 162 | 163 | Copyright 2015-2017 by Aaron Parecki 164 | 165 | Available under the Apache 2.0 License. See LICENSE.txt 166 | 167 | -------------------------------------------------------------------------------- /src/Caterpillar.php: -------------------------------------------------------------------------------- 1 | _bs = new Pheanstalk\Pheanstalk($server, $port); 27 | $this->_tube = $tube; 28 | $this->_server = $server; 29 | $this->_port = $port; 30 | $this->_log_path = $log_path; 31 | } 32 | 33 | public function queue($class, $method, $args=[], $opts=[]) { 34 | $defaults = [ 35 | 'priority' => 1024, 36 | 'delay' => 0, 37 | 'ttr' => 300 38 | ]; 39 | $opts = array_merge($defaults, $opts); 40 | 41 | if(!is_array($args)) 42 | $args = [$args]; 43 | 44 | $this->_bs->putInTube($this->_tube, 45 | json_encode(array('class'=>$class, 'method'=>$method, 'args'=>$args)), 46 | $opts['priority'], 47 | $opts['delay'], 48 | $opts['ttr']); 49 | } 50 | 51 | private function _process(&$job) { 52 | $data = json_decode($job->getData()); 53 | 54 | if(!is_object($data) || !property_exists($data, 'class')) { 55 | echo "Found bad job:\n"; 56 | print_r($data); 57 | echo "\n"; 58 | $this->_bs->delete($job); 59 | return; 60 | } 61 | 62 | echo "===============================================\n"; 63 | echo "# Beginning job: " . $data->class . '::' . $data->method . "\n"; 64 | 65 | call_user_func_array([$data->class, $data->method], $data->args); 66 | 67 | echo "\n# Job Complete\n-----------------------------------------------\n\n"; 68 | $this->_bs->delete($job); 69 | } 70 | 71 | public function print_stats() { 72 | $allTubes = $this->_bs->listTubes(); 73 | 74 | $fields = array( 75 | 'current-jobs-urgent' => 'urgent', 76 | 'current-jobs-ready' => 'ready', 77 | 'current-jobs-delayed' => 'delayed', 78 | 'current-jobs-buried' => 'buried', 79 | 'current-using' => 'using', 80 | 'current-watching' => 'watching' 81 | ); 82 | 83 | echo sprintf('%30s', 'tube'); 84 | foreach($fields as $k=>$v) { 85 | echo "\t" . $v; 86 | } 87 | echo "\n"; 88 | foreach($allTubes as $tube) { 89 | echo sprintf('%30s', $tube) . "\t"; 90 | $stats = $this->_bs->statsTube($tube); 91 | foreach($fields as $k=>$v) { 92 | echo $stats->{$k} . "\t"; 93 | } 94 | echo "\n"; 95 | } 96 | 97 | echo "\n"; 98 | } 99 | 100 | private function _run_worker() { 101 | $this->_bounce_log(); 102 | 103 | $this->_log("watching tube: " . $this->_tube); 104 | 105 | $this->_bs->watch($this->_tube)->ignore('default'); 106 | 107 | while(self::$PCNTL_CONTINUE) 108 | { 109 | if(($job=$this->_bs->reserve(2)) == FALSE) 110 | continue; 111 | 112 | $this->_log("processing job"); 113 | $this->_process($job); 114 | } // while true 115 | 116 | $this->_log("worker finished!"); 117 | } 118 | 119 | public function run_workers($num, $background=false) { 120 | 121 | if($background) { 122 | $this->_daemonize(); 123 | } 124 | 125 | $this->_write_pidfile(); 126 | 127 | $this->_pids = array(); 128 | for($child_id = 0; $child_id < $num; $child_id++) { 129 | // Fork now 130 | $pid = pcntl_fork(); 131 | if($pid === 0) { 132 | // This is the child process 133 | self::$PCNTL_CONTINUE = TRUE; 134 | 135 | // Set up the child signal handler to catch SIGTERM and prevent executing the next while() loop 136 | // pcntl_signal(SIGTERM, function($sig){ 137 | // #$this->_log("Child caught SIGTERM"); 138 | // #self::$PCNTL_CONTINUE = FALSE; 139 | // }); 140 | // pcntl_signal(SIGHUP, function($sig){ 141 | // echo "CHILD CAUGHT SIGHUP\n"; 142 | // $this->_bounce_log(); 143 | // }); 144 | 145 | $this->_log("Child process started"); 146 | 147 | $c = new Caterpillar($this->_tube, $this->_server, $this->_port, $this->_log_path); 148 | $c->_child_id = $child_id; 149 | $c->_run_worker(); 150 | 151 | $this->_log("Child finished gracefully"); 152 | die(); 153 | } else { 154 | // This is the parent process 155 | $this->_pids[] = $pid; 156 | } 157 | } 158 | 159 | //////////////////////////////////////////////////////////////////////////////////////// 160 | // Everything below this line is only run in the parent process 161 | 162 | $this->_child_id = FALSE; 163 | $this->_bounce_log(); 164 | 165 | $this->_log("Parent started " . count($this->_pids) . " child processes", self::$FILE|self::$ERR); 166 | 167 | // pcntl_signal(SIGHUP, function($sig){ 168 | // $this->_bounce_log(); 169 | // $this->_log("Parent caught SIGHUP"); 170 | // foreach($this->_pids as $p) { 171 | // posix_kill($p, SIGHUP); 172 | // } 173 | // $this->_log('Sent SIGHUP to ' . count($this->_pids) . " children"); 174 | // }); 175 | 176 | // Pass off USR1 signals to the children 177 | // pcntl_signal(SIGUSR1, function($sig){ 178 | // $this->_log("Parent caught SIGUSR1! Sending to children..."); 179 | // foreach($this->_pids as $p) { 180 | // posix_kill($p, SIGUSR1); 181 | // } 182 | // $this->_log('Done sending USR1 to ' . count($this->_pids) . " children"); 183 | // }); 184 | 185 | pcntl_signal(SIGTERM, function($sig){ 186 | $this->_log("Parent caught SIGTERM"); 187 | foreach($this->_pids as $p) { 188 | posix_kill($p, SIGTERM); 189 | } 190 | $this->_log('Done killing ' . count($this->_pids) . " children"); 191 | // After the children die, execution will finally reach "parent finished" below 192 | }); 193 | 194 | // Trap CTRL+C signals in case the script was run from a terminal 195 | pcntl_signal(SIGINT, function($sig){ 196 | $this->_log("CTRL+C! Killing children...", self::$FILE+self::$ERR); 197 | foreach($this->_pids as $p) { 198 | posix_kill($p, SIGTERM); 199 | } 200 | $this->_log('Done killing ' . count($this->_pids) . " children", self::$FILE+self::$ERR); 201 | }); 202 | 203 | // Keep waiting for the children until they all exit 204 | foreach($this->_pids as $p) { 205 | while(0 == pcntl_waitpid($p, $status, WNOHANG)) { 206 | pcntl_signal_dispatch(); 207 | sleep(1); 208 | } 209 | } 210 | 211 | $this->_log("Parent finished", self::$FILE+self::$ERR); 212 | $this->_remove_pidfile(); 213 | } 214 | 215 | public function run_foreground() { 216 | $c = new Caterpillar($this->_tube, $this->_server, $this->_port, $this->_log_path); 217 | $c->_child_id = 0; 218 | self::$PCNTL_CONTINUE = TRUE; 219 | $c->_run_worker(); 220 | } 221 | 222 | private function _daemonize() { 223 | // Fork the current process 224 | $pid = pcntl_fork(); 225 | 226 | // Check to make sure it forked okay 227 | if($pid == -1) { 228 | echo "\n Error: The process failed to fork.\n"; 229 | } else if($pid) { 230 | echo "Daemon started with pid: $pid\n"; 231 | //This is the parent process. 232 | exit; 233 | } else { 234 | //We're now in the child process. 235 | } 236 | 237 | $this->_bounce_log(); 238 | 239 | // Detach from the terminal window, so that we stay alive when it is closed 240 | if(posix_setsid() == -1) { 241 | echo "\n Error: Unable to detach from the terminal window.\n"; 242 | } 243 | } 244 | 245 | private function _log($msg, $dst=1) { 246 | if($dst & self::$FILE) { 247 | echo '[' . posix_getpid() . "] $msg\n"; 248 | } 249 | if($dst & self::$ERR) { 250 | fwrite(STDERR, '[' . posix_getpid() . "] $msg\n"); 251 | } 252 | } 253 | 254 | private function _write_pidfile() { 255 | $pidFile = fopen($this->_log_path . $this->_tube . '.pid', 'w'); 256 | fwrite($pidFile, posix_getpid()); 257 | fclose($pidFile); 258 | } 259 | private function _remove_pidfile() { 260 | $pidFile = $this->_log_path . $this->_tube . '.pid'; 261 | unlink($pidFile); 262 | } 263 | 264 | private function _bounce_log() { 265 | global $STDOUT; 266 | 267 | if(isset($STDOUT)) 268 | fclose($STDOUT); 269 | else 270 | fclose(STDOUT); 271 | 272 | if(isset($this->_child_id) && $this->_child_id !== FALSE) 273 | $filename = $this->_log_path . $this->_tube . '-' . $this->_child_id . '.log'; 274 | else 275 | $filename = $this->_log_path . $this->_tube . '.log'; 276 | 277 | $STDOUT = fopen($filename, 'a'); 278 | } 279 | 280 | } 281 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------